+++ /dev/null
-# -*- coding: utf-8 -*-
-"""
- Jinja fabfile
- ~~~~~~~~~~~~~
-
- Shortcuts for various tasks.
-
- :copyright: Copyright 2008 by Armin Ronacher.
- :license: BSD.
-"""
-
-
-def test():
- """Run the testsuite."""
- local("cd tests; py.test")
-
-
-def pylint():
- """Run pylint."""
- local("pylint --rcfile scripts/pylintrc jinja")
-
-
-def release(**kwargs):
- """Release, tag and upload Jinja2 to the Cheeseshop."""
- import re
- _version_re = re.compile(r'VERSION\s*=\s["\'](.*?)["\']')
- f = file("setup.py")
- try:
- for line in f:
- match = _version_re.match(line)
- if match is not None:
- version = match.group(1)
- break
- else:
- raise RuntimeError('no version def in setup.py :-/')
- finally:
- f.close()
-
- local('hg tag -m "%s" "%s"' % ('tagged %r' % version, version))
- local('python setup.py release sdist upload')
+++ /dev/null
-# -*- coding: utf-8 -*-
-"""
- conftest
- ~~~~~~~~
-
- Configure py.test for support stuff.
-
- :copyright: (c) 2009 by the Jinja Team.
- :license: BSD, see LICENSE for more details.
-"""
-
-import os
-import sys
-sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
-
-import py
-from jinja2 import Environment
-from jinja2.loaders import BaseLoader
-from jinja2.exceptions import TemplateNotFound
-
-
-NOSE = 'nose' in sys.modules
-if NOSE:
- import inspect
- from nose import case
-
- def runTest(self):
- args = list(self.arg)
- if 'env' in inspect.getargspec(self.test).args:
- args.insert(0, simple_env)
- self.test(*args)
- case.TestBase.runTest = runTest
-
-
-try:
- # This code adds support for coverage.py (see
- # http://nedbatchelder.com/code/modules/coverage.html).
- # It prints a coverage report for the modules specified in all
- # module globals (of the test modules) named "coverage_modules".
-
- import coverage, atexit
-
- IGNORED_MODULES = ['jinja2._speedups', 'jinja2.defaults',
- 'jinja2.translators']
-
- def report_coverage():
- coverage.stop()
- module_list = [
- mod for name, mod in sys.modules.copy().iteritems() if
- getattr(mod, '__file__', None) and
- name.startswith('jinja2.') and
- name not in IGNORED_MODULES
- ]
- module_list.sort()
- coverage.report(module_list)
-
- def callback(option, opt_str, value, parser):
- atexit.register(report_coverage)
- coverage.erase()
- coverage.start()
-
- py.test.config.addoptions('Test options', py.test.config.Option('-C',
- '--coverage', action='callback', callback=callback,
- help='Output information about code coverage (slow!)'))
-
-except ImportError:
- coverage = None
-
-
-class GlobalLoader(BaseLoader):
- # Should be overwritten by importing module (test file) in order to find TEMPLATE vars
- scope = globals()
-
- def get_source(self, environment, name):
- try:
- return self.scope[name.upper() + 'TEMPLATE'], None, None
- except KeyError:
- raise TemplateNotFound(name)
-
-
-loader = GlobalLoader()
-simple_env = Environment(trim_blocks=True, loader=loader, cache_size=0)
-
-
-class Directory(py.test.collect.Directory):
-
- def run(self):
- rv = super(Directory, self).run()
- if self.fspath.basename == 'tests':
- rv.append('doctests')
- return rv
-
- def join(self, name):
- if name == 'doctests':
- return JinjaDocTestModule(name, parent=self)
- return super(Directory, self).join(name)
-
-
-class Module(py.test.collect.Module):
-
- def __init__(self, *args, **kwargs):
- self.env = simple_env
- super(Module, self).__init__(*args, **kwargs)
-
- def makeitem(self, name, obj, usefilters=True):
- if name.startswith('test_'):
- if hasattr(obj, 'func_code'):
- return JinjaTestFunction(name, parent=self)
- elif isinstance(obj, basestring):
- return JinjaDocTest(name, parent=self)
-
-
-class JinjaTestFunction(py.test.collect.Function):
-
- def execute(self, target, *args):
- loader.scope = target.func_globals
- co = target.func_code
- if 'env' in co.co_varnames[:co.co_argcount]:
- target(self.parent.env, *args)
- else:
- target(*args)
-
-
-class JinjaDocTest(py.test.collect.Item):
-
- def __init__(self, *args, **kwargs):
- realmod = kwargs.pop('realmod', False)
- super(JinjaDocTest, self).__init__(*args, **kwargs)
- self.realmod = realmod
-
- def run(self):
- if self.realmod:
- mod = __import__(self.name, None, None, [''])
- else:
- mod = py.std.types.ModuleType(self.name)
- mod.__doc__ = self.obj
- mod.env = self.parent.env
- mod.MODULE = self.parent.obj
- self.execute(mod)
-
- def execute(self, mod):
- failed, tot = py.compat.doctest.testmod(mod, verbose=True)
- if failed:
- py.test.fail('doctest %s: %s failed out of %s' % (
- self.fspath, failed, tot))
-
-
-class JinjaDocTestModule(py.test.collect.Module):
-
- def __init__(self, *args, **kwargs):
- super(JinjaDocTestModule, self).__init__(*args, **kwargs)
- self.doctest_modules = [
- 'jinja2.environment', 'jinja2.compiler', 'jinja2.parser',
- 'jinja2.lexer', 'jinja2.ext', 'jinja2.sandbox',
- 'jinja2.filters', 'jinja2.tests', 'jinja2.utils',
- 'jinja2.runtime'
- ]
-
- def run(self):
- return self.doctest_modules
-
- def join(self, name):
- return JinjaDocTest(name, parent=self, realmod=True)
from jinja2 import Environment
from test_loaders import filesystem_loader
-import conftest
-if conftest.NOSE:
- import sys
- MODULE = sys.modules[__name__]
-
-
env = Environment(loader=filesystem_loader)
def test_runtime_error():
'''
->>> tmpl = MODULE.env.get_template('broken.html')
+>>> tmpl = env.get_template('broken.html')
>>> tmpl.render(fail=lambda: 1 / 0)
Traceback (most recent call last):
File "loaderres/templates/broken.html", line 2, in top-level template code
def test_syntax_error():
'''
->>> tmpl = MODULE.env.get_template('syntaxerror.html')
+>>> tmpl = env.get_template('syntaxerror.html')
Traceback (most recent call last):
...
TemplateSyntaxError: unknown tag 'endif'
"""
from jinja2 import Markup, Environment
-import conftest
+env = Environment()
CAPITALIZE = '''{{ "foo bar"|capitalize }}'''
SORT2 = '''{{ ['foo', 'Bar', 'blah']|sort }}'''
-def test_capitalize(env):
+def test_capitalize():
tmpl = env.from_string(CAPITALIZE)
assert tmpl.render() == 'Foo bar'
-def test_center(env):
+def test_center():
tmpl = env.from_string(CENTER)
assert tmpl.render() == ' foo '
-def test_default(env):
+def test_default():
tmpl = env.from_string(DEFAULT)
assert tmpl.render(given='yes') == 'no|False|no|yes'
-def test_dictsort(env):
+def test_dictsort():
tmpl = env.from_string(DICTSORT)
out = tmpl.render(foo={"a": 0, "b": 1, "c": 2, "A": 3})
assert out == ("[('a', 0), ('A', 3), ('b', 1), ('c', 2)]|"
"[('a', 0), ('b', 1), ('c', 2), ('A', 3)]")
-def test_batch(env):
+def test_batch():
tmpl = env.from_string(BATCH)
out = tmpl.render(foo=range(10))
assert out == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|"
"[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]")
-def test_slice(env):
+def test_slice():
tmpl = env.from_string(SLICE)
out = tmpl.render(foo=range(10))
assert out == ("[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"
"[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]")
-def test_escape(env):
+def test_escape():
tmpl = env.from_string(ESCAPE)
out = tmpl.render()
assert out == '<">&'
-def test_striptags(env):
+def test_striptags():
tmpl = env.from_string(STRIPTAGS)
out = tmpl.render(foo=' <p>just a small \n <a href="#">'
'example</a> link</p>\n<p>to a webpage</p> '
assert out == 'just a small example link to a webpage'
-def test_filesizeformat(env):
+def test_filesizeformat():
tmpl = env.from_string(FILESIZEFORMAT)
out = tmpl.render()
assert out == (
)
-def test_first(env):
+def test_first():
tmpl = env.from_string(FIRST)
out = tmpl.render(foo=range(10))
assert out == '0'
-def test_float(env):
+def test_float():
tmpl = env.from_string(FLOAT)
out = tmpl.render()
assert out == '42.0|0.0|32.32'
-def test_format(env):
+def test_format():
tmpl = env.from_string(FORMAT)
out = tmpl.render()
assert out == 'a|b'
-def test_indent(env):
+def test_indent():
tmpl = env.from_string(INDENT)
text = '\n'.join([' '.join(['foo', 'bar'] * 2)] * 2)
out = tmpl.render(foo=text)
'foo bar foo bar\n foo bar foo bar')
-def test_int(env):
+def test_int():
tmpl = env.from_string(INT)
out = tmpl.render()
assert out == '42|0|32'
-def test_join(env):
+def test_join():
tmpl = env.from_string(JOIN)
out = tmpl.render()
assert out == '1|2|3'
assert tmpl.render() == '<foo><span>foo</span>'
-def test_last(env):
+def test_last():
tmpl = env.from_string(LAST)
out = tmpl.render(foo=range(10))
assert out == '9'
-def test_length(env):
+def test_length():
tmpl = env.from_string(LENGTH)
out = tmpl.render()
assert out == '11'
-def test_lower(env):
+def test_lower():
tmpl = env.from_string(LOWER)
out = tmpl.render()
assert out == 'foo'
-def test_pprint(env):
+def test_pprint():
from pprint import pformat
tmpl = env.from_string(PPRINT)
data = range(1000)
assert tmpl.render(data=data) == pformat(data)
-def test_random(env):
+def test_random():
tmpl = env.from_string(RANDOM)
seq = range(100)
for _ in range(10):
assert int(tmpl.render(seq=seq)) in seq
-def test_reverse(env):
+def test_reverse():
tmpl = env.from_string(REVERSE)
assert tmpl.render() == 'raboof|[3, 2, 1]'
-def test_string(env):
+def test_string():
tmpl = env.from_string(STRING)
assert tmpl.render(foo=range(10)) == unicode(xrange(10))
-def test_title(env):
+def test_title():
tmpl = env.from_string(TITLE)
assert tmpl.render() == "Foo Bar"
-def NOT_WORKING_test_truncate(env):
- tmpl = env.from_string(TRUNCATE)
- assert tmpl.render() == 'foo'
-
-
-def test_truncate2(env):
+def test_truncate():
tmpl = env.from_string(TRUNCATE)
out = tmpl.render(data='foobar baz bar' * 1000,
smalldata='foobar baz bar')
assert out == 'foobar baz barf>>>|foobar baz >>>|foobar baz bar'
-def test_upper(env):
+def test_upper():
tmpl = env.from_string(UPPER)
assert tmpl.render() == 'FOO'
-def test_urlize(env):
+def test_urlize():
tmpl = env.from_string(URLIZE)
assert tmpl.render() == 'foo <a href="http://www.example.com/">'\
'http://www.example.com/</a> bar'
-def test_wordcount(env):
+def test_wordcount():
tmpl = env.from_string(WORDCOUNT)
assert tmpl.render() == '3'
-def test_block(env):
+def test_block():
tmpl = env.from_string(BLOCK)
assert tmpl.render() == '<hehe>'
-def test_chaining(env):
+def test_chaining():
tmpl = env.from_string(CHAINING)
assert tmpl.render() == '<FOO>'
-def test_sum(env):
+def test_sum():
tmpl = env.from_string(SUM)
assert tmpl.render() == '21'
-def test_abs(env):
+def test_abs():
tmpl = env.from_string(ABS)
return tmpl.render() == '1|1'
-def test_round(env):
+def test_round():
tmpl = env.from_string(ROUND)
return tmpl.render() == '3.0|2.0|2.1|3.0'
-def test_xmlattr(env):
+def test_xmlattr():
tmpl = env.from_string(XMLATTR)
out = tmpl.render().split()
assert len(out) == 3
assert 'blub:blub="<?>"' in out
-def test_sort1(env):
+def test_sort1():
tmpl = env.from_string(SORT1)
assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
-def test_groupby(env):
+def test_groupby():
tmpl = env.from_string(GROUPBY)
assert tmpl.render().splitlines() == [
"1: {'foo': 1, 'bar': 2}, {'foo': 1, 'bar': 1}",
]
-def test_filtertag(env):
+def test_filtertag():
tmpl = env.from_string(FILTERTAG)
assert tmpl.render() == 'fooBAR'
assert tmpl.render(string=Markup('foo')) == 'f>x<>x<'
-def test_forceescape(env):
+def test_forceescape():
tmpl = env.from_string('{{ x|forceescape }}')
assert tmpl.render(x=Markup('<div />')) == u'<div />'
assert tmpl.render() == '<div>foo</div>'
-def test_sort2(env):
+def test_sort2():
assert env.from_string(SORT2).render() == "['Bar', 'blah', 'foo']"
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
+from jinja2 import Environment
from jinja2.exceptions import UndefinedError, TemplateSyntaxError
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+from nose.tools import assert_raises
+
+env = Environment()
SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''
{% for loop in seq %}...{% endfor %}'''
-def test_simple(env):
+def test_simple():
tmpl = env.from_string(SIMPLE)
assert tmpl.render(seq=range(10)) == '0123456789'
-def test_else(env):
+def test_else():
tmpl = env.from_string(ELSE)
assert tmpl.render() == '...'
-def test_empty_blocks(env):
+def test_empty_blocks():
tmpl = env.from_string(EMPTYBLOCKS)
assert tmpl.render() == '<>'
-def test_context_vars(env):
+def test_context_vars():
tmpl = env.from_string(CONTEXTVARS)
one, two, _ = tmpl.render(seq=[0, 1]).split('###')
(one_index, one_index0, one_revindex, one_revindex0, one_first,
assert one_length == two_length == '2'
-def test_cycling(env):
+def test_cycling():
tmpl = env.from_string(CYCLING)
output = tmpl.render(seq=range(4), through=('<1>', '<2>'))
assert output == '<1><2>' * 4
-def test_scope(env):
+def test_scope():
tmpl = env.from_string(SCOPE)
output = tmpl.render(seq=range(10))
assert not output
-def test_varlen(env):
+def test_varlen():
def inner():
for item in range(5):
yield item
assert output == '01234'
-def test_noniter(env):
+def test_noniter():
tmpl = env.from_string(NONITER)
- raises(TypeError, tmpl.render)
+ assert_raises(TypeError, tmpl.render)
-def test_recursive(env):
+def test_recursive():
tmpl = env.from_string(RECURSIVE)
assert tmpl.render(seq=[
dict(a=1, b=[dict(a=1), dict(a=2)]),
]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]'
-def test_looploop(env):
+def test_looploop():
tmpl = env.from_string(LOOPLOOP)
assert tmpl.render(table=['ab', 'cd']) == '[1|1][1|2][2|1][2|2]'
-def test_reversed_bug(env):
+def test_reversed_bug():
tmpl = env.from_string('{% for i in items %}{{ i }}{% if not loop.last %}'
',{% endif %}{% endfor %}')
assert tmpl.render(items=reversed([3, 2, 1])) == '1,2,3'
-def test_loop_errors(env):
+def test_loop_errors():
tmpl = env.from_string(LOOPERROR1)
- raises(UndefinedError, tmpl.render)
+ assert_raises(UndefinedError, tmpl.render)
tmpl = env.from_string(LOOPERROR2)
assert tmpl.render() == ''
-def test_loop_filter(env):
+def test_loop_filter():
tmpl = env.from_string(LOOPFILTER)
assert tmpl.render() == '[0][2][4][6][8]'
tmpl = env.from_string(EXTENDEDLOOPFILTER)
assert tmpl.render() == '[1:0][2:2][3:4][4:6][5:8]'
-def test_loop_unassignable(env):
- raises(TemplateSyntaxError, env.from_string, LOOPUNASSIGNABLE)
+def test_loop_unassignable():
+ assert_raises(TemplateSyntaxError, env.from_string, LOOPUNASSIGNABLE)
-def test_scoped_special_var(env):
+def test_scoped_special_var():
t = env.from_string('{% for s in seq %}[{{ loop.first }}{% for c in s %}'
'|{{ loop.first }}{% endfor %}]{% endfor %}')
assert t.render(seq=('ab', 'cd')) == '[True|True|False][False|True|False]'
-def test_scoped_loop_var(env):
+def test_scoped_loop_var():
t = env.from_string('{% for x in seq %}{{ loop.first }}'
'{% for y in seq %}{% endfor %}{% endfor %}')
assert t.render(seq='ab') == 'TrueFalse'
assert t.render(seq='ab') == 'TrueFalseTrueFalse'
-def test_recursive_empty_loop_iter(env):
+def test_recursive_empty_loop_iter():
t = env.from_string('''
{%- for item in foo recursive -%}{%- endfor -%}
''')
assert t.render(dict(foo=[])) == ''
-def test_call_in_loop(env):
+def test_call_in_loop():
t = env.from_string('''
{%- macro do_something() -%}
[{{ caller() }}]
assert t.render() == '[1][2][3]'
-def test_scoping_bug(env):
+def test_scoping_bug():
t = env.from_string('''
{%- for item in foo %}...{{ item }}...{% endfor %}
{%- macro item(a) %}...{{ a }}...{% endmacro %}
:copyright: Copyright 2009 by the Jinja Team.
:license: BSD.
"""
+from jinja2 import Environment
+env = Environment()
-import conftest
-
-def test_assigned_scoping(env):
+def test_assigned_scoping():
t = env.from_string('''
{%- for item in (1, 2, 3, 4) -%}
[{{ item }}]
assert t.render() == '[1][2][3][4]42'
-def test_closure_scoping(env):
+def test_closure_scoping():
t = env.from_string('''
{%- set wrapper = "<FOO>" %}
{%- for item in (1, 2, 3, 4) %}
from jinja2 import Environment, DictLoader, contextfunction
from jinja2.exceptions import TemplateAssertionError
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+from nose.tools import assert_raises
templates = {
'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'
tmpl = i18n_env.from_string('{% trans foo=42, count=2 %}{{ count }} item{% '
'pluralize count %}{{ count }} items{% endtrans %}')
assert tmpl.render() == '2 items'
- raises(TemplateAssertionError, i18n_env.from_string,
- '{% trans foo %}...{% pluralize bar %}...{% endtrans %}')
+ assert_raises(TemplateAssertionError, i18n_env.from_string,
+ '{% trans foo %}...{% pluralize bar %}...{% endtrans %}')
def test_trans_stringformatting():
:license: BSD, see LICENSE for more details.
"""
-import conftest
+from jinja2 import Environment
+env = Environment()
SIMPLE = '''{% if true %}...{% endif %}'''
EMPTY = '''[{% if true %}{% else %}{% endif %}]'''
-def test_simple(env):
+def test_simple():
tmpl = env.from_string(SIMPLE)
assert tmpl.render() == '...'
-def test_elif(env):
+def test_elif():
tmpl = env.from_string(ELIF)
assert tmpl.render() == '...'
-def test_else(env):
+def test_else():
tmpl = env.from_string(ELSE)
assert tmpl.render() == '...'
-def test_empty(env):
+def test_empty():
tmpl = env.from_string(EMPTY)
assert tmpl.render() == '[]'
-def test_complete(env):
+def test_complete():
tmpl = env.from_string('{% if a %}A{% elif b %}B{% elif c == d %}'
'C{% else %}D{% endif %}')
assert tmpl.render(a=0, b=False, c=42, d=42.0) == 'C'
-def test_no_scope(env):
+def test_no_scope():
tmpl = env.from_string('{% if a %}{% set foo = 1 %}{% endif %}{{ foo }}')
assert tmpl.render(a=True) == '1'
tmpl = env.from_string('{% if true %}{% set foo = 1 %}{% endif %}{{ foo }}')
from jinja2 import Environment, DictLoader
from jinja2.exceptions import TemplateNotFound
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+from nose.tools import assert_raises
test_env = Environment(loader=DictLoader(dict(
def test_include_ignoring_missing():
t = test_env.from_string('{% include "missing" %}')
- raises(TemplateNotFound, t.render)
+ assert_raises(TemplateNotFound, t.render)
for extra in '', 'with context', 'without context':
t = test_env.from_string('{% include "missing" ignore missing ' +
extra + ' %}')
from jinja2 import Environment, DictLoader
from jinja2.exceptions import TemplateSyntaxError
-import conftest
-conftest.GlobalLoader.scope = globals()
-
LAYOUTTEMPLATE = '''\
|{% block block1 %}block 1 from layout{% endblock %}
{% endblock %}
'''
-def test_layout(env):
+env = Environment(loader=DictLoader({
+ 'layout': LAYOUTTEMPLATE,
+ 'level1': LEVEL1TEMPLATE,
+ 'level2': LEVEL2TEMPLATE,
+ 'level3': LEVEL3TEMPLATE,
+ 'level4': LEVEL4TEMPLATE,
+ 'working': WORKINGTEMPLATE
+}), trim_blocks=True)
+
+def test_layout():
tmpl = env.get_template('layout')
assert tmpl.render() == ('|block 1 from layout|block 2 from '
'layout|nested block 4 from layout|')
-def test_level1(env):
+def test_level1():
tmpl = env.get_template('level1')
assert tmpl.render() == ('|block 1 from level1|block 2 from '
'layout|nested block 4 from layout|')
-def test_level2(env):
+def test_level2():
tmpl = env.get_template('level2')
assert tmpl.render() == ('|block 1 from level1|nested block 5 from '
'level2|nested block 4 from layout|')
-def test_level3(env):
+def test_level3():
tmpl = env.get_template('level3')
assert tmpl.render() == ('|block 1 from level1|block 5 from level3|'
'block 4 from level3|')
-def test_level4(env):
+def test_level4():
tmpl = env.get_template('level4')
assert tmpl.render() == ('|block 1 from level1|block 5 from '
'level3|block 3 from level4|')
assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'
-def test_working(env):
+def test_working():
tmpl = env.get_template('working')
-def test_reuse_blocks(env):
+def test_reuse_blocks():
tmpl = env.from_string('{{ self.foo() }}|{% block foo %}42{% endblock %}|{{ self.foo() }}')
assert tmpl.render() == '42|42|42'
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
+from jinja2 import Environment
-import conftest
+env = Environment()
RAW = '{% raw %}foo{% endraw %}|{%raw%}{{ bar }}|{% baz %}{% endraw %}'
BYTEFALLBACK = u'''{{ 'foo'|pprint }}|{{ 'bär'|pprint }}'''
-def test_raw(env):
+def test_raw():
tmpl = env.from_string(RAW)
assert tmpl.render() == 'foo|{{ bar }}|{% baz %}'
"<li>1</li>\n <li>2</li>\n</ul>")
-def test_string_escapes(env):
+def test_string_escapes():
for char in u'\0', u'\u2668', u'\xe4', u'\t', u'\r', u'\n':
tmpl = env.from_string('{{ %s }}' % repr(char)[1:])
assert tmpl.render() == char
assert env.from_string('{{ "\N{HOT SPRINGS}" }}').render() == u'\u2668'
-def test_bytefallback(env):
+def test_bytefallback():
tmpl = env.from_string(BYTEFALLBACK)
assert tmpl.render() == u"'foo'|u'b\\xe4r'"
-def test_operators(env):
+def test_operators():
from jinja2.lexer import operators
for test, expect in operators.iteritems():
if test in '([{}])':
from jinja2.loaders import split_template_path
from jinja2.exceptions import TemplateNotFound
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+from nose.tools import assert_raises
dict_loader = loaders.DictLoader({
env = Environment(loader=dict_loader)
tmpl = env.get_template('justdict.html')
assert tmpl.render().strip() == 'FOO'
- raises(TemplateNotFound, env.get_template, 'missing.html')
+ assert_raises(TemplateNotFound, env.get_template, 'missing.html')
def test_package_loader():
env = Environment(loader=package_loader)
tmpl = env.get_template('test.html')
assert tmpl.render().strip() == 'BAR'
- raises(TemplateNotFound, env.get_template, 'missing.html')
+ assert_raises(TemplateNotFound, env.get_template, 'missing.html')
def test_filesystem_loader():
assert tmpl.render().strip() == 'BAR'
tmpl = env.get_template('foo/test.html')
assert tmpl.render().strip() == 'FOO'
- raises(TemplateNotFound, env.get_template, 'missing.html')
+ assert_raises(TemplateNotFound, env.get_template, 'missing.html')
def test_choice_loader():
assert tmpl.render().strip() == 'FOO'
tmpl = env.get_template('test.html')
assert tmpl.render().strip() == 'BAR'
- raises(TemplateNotFound, env.get_template, 'missing.html')
+ assert_raises(TemplateNotFound, env.get_template, 'missing.html')
def test_function_loader():
env = Environment(loader=function_loader)
tmpl = env.get_template('justfunction.html')
assert tmpl.render().strip() == 'FOO'
- raises(TemplateNotFound, env.get_template, 'missing.html')
+ assert_raises(TemplateNotFound, env.get_template, 'missing.html')
def test_prefix_loader():
assert tmpl.render().strip() == 'BAR'
tmpl = env.get_template('b/justdict.html')
assert tmpl.render().strip() == 'FOO'
- raises(TemplateNotFound, env.get_template, 'missing')
+ assert_raises(TemplateNotFound, env.get_template, 'missing')
def test_caching():
def test_split_template_path():
assert split_template_path('foo/bar') == ['foo', 'bar']
assert split_template_path('./foo/bar') == ['foo', 'bar']
- raises(TemplateNotFound, split_template_path, '../foo')
+ assert_raises(TemplateNotFound, split_template_path, '../foo')
:license: BSD, see LICENSE for more details.
"""
-import conftest
-conftest.GlobalLoader.scope = globals()
+from jinja2 import Environment, DictLoader
+
+env = Environment(trim_blocks=True)
SIMPLE = '''\
INCLUDETEMPLATE = '''{% macro test(foo) %}[{{ foo }}]{% endmacro %}'''
-def test_simple(env):
+def test_simple():
tmpl = env.from_string(SIMPLE)
assert tmpl.render() == 'Hello Peter!'
-def test_scoping(env):
+def test_scoping():
tmpl = env.from_string(SCOPING)
assert tmpl.render() == 'foo|bar'
-def test_arguments(env):
+def test_arguments():
tmpl = env.from_string(ARGUMENTS)
assert tmpl.render() == '||c|d|a||c|d|a|b|c|d|1|2|3|d'
-def test_varargs(env):
+def test_varargs():
tmpl = env.from_string(VARARGS)
assert tmpl.render() == '1|2|3'
-def test_simple_call(env):
+def test_simple_call():
tmpl = env.from_string(SIMPLECALL)
assert tmpl.render() == '[[data]]'
-def test_complex_call(env):
+def test_complex_call():
tmpl = env.from_string(COMPLEXCALL)
assert tmpl.render() == '[[data]]'
-def test_caller_undefined(env):
+def test_caller_undefined():
tmpl = env.from_string(CALLERUNDEFINED)
assert tmpl.render() == 'True'
-def test_include(env):
+def test_include():
+ env = Environment(loader=DictLoader({'include': INCLUDETEMPLATE}))
tmpl = env.from_string('{% from "include" import test %}{{ test("foo") }}')
assert tmpl.render() == '[foo]'
-def test_macro_api(env):
+def test_macro_api():
tmpl = env.from_string('{% macro foo(a, b) %}{% endmacro %}'
'{% macro bar() %}{{ varargs }}{{ kwargs }}{% endmacro %}'
'{% macro baz() %}{{ caller() }}{% endmacro %}')
"""
from jinja2 import Environment, DictLoader, TemplateSyntaxError
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+env = Environment()
+
+from nose.tools import assert_raises
def test_keyword_folding():
assert t.render(expr=True) == '((title))'
-def test_urlize_filter_escaping(env):
+def test_urlize_filter_escaping():
tmpl = env.from_string('{{ "http://www.example.org/<foo"|urlize }}')
assert tmpl.render() == '<a href="http://www.example.org/<foo">http://www.example.org/<foo</a>'
-def test_loop_call_loop(env):
+def test_loop_call_loop():
tmpl = env.from_string('''
{% macro test() %}
def test_weird_inline_comment():
env = Environment(line_statement_prefix='%')
- raises(TemplateSyntaxError, env.from_string,
- '% for item in seq {# missing #}\n...% endfor')
+ assert_raises(TemplateSyntaxError, env.from_string,
+ '% for item in seq {# missing #}\n...% endfor')
-def test_old_macro_loop_scoping_bug(env):
+def test_old_macro_loop_scoping_bug():
tmpl = env.from_string('{% for i in (1, 2) %}{{ i }}{% endfor %}'
'{% macro i() %}3{% endmacro %}{{ i() }}')
assert tmpl.render() == '123'
"""
from jinja2 import Environment
-import conftest
+env = Environment()
PHP_SYNTAX = '''\
assert tmpl.render(seq=range(5)) == '01234'
-def test_balancing(env):
+def test_balancing():
tmpl = env.from_string(BALANCING)
assert tmpl.render() == 'bar'
-def test_start_comment(env):
+def test_start_comment():
tmpl = env.from_string(STARTCOMMENT)
assert tmpl.render().strip() == 'foo'
from jinja2.exceptions import SecurityError, TemplateSyntaxError
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
- import sys
- MODULE = sys.modules[__name__]
-else:
- from py.test import raises
+from nose.tools import assert_raises
class PrivateStuff(object):
def test_unsafe():
'''
->>> env = MODULE.SandboxedEnvironment()
->>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff())
+>>> env = SandboxedEnvironment()
+>>> env.from_string("{{ foo.foo() }}").render(foo=PrivateStuff())
Traceback (most recent call last):
...
SecurityError: <bound method PrivateStuff.foo of PrivateStuff> is not safely callable
->>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PrivateStuff())
+>>> env.from_string("{{ foo.bar() }}").render(foo=PrivateStuff())
u'23'
->>> env.from_string("{{ foo._foo() }}").render(foo=MODULE.PublicStuff())
+>>> env.from_string("{{ foo._foo() }}").render(foo=PublicStuff())
Traceback (most recent call last):
...
SecurityError: access to attribute '_foo' of 'PublicStuff' object is unsafe.
->>> env.from_string("{{ foo.bar() }}").render(foo=MODULE.PublicStuff())
+>>> env.from_string("{{ foo.bar() }}").render(foo=PublicStuff())
u'23'
>>> env.from_string("{{ foo.__class__ }}").render(foo=42)
def test_restricted():
env = SandboxedEnvironment()
- raises(TemplateSyntaxError, env.from_string,
- "{% for item.attribute in seq %}...{% endfor %}")
- raises(TemplateSyntaxError, env.from_string,
- "{% for foo, bar.baz in seq %}...{% endfor %}")
+ assert_raises(TemplateSyntaxError, env.from_string,
+ "{% for item.attribute in seq %}...{% endfor %}")
+ assert_raises(TemplateSyntaxError, env.from_string,
+ "{% for foo, bar.baz in seq %}...{% endfor %}")
def test_immutable_environment():
'''
->>> env = MODULE.ImmutableSandboxedEnvironment()
+>>> env = ImmutableSandboxedEnvironment()
>>> env.from_string('{{ [].append(23) }}').render()
Traceback (most recent call last):
...
def test_attr_filter():
env = SandboxedEnvironment()
tmpl = env.from_string('{{ 42|attr("__class__")|attr("__subclasses__")() }}')
- raises(SecurityError, tmpl.render)
+ assert_raises(SecurityError, tmpl.render)
:license: BSD, see LICENSE for more details.
"""
-import conftest
-if conftest.NOSE:
- env = conftest.simple_env
+from jinja2 import Environment
+env = Environment()
def test_basic_streaming():
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from jinja2 import Environment, DictLoader
+from jinja2 import Environment
from jinja2.exceptions import TemplateSyntaxError, UndefinedError
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+from nose.tools import assert_raises
+
+env = Environment()
CALL = '''{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}'''
def test_call():
- from jinja2 import Environment
env = Environment()
env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g
tmpl = env.from_string(CALL)
assert tmpl.render() == 'abdfh'
-def test_slicing(env):
+def test_slicing():
tmpl = env.from_string(SLICING)
assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
-def test_attr(env):
+def test_attr():
tmpl = env.from_string(ATTR)
assert tmpl.render(foo={'bar': 42}) == '42|42'
-def test_subscript(env):
+def test_subscript():
tmpl = env.from_string(SUBSCRIPT)
assert tmpl.render(foo=[0, 1, 2]) == '0|2'
-def test_tuple(env):
+def test_tuple():
tmpl = env.from_string(TUPLE)
assert tmpl.render() == '()|(1,)|(1, 2)'
-def test_math(env):
+def test_math():
tmpl = env.from_string(MATH)
assert tmpl.render() == '1.5|8'
-def test_div(env):
+def test_div():
tmpl = env.from_string(DIV)
assert tmpl.render() == '1|1.5|1'
-def test_unary(env):
+def test_unary():
tmpl = env.from_string(UNARY)
assert tmpl.render() == '3|-3'
-def test_concat(env):
+def test_concat():
tmpl = env.from_string(CONCAT)
assert tmpl.render() == '[1, 2]foo'
-def test_compare(env):
+def test_compare():
tmpl = env.from_string(COMPARE)
assert tmpl.render() == 'True|True|True|True|True'
-def test_inop(env):
+def test_inop():
tmpl = env.from_string(INOP)
assert tmpl.render() == 'True|False'
-def test_literals(env):
+def test_literals():
tmpl = env.from_string(LITERALS)
assert tmpl.render().lower() == '[]|{}|()'
-def test_bool(env):
+def test_bool():
tmpl = env.from_string(BOOL)
assert tmpl.render() == 'False|True|True'
-def test_grouping(env):
+def test_grouping():
tmpl = env.from_string(GROUPING)
assert tmpl.render() == 'False'
-def test_django_attr(env):
+def test_django_attr():
tmpl = env.from_string(DJANGOATTR)
assert tmpl.render() == '1|1'
-def test_conditional_expression(env):
+def test_conditional_expression():
tmpl = env.from_string(CONDEXPR)
assert tmpl.render() == '0'
-def test_short_conditional_expression(env):
+def test_short_conditional_expression():
tmpl = env.from_string('<{{ 1 if false }}>')
assert tmpl.render() == '<>'
tmpl = env.from_string('<{{ (1 if false).bar }}>')
- raises(UndefinedError, tmpl.render)
+ assert_raises(UndefinedError, tmpl.render)
-def test_filter_priority(env):
+def test_filter_priority():
tmpl = env.from_string(FILTERPRIORITY)
assert tmpl.render() == 'FOOBAR'
-def test_function_calls(env):
+def test_function_calls():
tests = [
(True, '*foo, bar'),
(True, '*foo, *bar'),
]
for should_fail, sig in tests:
if should_fail:
- raises(TemplateSyntaxError, env.from_string, '{{ foo(%s) }}' % sig)
+ assert_raises(TemplateSyntaxError, env.from_string, '{{ foo(%s) }}' % sig)
else:
env.from_string('foo(%s)' % sig)
-def test_tuple_expr(env):
+def test_tuple_expr():
for tmpl in TUPLETEMPLATES:
print tmpl
assert env.from_string(tmpl)
-def test_trailing_comma(env):
+def test_trailing_comma():
tmpl = env.from_string(TRAILINGCOMMA)
assert tmpl.render().lower() == '(1, 2)|[1, 2]|{1: 2}'
-def test_block_end_name(env):
+def test_block_end_name():
env.from_string('{% block foo %}...{% endblock foo %}')
- raises(TemplateSyntaxError, env.from_string, '{% block x %}{% endblock y %}')
+ assert_raises(TemplateSyntaxError, env.from_string, '{% block x %}{% endblock y %}')
-def test_contant_casing(env):
+def test_contant_casing():
for const in True, False, None:
tmpl = env.from_string('{{ %s }}|{{ %s }}|{{ %s }}' % (
str(const), str(const).lower(), str(const).upper()
assert tmpl.render() == '%s|%s|' % (const, const)
-def test_test_chaining(env):
- raises(TemplateSyntaxError, env.from_string, '{{ foo is string is sequence }}')
+def test_test_chaining():
+ assert_raises(TemplateSyntaxError, env.from_string, '{{ foo is string is sequence }}')
env.from_string('{{ 42 is string or 42 is number }}').render() == 'True'
-def test_string_concatenation(env):
+def test_string_concatenation():
tmpl = env.from_string('{{ "foo" "bar" "baz" }}')
assert tmpl.render() == 'foobarbaz'
-def test_notin(env):
+def test_notin():
bar = xrange(100)
tmpl = env.from_string('''{{ not 42 in bar }}''')
assert tmpl.render(bar=bar) == unicode(not 42 in bar)
-def test_implicit_subscribed_tuple(env):
+def test_implicit_subscribed_tuple():
class Foo(object):
def __getitem__(self, x):
return x
:license: BSD, see LICENSE for more details.
"""
from jinja2 import Environment, Markup
-
-import conftest
+env = Environment()
DEFINED = '''{{ missing is defined }}|{{ true is defined }}'''
{{ range(5) is iterable }}'''
-def test_defined(env):
+def test_defined():
tmpl = env.from_string(DEFINED)
assert tmpl.render() == 'False|True'
-def test_even(env):
+def test_even():
tmpl = env.from_string(EVEN)
assert tmpl.render() == 'False|True'
-def test_odd(env):
+def test_odd():
tmpl = env.from_string(ODD)
assert tmpl.render() == 'True|False'
-def test_lower(env):
+def test_lower():
tmpl = env.from_string(LOWER)
assert tmpl.render() == 'True|False'
-def test_typechecks(env):
+def test_typechecks():
tmpl = env.from_string(TYPECHECKS)
assert tmpl.render() == ''
-def test_sequence(env):
+def test_sequence():
tmpl = env.from_string(SEQUENCE)
assert tmpl.render() == 'True|True|False'
-def test_upper(env):
+def test_upper():
tmpl = env.from_string(UPPER)
assert tmpl.render() == 'True|False'
-def test_sameas(env):
+def test_sameas():
tmpl = env.from_string(SAMEAS)
assert tmpl.render(foo=False) == 'True|False'
-def test_typechecks(env):
+def test_typechecks():
tmpl = env.from_string(TYPECHECKS)
assert tmpl.render() == (
'False\nTrue\nFalse\nTrue\nTrue\nFalse\n'
)
-def test_no_paren_for_arg1(env):
+def test_no_paren_for_arg1():
tmpl = env.from_string(NOPARENFORARG1)
assert tmpl.render(foo=None) == 'True'
from jinja2 import Template
from jinja2.exceptions import UndefinedError
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+from nose.tools import assert_raises
def test_default_undefined():
def test_indexing_gives_undefined():
t = Template("{{ var[42].foo }}")
- raises(UndefinedError, t.render, var=0)
+ assert_raises(UndefinedError, t.render, var=0)
:license: BSD, see LICENSE for more details.
"""
import gc
-from jinja2 import escape, is_undefined
+from jinja2 import escape, is_undefined, Environment
from jinja2.utils import Cycler
from jinja2.exceptions import TemplateSyntaxError
-import conftest
-if conftest.NOSE:
- from nose.tools import assert_raises as raises
-else:
- from py.test import raises
+from nose.tools import assert_raises
+
+env = Environment()
UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''
CONSTASS2 = '''{% for none in seq %}{% endfor %}'''
-def test_unpacking(env):
+def test_unpacking():
tmpl = env.from_string(UNPACKING)
assert tmpl.render() == '1|2|3'
-def test_raw(env):
+def test_raw():
tmpl = env.from_string(RAW)
assert tmpl.render() == '{{ FOO }} and {% BAR %}'
-def test_const(env):
+def test_const():
tmpl = env.from_string(CONST)
assert tmpl.render() == 'True|False|None|True|False'
-def test_const_assign(env):
+def test_const_assign():
for tmpl in CONSTASS1, CONSTASS2:
- raises(TemplateSyntaxError, env.from_string, tmpl)
+ assert_raises(TemplateSyntaxError, env.from_string, tmpl)
-def test_localset(env):
+def test_localset():
tmpl = env.from_string(LOCALSET)
assert tmpl.render() == '0'
def test_item_and_attribute():
- from jinja2 import Environment
from jinja2.sandbox import SandboxedEnvironment
for env in Environment(), SandboxedEnvironment():
def test_finalizer():
- from jinja2 import Environment
def finalize_none_empty(value):
if value is None:
value = u''
assert c.current == 1
-def test_expressions(env):
+def test_expressions():
expr = env.compile_expression("foo")
assert expr() is None
assert expr(foo=42) == 42