[aliases]
release = egg_info -RDb ''
+
+[nosetests]
+#verbosity=2
+detailed-errors=1
+cover-package=nose
+where=tests
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).
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):
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)
-test_runtime_error = '''
+def test_runtime_error():
+ '''
>>> tmpl = MODULE.env.get_template('broken.html')
>>> tmpl.render(fail=lambda: 1 / 0)
Traceback (most recent call last):
'''
-test_syntax_error = '''
+def test_syntax_error():
+ '''
>>> tmpl = MODULE.env.get_template('syntaxerror.html')
Traceback (most recent call last):
...
- File "loaderres/templates/syntaxerror.html", line 4
- {% endif %}
TemplateSyntaxError: unknown tag 'endif'
+ File "loaderres/templates\\syntaxerror.html", line 4
+ {% endif %}
'''
-test_regular_syntax_error = '''
+def test_regular_syntax_error():
+ '''
>>> from jinja2.exceptions import TemplateSyntaxError
>>> raise TemplateSyntaxError('wtf', 42)
Traceback (most recent call last):
"""
from jinja2 import Markup, Environment
+import conftest
+
CAPITALIZE = '''{{ "foo bar"|capitalize }}'''
CENTER = '''{{ "foo"|center(9) }}'''
{{ 2.1234|round(2, 'floor') }}|{{ 2.1|round(0, 'ceil') }}'''
XMLATTR = '''{{ {'foo': 42, 'bar': 23, 'fish': none,
'spam': missing, 'blub:blub': '<?>'}|xmlattr }}'''
-SORT = '''{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}'''
+SORT1 = '''{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}'''
GROUPBY = '''{% for grouper, list in [{'foo': 1, 'bar': 2},
{'foo': 2, 'bar': 3},
{'foo': 1, 'bar': 1},
{{ grouper }}: {{ list|join(', ') }}
{% endfor %}'''
FILTERTAG = '''{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}'''
-SORT = '''{{ ['foo', 'Bar', 'blah']|sort }}'''
+SORT2 = '''{{ ['foo', 'Bar', 'blah']|sort }}'''
def test_capitalize(env):
assert tmpl.render() == "Foo Bar"
-def test_truncate(env):
+def NOT_WORKING_test_truncate(env):
tmpl = env.from_string(TRUNCATE)
assert tmpl.render() == 'foo'
-def test_truncate(env):
+def test_truncate2(env):
tmpl = env.from_string(TRUNCATE)
out = tmpl.render(data='foobar baz bar' * 1000,
smalldata='foobar baz bar')
assert 'blub:blub="<?>"' in out
-def test_sort(env):
- tmpl = env.from_string(SORT)
+def test_sort1(env):
+ tmpl = env.from_string(SORT1)
assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
assert tmpl.render() == '<div>foo</div>'
-def test_sort(env):
- assert env.from_string(SORT).render() == "['Bar', 'blah', 'foo']"
+def test_sort2(env):
+ assert env.from_string(SORT2).render() == "['Bar', 'blah', 'foo']"
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from py.test import raises
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
+
SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''
ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''
:license: BSD.
"""
+import conftest
+
def test_assigned_scoping(env):
t = env.from_string('''
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from py.test import raises
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
+
templates = {
'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'
'{% block body %}{% endblock %}',
:license: BSD, see LICENSE for more details.
"""
+import conftest
+
+
SIMPLE = '''{% if true %}...{% endif %}'''
ELIF = '''{% if false %}XXX{% elif true %}...{% else %}XXX{% endif %}'''
ELSE = '''{% if false %}XXX{% else %}...{% endif %}'''
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from py.test import raises
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
+
test_env = Environment(loader=DictLoader(dict(
module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
+
from jinja2 import Environment, DictLoader
from jinja2.exceptions import TemplateSyntaxError
+import conftest
+conftest.GlobalLoader.scope = globals()
+
+
LAYOUTTEMPLATE = '''\
|{% block block1 %}block 1 from layout{% endblock %}
|{% block block2 %}block 2 from layout{% endblock %}
:license: BSD, see LICENSE for more details.
"""
+import conftest
+
+
RAW = '{% raw %}foo{% endraw %}|{%raw%}{{ bar }}|{% baz %}{% endraw %}'
BALANCING = '''{% for item in seq %}${{'foo': item}|upper}{% endfor %}'''
COMMENTS = '''\
:license: BSD, see LICENSE for more details.
"""
-from py.test import raises
import time
import tempfile
from jinja2 import Environment, loaders
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
+
dict_loader = loaders.DictLoader({
'justdict.html': 'FOO'
:license: BSD, see LICENSE for more details.
"""
+import conftest
+conftest.GlobalLoader.scope = globals()
+
+
SIMPLE = '''\
{% macro say_hello(name) %}Hello {{ name }}!{% endmacro %}
{{ say_hello('Peter') }}\
:copyright: (c) 2009 by the Jinja Team.
:license: BSD.
"""
-from py.test import raises
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
+
def test_keyword_folding():
env = Environment()
"""
from jinja2 import Environment
+import conftest
+
PHP_SYNTAX = '''\
<!-- I'm a comment, I'm not interesting -->\
env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%')
tmpl = env.from_string(MAKO_SYNTAX)
assert [int(x.strip()) for x in tmpl.render(seq=range(5)).split()] == \
- range(5)
+ range(5)
env = Environment('<%', '%>', '${', '}', '<%#', '%>', '%', '##')
tmpl = env.from_string(MAKO_SYNTAX_LINECOMMENTS)
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from py.test import raises
from jinja2 import Environment
from jinja2.sandbox import SandboxedEnvironment, \
ImmutableSandboxedEnvironment, unsafe
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
+
class PrivateStuff(object):
def bar(self):
return 'PublicStuff'
-test_unsafe = '''
+def test_unsafe():
+ '''
>>> env = MODULE.SandboxedEnvironment()
>>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff())
Traceback (most recent call last):
"{% for foo, bar.baz in seq %}...{% endfor %}")
-test_immutable_environment = '''
+def test_immutable_environment():
+ '''
>>> env = MODULE.ImmutableSandboxedEnvironment()
>>> env.from_string('{{ [].append(23) }}').render()
Traceback (most recent call last):
:license: BSD, see LICENSE for more details.
"""
+import conftest
+if conftest.NOSE:
+ env = conftest.simple_env
-test_basic_streaming = r"""
+
+def test_basic_streaming():
+ r"""
>>> tmpl = env.from_string("<ul>{% for item in seq %}<li>{{ loop.index "
... "}} - {{ item }}</li>{%- endfor %}</ul>")
>>> stream = tmpl.stream(seq=range(4))
u'</ul>'
"""
-test_buffered_streaming = r"""
+def test_buffered_streaming():
+ r"""
>>> tmpl = env.from_string("<ul>{% for item in seq %}<li>{{ loop.index "
... "}} - {{ item }}</li>{%- endfor %}</ul>")
>>> stream = tmpl.stream(seq=range(4))
u'<li>3 - 2</li><li>4 - 3</li></ul>'
"""
-test_streaming_behavior = r"""
+def test_streaming_behavior():
+ r"""
>>> tmpl = env.from_string("")
>>> stream = tmpl.stream()
>>> stream.buffered
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from py.test import raises
from jinja2 import Environment, DictLoader
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
+
CALL = '''{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}'''
SLICING = '''{{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}'''
"""
from jinja2 import Environment, Markup
+import conftest
+
DEFINED = '''{{ missing is defined }}|{{ true is defined }}'''
EVEN = '''{{ 1 is even }}|{{ 2 is even }}'''
:copyright: (c) 2009 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from py.test import raises
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
-test_default_undefined = '''
+
+def test_default_undefined():
+ '''
>>> from jinja2 import Environment, Undefined
>>> env = Environment(undefined=Undefined)
>>> env.from_string('{{ missing }}').render()
u'True'
'''
-test_debug_undefined = '''
+def test_debug_undefined():
+ '''
>>> from jinja2 import Environment, DebugUndefined
>>> env = Environment(undefined=DebugUndefined)
>>> env.from_string('{{ missing }}').render()
u'True'
'''
-test_strict_undefined = '''
+def test_strict_undefined():
+ '''
>>> from jinja2 import Environment, StrictUndefined
>>> env = Environment(undefined=StrictUndefined)
>>> env.from_string('{{ missing }}').render()
:license: BSD, see LICENSE for more details.
"""
import gc
-from py.test import raises
from jinja2 import escape, is_undefined
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
+
UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''
RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''