Skip to content
Myr

Fixed some tests, added syntax test.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent c608fb8 · Browse files · 1 file+152 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+152 added−2 removed

  • other:+152 added−2 removed

1 file+152 added−2 removed

modified:jinja2/testsuite/lexnparse.py+152 added-2 removed
13 unmodified lines
14import unittest1516from jinja2.testsuite import JinjaTestCase1718from jinja2 import Environment, Template, TemplateSyntaxError18from jinja2 import Environment, Template, TemplateSyntaxError, UndefinedError1920env = Environment()2122
157 unmodified lines
180 "and may not contain hypens, use an underscore instead.")181182183class SyntaxTestCase(JinjaTestCase):184 pass184185 def test_call(self):186 env = Environment()187 env.globals['foo'] = lambda a, b, c, e, g: a + b + c + e + g188 tmpl = env.from_string("{{ foo('a', c='d', e='f', *['b'], **{'g': 'h'}) }}")189 assert tmpl.render() == 'abdfh'190191 def test_slicing(self):192 tmpl = env.from_string('{{ [1, 2, 3][:] }}|{{ [1, 2, 3][::-1] }}')193 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'194195 def test_attr(self):196 tmpl = env.from_string("{{ foo.bar }}|{{ foo['bar'] }}")197 assert tmpl.render(foo={'bar': 42}) == '42|42'198199 def test_subscript(self):200 tmpl = env.from_string("{{ foo[0] }}|{{ foo[-1] }}")201 assert tmpl.render(foo=[0, 1, 2]) == '0|2'202203 def test_tuple(self):204 tmpl = env.from_string('{{ () }}|{{ (1,) }}|{{ (1, 2) }}')205 assert tmpl.render() == '()|(1,)|(1, 2)'206207 def test_math(self):208 tmpl = env.from_string('{{ (1 + 1 * 2) - 3 / 2 }}|{{ 2**3 }}')209 assert tmpl.render() == '1.5|8'210211 def test_div(self):212 tmpl = env.from_string('{{ 3 // 2 }}|{{ 3 / 2 }}|{{ 3 % 2 }}')213 assert tmpl.render() == '1|1.5|1'214215 def test_unary(self):216 tmpl = env.from_string('{{ +3 }}|{{ -3 }}')217 assert tmpl.render() == '3|-3'218219 def test_concat(self):220 tmpl = env.from_string("{{ [1, 2] ~ 'foo' }}")221 assert tmpl.render() == '[1, 2]foo'222223 def test_compare(self):224 tmpl = env.from_string('{{ 1 > 0 }}|{{ 1 >= 1 }}|{{ 2 < 3 }}|'225 '{{ 2 == 2 }}|{{ 1 <= 1 }}')226 assert tmpl.render() == 'True|True|True|True|True'227228 def test_inop(self):229 tmpl = env.from_string('{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}')230 assert tmpl.render() == 'True|False'231232 def test_literals(self):233 tmpl = env.from_string('{{ [] }}|{{ {} }}|{{ () }}')234 assert tmpl.render().lower() == '[]|{}|()'235236 def test_bool(self):237 tmpl = env.from_string('{{ true and false }}|{{ false '238 'or true }}|{{ not false }}')239 assert tmpl.render() == 'False|True|True'240241 def test_grouping(self):242 tmpl = env.from_string('{{ (true and false) or (false and true) and not false }}')243 assert tmpl.render() == 'False'244245 def test_django_attr(self):246 tmpl = env.from_string('{{ [1, 2, 3].0 }}|{{ [[1]].0.0 }}')247 assert tmpl.render() == '1|1'248249 def test_conditional_expression(self):250 tmpl = env.from_string('''{{ 0 if true else 1 }}''')251 assert tmpl.render() == '0'252253 def test_short_conditional_expression(self):254 tmpl = env.from_string('<{{ 1 if false }}>')255 assert tmpl.render() == '<>'256257 tmpl = env.from_string('<{{ (1 if false).bar }}>')258 self.assert_raises(UndefinedError, tmpl.render)259260 def test_filter_priority(self):261 tmpl = env.from_string('{{ "foo"|upper + "bar"|upper }}')262 assert tmpl.render() == 'FOOBAR'263264 def test_function_calls(self):265 tests = [266 (True, '*foo, bar'),267 (True, '*foo, *bar'),268 (True, '*foo, bar=42'),269 (True, '**foo, *bar'),270 (True, '**foo, bar'),271 (False, 'foo, bar'),272 (False, 'foo, bar=42'),273 (False, 'foo, bar=23, *args'),274 (False, 'a, b=c, *d, **e'),275 (False, '*foo, **bar')276 ]277 for should_fail, sig in tests:278 if should_fail:279 self.assert_raises(TemplateSyntaxError,280 env.from_string, '{{ foo(%s) }}' % sig)281 else:282 env.from_string('foo(%s)' % sig)283284285 def test_tuple_expr(self):286 for tmpl in [287 '{{ () }}',288 '{{ (1, 2) }}',289 '{{ (1, 2,) }}',290 '{{ 1, }}',291 '{{ 1, 2 }}',292 '{% for foo, bar in seq %}...{% endfor %}',293 '{% for x in foo, bar %}...{% endfor %}',294 '{% for x in foo, %}...{% endfor %}'295 ]:296 assert env.from_string(tmpl)297298 def test_trailing_comma(self):299 tmpl = env.from_string('{{ (1, 2,) }}|{{ [1, 2,] }}|{{ {1: 2,} }}')300 assert tmpl.render().lower() == '(1, 2)|[1, 2]|{1: 2}'301302 def test_block_end_name(self):303 env.from_string('{% block foo %}...{% endblock foo %}')304 self.assert_raises(TemplateSyntaxError, env.from_string,305 '{% block x %}{% endblock y %}')306307 def test_contant_casing(self):308 for const in True, False, None:309 tmpl = env.from_string('{{ %s }}|{{ %s }}|{{ %s }}' % (310 str(const), str(const).lower(), str(const).upper()311 ))312 assert tmpl.render() == '%s|%s|' % (const, const)313314 def test_test_chaining(self):315 self.assert_raises(TemplateSyntaxError, env.from_string,316 '{{ foo is string is sequence }}')317 env.from_string('{{ 42 is string or 42 is number }}'318 ).render() == 'True'319320 def test_string_concatenation(self):321 tmpl = env.from_string('{{ "foo" "bar" "baz" }}')322 assert tmpl.render() == 'foobarbaz'323324 def test_notin(self):325 bar = xrange(100)326 tmpl = env.from_string('''{{ not 42 in bar }}''')327 assert tmpl.render(bar=bar) == unicode(not 42 in bar)328329 def test_implicit_subscribed_tuple(self):330 class Foo(object):331 def __getitem__(self, x):332 return x333 t = env.from_string('{{ foo[1, 2] }}')334 assert t.render(foo=Foo()) == u'(1, 2)'335336337def suite():338 suite = unittest.TestSuite()339 suite.addTest(unittest.makeSuite(LexerTestCase))340 suite.addTest(unittest.makeSuite(ParserTestCase))
2 unmodified lines