From c7e6c6d2e573804709ba4c01433860f785a865c7 Mon Sep 17 00:00:00 2001 From: Rene Leonhardt Date: Mon, 20 Apr 2009 23:08:53 +0200 Subject: [PATCH] Support nose framework for the whole test suite. --HG-- branch : trunk --- setup.cfg | 6 ++++++ tests/conftest.py | 14 ++++++++++++++ tests/test_debug.py | 18 +++++++++++++----- tests/test_filters.py | 18 ++++++++++-------- tests/test_forloop.py | 7 ++++++- tests/test_heavy.py | 2 ++ tests/test_i18n.py | 7 ++++++- tests/test_ifcondition.py | 3 +++ tests/test_imports.py | 7 ++++++- tests/test_inheritance.py | 5 +++++ tests/test_lexer.py | 3 +++ tests/test_loaders.py | 7 ++++++- tests/test_macros.py | 4 ++++ tests/test_old_bugs.py | 7 ++++++- tests/test_parser.py | 4 +++- tests/test_security.py | 15 ++++++++++++--- tests/test_streaming.py | 13 ++++++++++--- tests/test_syntax.py | 7 ++++++- tests/test_tests.py | 2 ++ tests/test_undefined.py | 16 ++++++++++++---- tests/test_various.py | 7 ++++++- 21 files changed, 141 insertions(+), 31 deletions(-) diff --git a/setup.cfg b/setup.cfg index 2d74c58..f739d86 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,3 +4,9 @@ tag_date = true [aliases] release = egg_info -RDb '' + +[nosetests] +#verbosity=2 +detailed-errors=1 +cover-package=nose +where=tests diff --git a/tests/conftest.py b/tests/conftest.py index 453fbd7..dc38a95 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,19 @@ 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). @@ -55,6 +68,7 @@ except ImportError: 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): diff --git a/tests/test_debug.py b/tests/test_debug.py index aadb9a4..49c66bb 100644 --- a/tests/test_debug.py +++ b/tests/test_debug.py @@ -11,11 +11,17 @@ 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): @@ -27,17 +33,19 @@ ZeroDivisionError: integer division or modulo by zero ''' -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): diff --git a/tests/test_filters.py b/tests/test_filters.py index 5e45afe..aa9e698 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -8,6 +8,8 @@ """ from jinja2 import Markup, Environment +import conftest + CAPITALIZE = '''{{ "foo bar"|capitalize }}''' CENTER = '''{{ "foo"|center(9) }}''' @@ -59,7 +61,7 @@ ROUND = '''{{ 2.7|round }}|{{ 2.1|round }}|\ {{ 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}, @@ -67,7 +69,7 @@ GROUPBY = '''{% for grouper, list in [{'foo': 1, 'bar': 2}, {{ 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): @@ -219,12 +221,12 @@ def test_title(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') @@ -281,8 +283,8 @@ def test_xmlattr(env): 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]' @@ -327,5 +329,5 @@ def test_safe(): 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']" diff --git a/tests/test_forloop.py b/tests/test_forloop.py index c5bac48..0c93484 100644 --- a/tests/test_forloop.py +++ b/tests/test_forloop.py @@ -6,9 +6,14 @@ :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 %}''' diff --git a/tests/test_heavy.py b/tests/test_heavy.py index b115cbd..4951dec 100644 --- a/tests/test_heavy.py +++ b/tests/test_heavy.py @@ -11,6 +11,8 @@ :license: BSD. """ +import conftest + def test_assigned_scoping(env): t = env.from_string(''' diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 7e9fb49..109072a 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -6,10 +6,15 @@ :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': '{{ page_title|default(_("missing")) }}' '{% block body %}{% endblock %}', diff --git a/tests/test_ifcondition.py b/tests/test_ifcondition.py index eff49bf..82c91f3 100644 --- a/tests/test_ifcondition.py +++ b/tests/test_ifcondition.py @@ -7,6 +7,9 @@ :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 %}''' diff --git a/tests/test_imports.py b/tests/test_imports.py index f5b227f..b865f6d 100644 --- a/tests/test_imports.py +++ b/tests/test_imports.py @@ -6,10 +6,15 @@ :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 %}', diff --git a/tests/test_inheritance.py b/tests/test_inheritance.py index 13a9d8d..19c3fc2 100644 --- a/tests/test_inheritance.py +++ b/tests/test_inheritance.py @@ -6,9 +6,14 @@ :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 %} diff --git a/tests/test_lexer.py b/tests/test_lexer.py index b1631f0..abaa08f 100644 --- a/tests/test_lexer.py +++ b/tests/test_lexer.py @@ -7,6 +7,9 @@ :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 = '''\ diff --git a/tests/test_loaders.py b/tests/test_loaders.py index d8f7b82..9149d46 100644 --- a/tests/test_loaders.py +++ b/tests/test_loaders.py @@ -7,13 +7,18 @@ :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' diff --git a/tests/test_macros.py b/tests/test_macros.py index e2e7bc2..8b3bdb9 100644 --- a/tests/test_macros.py +++ b/tests/test_macros.py @@ -7,6 +7,10 @@ :license: BSD, see LICENSE for more details. """ +import conftest +conftest.GlobalLoader.scope = globals() + + SIMPLE = '''\ {% macro say_hello(name) %}Hello {{ name }}!{% endmacro %} {{ say_hello('Peter') }}\ diff --git a/tests/test_old_bugs.py b/tests/test_old_bugs.py index 59d6037..7815bb7 100644 --- a/tests/test_old_bugs.py +++ b/tests/test_old_bugs.py @@ -8,9 +8,14 @@ :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() diff --git a/tests/test_parser.py b/tests/test_parser.py index ca9506d..99a7e47 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -8,6 +8,8 @@ """ from jinja2 import Environment +import conftest + PHP_SYNTAX = '''\ \ @@ -96,7 +98,7 @@ def test_line_syntax(): 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) diff --git a/tests/test_security.py b/tests/test_security.py index 492a081..6b8a5db 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -6,7 +6,6 @@ :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 @@ -14,6 +13,14 @@ from jinja2 import Markup, escape 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): @@ -35,7 +42,8 @@ class PublicStuff(object): return 'PublicStuff' -test_unsafe = ''' +def test_unsafe(): + ''' >>> env = MODULE.SandboxedEnvironment() >>> env.from_string("{{ foo.foo() }}").render(foo=MODULE.PrivateStuff()) Traceback (most recent call last): @@ -70,7 +78,8 @@ def test_restricted(): "{% 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): diff --git a/tests/test_streaming.py b/tests/test_streaming.py index 8e44263..fd837ce 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -7,8 +7,13 @@ :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("") >>> stream = tmpl.stream(seq=range(4)) @@ -26,7 +31,8 @@ u'
  • 4 - 3
  • ' u'' """ -test_buffered_streaming = r""" +def test_buffered_streaming(): + r""" >>> tmpl = env.from_string("") >>> stream = tmpl.stream(seq=range(4)) @@ -37,7 +43,8 @@ u'' """ -test_streaming_behavior = r""" +def test_streaming_behavior(): + r""" >>> tmpl = env.from_string("") >>> stream = tmpl.stream() >>> stream.buffered diff --git a/tests/test_syntax.py b/tests/test_syntax.py index 5e9e804..2e8877a 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -6,10 +6,15 @@ :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] }}''' diff --git a/tests/test_tests.py b/tests/test_tests.py index e210b4b..e3e6f0f 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -8,6 +8,8 @@ """ from jinja2 import Environment, Markup +import conftest + DEFINED = '''{{ missing is defined }}|{{ true is defined }}''' EVEN = '''{{ 1 is even }}|{{ 2 is even }}''' diff --git a/tests/test_undefined.py b/tests/test_undefined.py index dc66d56..be829bf 100644 --- a/tests/test_undefined.py +++ b/tests/test_undefined.py @@ -6,12 +6,18 @@ :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() @@ -30,7 +36,8 @@ u'' u'True' ''' -test_debug_undefined = ''' +def test_debug_undefined(): + ''' >>> from jinja2 import Environment, DebugUndefined >>> env = Environment(undefined=DebugUndefined) >>> env.from_string('{{ missing }}').render() @@ -49,7 +56,8 @@ u"{{ no such element: int['missing'] }}" u'True' ''' -test_strict_undefined = ''' +def test_strict_undefined(): + ''' >>> from jinja2 import Environment, StrictUndefined >>> env = Environment(undefined=StrictUndefined) >>> env.from_string('{{ missing }}').render() diff --git a/tests/test_various.py b/tests/test_various.py index f0c997d..511b157 100644 --- a/tests/test_various.py +++ b/tests/test_various.py @@ -7,11 +7,16 @@ :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 %}''' -- 2.26.2