From: Armin Ronacher Date: Sun, 26 Jul 2009 09:08:50 +0000 (+0200) Subject: Got rid of py.testisms. X-Git-Tag: 2.2~13 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=42979eb9ffb82056b96ad2de78513e2bb97608f2;p=jinja2.git Got rid of py.testisms. --HG-- branch : trunk --- diff --git a/fabfile.py b/fabfile.py deleted file mode 100644 index 4c16cd0..0000000 --- a/fabfile.py +++ /dev/null @@ -1,40 +0,0 @@ -# -*- 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') diff --git a/tests/conftest.py b/tests/conftest.py deleted file mode 100644 index dc38a95..0000000 --- a/tests/conftest.py +++ /dev/null @@ -1,163 +0,0 @@ -# -*- 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) diff --git a/tests/test_debug.py b/tests/test_debug.py index 49c66bb..d8e8a44 100644 --- a/tests/test_debug.py +++ b/tests/test_debug.py @@ -11,18 +11,12 @@ 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 @@ -35,7 +29,7 @@ ZeroDivisionError: integer division or modulo by zero 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' diff --git a/tests/test_filters.py b/tests/test_filters.py index aa9e698..19902ff 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -8,7 +8,7 @@ """ from jinja2 import Markup, Environment -import conftest +env = Environment() CAPITALIZE = '''{{ "foo bar"|capitalize }}''' @@ -72,22 +72,22 @@ FILTERTAG = '''{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}''' 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)]|" @@ -95,27 +95,27 @@ def test_dictsort(env): "[('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='

just a small \n ' 'example link

\n

to a webpage

' @@ -123,7 +123,7 @@ def test_striptags(env): 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 == ( @@ -132,25 +132,25 @@ def test_filesizeformat(env): ) -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) @@ -158,13 +158,13 @@ def test_indent(env): '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' @@ -174,107 +174,102 @@ def test_join(env): assert tmpl.render() == '<foo>foo' -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 '\ 'http://www.example.com/ 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 @@ -283,12 +278,12 @@ def test_xmlattr(env): 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}", @@ -297,7 +292,7 @@ def test_groupby(env): ] -def test_filtertag(env): +def test_filtertag(): tmpl = env.from_string(FILTERTAG) assert tmpl.render() == 'fooBAR' @@ -316,7 +311,7 @@ def test_replace(): 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('
')) == u'<div />' @@ -329,5 +324,5 @@ def test_safe(): assert tmpl.render() == '<div>foo</div>' -def test_sort2(env): +def test_sort2(): assert env.from_string(SORT2).render() == "['Bar', 'blah', 'foo']" diff --git a/tests/test_forloop.py b/tests/test_forloop.py index 0c93484..dd66ca0 100644 --- a/tests/test_forloop.py +++ b/tests/test_forloop.py @@ -6,13 +6,12 @@ :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 %}''' @@ -49,22 +48,22 @@ LOOPUNASSIGNABLE = '''\ {% 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, @@ -81,19 +80,19 @@ def test_context_vars(env): 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 @@ -102,12 +101,12 @@ def test_varlen(env): 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)]), @@ -116,42 +115,42 @@ def test_recursive(env): ]) == '[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' @@ -160,14 +159,14 @@ def test_scoped_loop_var(env): 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() }}] @@ -182,7 +181,7 @@ def test_call_in_loop(env): 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 %} diff --git a/tests/test_heavy.py b/tests/test_heavy.py index 4951dec..b045fbb 100644 --- a/tests/test_heavy.py +++ b/tests/test_heavy.py @@ -10,11 +10,11 @@ :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 }}] @@ -42,7 +42,7 @@ def test_assigned_scoping(env): assert t.render() == '[1][2][3][4]42' -def test_closure_scoping(env): +def test_closure_scoping(): t = env.from_string(''' {%- set wrapper = "" %} {%- for item in (1, 2, 3, 4) %} diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 109072a..ab967b9 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -9,11 +9,7 @@ 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': '{{ page_title|default(_("missing")) }}' @@ -77,8 +73,8 @@ def test_complex_plural(): 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(): diff --git a/tests/test_ifcondition.py b/tests/test_ifcondition.py index 82c91f3..52bbaec 100644 --- a/tests/test_ifcondition.py +++ b/tests/test_ifcondition.py @@ -7,7 +7,8 @@ :license: BSD, see LICENSE for more details. """ -import conftest +from jinja2 import Environment +env = Environment() SIMPLE = '''{% if true %}...{% endif %}''' @@ -16,33 +17,33 @@ ELSE = '''{% if false %}XXX{% else %}...{% 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 }}') diff --git a/tests/test_imports.py b/tests/test_imports.py index b865f6d..acee8f1 100644 --- a/tests/test_imports.py +++ b/tests/test_imports.py @@ -9,11 +9,7 @@ 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( @@ -50,7 +46,7 @@ def test_context_include(): 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 + ' %}') diff --git a/tests/test_inheritance.py b/tests/test_inheritance.py index 19c3fc2..58b992f 100644 --- a/tests/test_inheritance.py +++ b/tests/test_inheritance.py @@ -10,9 +10,6 @@ from jinja2 import Environment, DictLoader from jinja2.exceptions import TemplateSyntaxError -import conftest -conftest.GlobalLoader.scope = globals() - LAYOUTTEMPLATE = '''\ |{% block block1 %}block 1 from layout{% endblock %} @@ -52,31 +49,40 @@ WORKINGTEMPLATE = '''\ {% 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|') @@ -96,11 +102,11 @@ def test_super(): 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' diff --git a/tests/test_lexer.py b/tests/test_lexer.py index abaa08f..1b12eb4 100644 --- a/tests/test_lexer.py +++ b/tests/test_lexer.py @@ -6,8 +6,9 @@ :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 %}' @@ -21,7 +22,7 @@ COMMENTS = '''\ 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 %}' @@ -41,19 +42,19 @@ def test_comments(): "
  • 1
  • \n
  • 2
  • \n") -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 '([{}])': diff --git a/tests/test_loaders.py b/tests/test_loaders.py index 9149d46..17e6bf1 100644 --- a/tests/test_loaders.py +++ b/tests/test_loaders.py @@ -13,11 +13,7 @@ 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 +from nose.tools import assert_raises dict_loader = loaders.DictLoader({ @@ -37,14 +33,14 @@ def test_dict_loader(): 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(): @@ -53,7 +49,7 @@ 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(): @@ -62,14 +58,14 @@ 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(): @@ -78,7 +74,7 @@ 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(): @@ -112,4 +108,4 @@ 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') diff --git a/tests/test_macros.py b/tests/test_macros.py index 8b3bdb9..4fd7202 100644 --- a/tests/test_macros.py +++ b/tests/test_macros.py @@ -7,8 +7,9 @@ :license: BSD, see LICENSE for more details. """ -import conftest -conftest.GlobalLoader.scope = globals() +from jinja2 import Environment, DictLoader + +env = Environment(trim_blocks=True) SIMPLE = '''\ @@ -52,47 +53,48 @@ CALLERUNDEFINED = '''\ 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 %}') diff --git a/tests/test_old_bugs.py b/tests/test_old_bugs.py index 7815bb7..b004a29 100644 --- a/tests/test_old_bugs.py +++ b/tests/test_old_bugs.py @@ -10,11 +10,9 @@ """ 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(): @@ -36,12 +34,12 @@ def test_extends_output_bugs(): 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/http://www.example.org/<foo' -def test_loop_call_loop(env): +def test_loop_call_loop(): tmpl = env.from_string(''' {% macro test() %} @@ -63,11 +61,11 @@ def test_loop_call_loop(env): 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' diff --git a/tests/test_parser.py b/tests/test_parser.py index 99a7e47..969bac2 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -8,7 +8,7 @@ """ from jinja2 import Environment -import conftest +env = Environment() PHP_SYNTAX = '''\ @@ -84,12 +84,12 @@ def test_comment_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' diff --git a/tests/test_security.py b/tests/test_security.py index 6b8a5db..9d910b6 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -13,13 +13,7 @@ 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 +from nose.tools import assert_raises class PrivateStuff(object): @@ -44,19 +38,19 @@ class PublicStuff(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: 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) @@ -72,15 +66,15 @@ SecurityError: access to attribute '__class__' of 'int' object is unsafe. 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): ... @@ -144,4 +138,4 @@ def test_template_data(): def test_attr_filter(): env = SandboxedEnvironment() tmpl = env.from_string('{{ 42|attr("__class__")|attr("__subclasses__")() }}') - raises(SecurityError, tmpl.render) + assert_raises(SecurityError, tmpl.render) diff --git a/tests/test_streaming.py b/tests/test_streaming.py index fd837ce..8b4e36f 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -7,9 +7,8 @@ :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(): diff --git a/tests/test_syntax.py b/tests/test_syntax.py index 2e8877a..759af6a 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -6,14 +6,12 @@ :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'}) }}''' @@ -47,102 +45,101 @@ TRAILINGCOMMA = '''{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}''' 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'), @@ -157,28 +154,28 @@ def test_function_calls(env): ] 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() @@ -186,23 +183,23 @@ def test_contant_casing(env): 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 diff --git a/tests/test_tests.py b/tests/test_tests.py index e3e6f0f..2312889 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -7,8 +7,7 @@ :license: BSD, see LICENSE for more details. """ from jinja2 import Environment, Markup - -import conftest +env = Environment() DEFINED = '''{{ missing is defined }}|{{ true is defined }}''' @@ -36,47 +35,47 @@ TYPECHECKS = '''\ {{ 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' @@ -84,7 +83,7 @@ def test_typechecks(env): ) -def test_no_paren_for_arg1(env): +def test_no_paren_for_arg1(): tmpl = env.from_string(NOPARENFORARG1) assert tmpl.render(foo=None) == 'True' diff --git a/tests/test_undefined.py b/tests/test_undefined.py index be829bf..d9e3c93 100644 --- a/tests/test_undefined.py +++ b/tests/test_undefined.py @@ -9,11 +9,7 @@ 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(): @@ -87,4 +83,4 @@ UndefinedError: 'missing' is undefined def test_indexing_gives_undefined(): t = Template("{{ var[42].foo }}") - raises(UndefinedError, t.render, var=0) + assert_raises(UndefinedError, t.render, var=0) diff --git a/tests/test_various.py b/tests/test_various.py index 511b157..92ac6b8 100644 --- a/tests/test_various.py +++ b/tests/test_various.py @@ -7,15 +7,13 @@ :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 %}''' @@ -29,27 +27,27 @@ CONSTASS1 = '''{% set true = 42 %}''' 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' @@ -67,7 +65,6 @@ def test_markup_leaks(): def test_item_and_attribute(): - from jinja2 import Environment from jinja2.sandbox import SandboxedEnvironment for env in Environment(), SandboxedEnvironment(): @@ -80,7 +77,6 @@ def test_item_and_attribute(): def test_finalizer(): - from jinja2 import Environment def finalize_none_empty(value): if value is None: value = u'' @@ -104,7 +100,7 @@ def test_cycler(): 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