test_ext.py
This file
Path
tests/test_ext.py
160 lines · 4.6 KiB · Python
Last changed by
Fixed a bug that caused internal errors if names where used as iterationArmin Ronacherbefore adoption271a0eb
History of this fileTake it
added loopcontrols extension and added unittests for itArmin Ronacherbefore adoption3da9031
lines 1–5why, one step back1# -*- coding: utf-8 -*-2"""3 unit test for some extensions4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
line 6why, one step back6 :copyright: (c) 2009 by the Jinja Team.added loopcontrols extension and added unittests for itArmin Ronacherbefore adoption3da9031
lines 7–8why, one step back7 :license: BSD, see LICENSE for more details.8"""Added unittests for stream filtering and preprocessing features of extensions.Armin Ronacherbefore adoption3e3a9be
line 9why, one step back9import remore unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e
line 10why, one step back10from jinja2 import Environment, nodesAdded example extension that uses the stream filtering and added unittests.Armin Ronacherbefore adoptiond02fc7d
lines 11–12why, one step back11from jinja2.ext import Extension12from jinja2.lexer import Token, count_newlinesadded loopcontrols extension and added unittests for itArmin Ronacherbefore adoption3da9031
lines 13–14why, one step back1314more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e
lines 15–17why, one step back15importable_object = 231617Added example extension that uses the stream filtering and added unittests.Armin Ronacherbefore adoptiond02fc7d
line 18why, one step back18_gettext_re = re.compile(r'_\((.*?)\)')Added unittests for stream filtering and preprocessing features of extensions.Armin Ronacherbefore adoption3e3a9be
lines 19–20why, one step back1920more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e
lines 21–41why, one step back21class TestExtension(Extension):22 tags = set(['test'])23 ext_attr = 422425 def parse(self, parser):26 return nodes.Output([self.call_method('_dump', [27 nodes.EnvironmentAttribute('sandboxed'),28 self.attr('ext_attr'),29 nodes.ImportedName(__name__ + '.importable_object'),30 nodes.ContextReference()31 ])]).set_lineno(parser.stream.next().lineno)3233 def _dump(self, sandboxed, ext_attr, imported_object, context):34 return '%s|%s|%s|%s' % (35 sandboxed,36 ext_attr,37 imported_object,38 context.blocks39 )4041Added unittests for stream filtering and preprocessing features of extensions.Armin Ronacherbefore adoption3e3a9be
lines 42–68why, one step back42class PreprocessorExtension(Extension):4344 def preprocess(self, source, name, filename=None):45 return source.replace('[[TEST]]', '({{ foo }})')464748class StreamFilterExtension(Extension):4950 def filter_stream(self, stream):51 for token in stream:52 if token.type is 'data':53 for t in self.interpolate(token):54 yield t55 else:56 yield token5758 def interpolate(self, token):59 pos = 060 end = len(token.value)61 lineno = token.lineno62 while 1:63 match = _gettext_re.search(token.value, pos)64 if match is None:65 break66 value = token.value[pos:match.start()]67 if value:68 yield Token(lineno, 'data', value)Added example extension that uses the stream filtering and added unittests.Armin Ronacherbefore adoptiond02fc7d
line 69why, one step back69 lineno += count_newlines(token.value)Added unittests for stream filtering and preprocessing features of extensions.Armin Ronacherbefore adoption3e3a9be
lines 70–80why, one step back70 yield Token(lineno, 'variable_begin', None)71 yield Token(lineno, 'name', 'gettext')72 yield Token(lineno, 'lparen', None)73 yield Token(lineno, 'string', match.group(1))74 yield Token(lineno, 'rparen', None)75 yield Token(lineno, 'variable_end', None)76 pos = match.end()77 if pos < end:78 yield Token(lineno, 'data', token.value[pos:])7980added loopcontrols extension and added unittests for itArmin Ronacherbefore adoption3da9031
lines 81–106why, one step back81def test_loop_controls():82 env = Environment(extensions=['jinja2.ext.loopcontrols'])8384 tmpl = env.from_string('''85 {%- for item in [1, 2, 3, 4] %}86 {%- if item % 2 == 0 %}{% continue %}{% endif -%}87 {{ item }}88 {%- endfor %}''')89 assert tmpl.render() == '13'9091 tmpl = env.from_string('''92 {%- for item in [1, 2, 3, 4] %}93 {%- if item > 2 %}{% break %}{% endif -%}94 {{ item }}95 {%- endfor %}''')96 assert tmpl.render() == '12'979899def test_do():100 env = Environment(extensions=['jinja2.ext.do'])101 tmpl = env.from_string('''102 {%- set items = [] %}103 {%- for char in "foo" %}104 {%- do items.append(loop.index0 ~ char) %}105 {%- endfor %}{{ items|join(', ') }}''')106 assert tmpl.render() == '0f, 1o, 2o'more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e
lines 107–112why, one step back107108109def test_extension_nodes():110 env = Environment(extensions=[TestExtension])111 tmpl = env.from_string('{% test %}')112 assert tmpl.render() == 'False|42|23|{}'fixed bug with static unicode strings and auto escapingArmin Ronacherbefore adoption9cf9591
lines 113–116why, one step back113114115def test_identifier():116 assert TestExtension.identifier == __name__ + '.TestExtension'added more unittests for various tasksArmin Ronacherbefore adoption9f258ff
lines 117–124why, one step back117118119def test_rebinding():120 original = Environment(extensions=[TestExtension])121 overlay = original.overlay()122 for env in original, overlay:123 for ext in env.extensions.itervalues():124 assert ext.environment is envAdded unittests for stream filtering and preprocessing features of extensions.Armin Ronacherbefore adoption3e3a9be
lines 125–137why, one step back125126127def test_preprocessor_extension():128 env = Environment(extensions=[PreprocessorExtension])129 tmpl = env.from_string('{[[TEST]]}')130 assert tmpl.render(foo=42) == '{(42)}'131132133def test_streamfilter_extension():134 env = Environment(extensions=[StreamFilterExtension])135 env.globals['gettext'] = lambda x: x.title()136 tmpl = env.from_string('Foo _(bar) Baz')137 assert tmpl.render() == 'Foo Bar Baz'Fixed a bug that caused internal errors if names where used as iterationArmin Ronacherbefore adoption271a0eb
lines 138–160why, one step back138139140class WithExtension(Extension):141 tags = frozenset(['with'])142143 def parse(self, parser):144 lineno = parser.stream.next().lineno145 value = parser.parse_expression()146 parser.stream.expect('name:as')147 name = parser.stream.expect('name')148149 body = parser.parse_statements(['name:endwith'], drop_needle=True)150 body.insert(0, nodes.Assign(nodes.Name(name.value, 'store'), value))151 return nodes.Scope(body)152153154def test_with_extension():155 env = Environment(extensions=[WithExtension])156 t = env.from_string('{{ a }}{% with 2 as a %}{{ a }}{% endwith %}{{ a }}')157 assert t.render(a=1) == '121'158159 t = env.from_string('{% with 2 as a %}{{ a }}{% endwith %}{{ a }}')160 assert t.render(a=3) == '23'