1 # -*- coding: utf-8 -*-
3 unit test for the test functions
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 :copyright: (c) 2009 by the Jinja Team.
7 :license: BSD, see LICENSE for more details.
9 from jinja2 import Environment, Markup
13 DEFINED = '''{{ missing is defined }}|{{ true is defined }}'''
14 EVEN = '''{{ 1 is even }}|{{ 2 is even }}'''
15 LOWER = '''{{ "foo" is lower }}|{{ "FOO" is lower }}'''
16 ODD = '''{{ 1 is odd }}|{{ 2 is odd }}'''
17 SEQUENCE = '''{{ [1, 2, 3] is sequence }}|\
18 {{ "foo" is sequence }}|\
19 {{ 42 is sequence }}'''
20 UPPER = '''{{ "FOO" is upper }}|{{ "foo" is upper }}'''
21 SAMEAS = '''{{ foo is sameas false }}|{{ 0 is sameas false }}'''
22 NOPARENFORARG1 = '''{{ foo is sameas none }}'''
31 {{ "foo" is sequence }}
33 {{ range is callable }}
35 {{ range(5) is iterable }}'''
39 tmpl = env.from_string(DEFINED)
40 assert tmpl.render() == 'False|True'
44 tmpl = env.from_string(EVEN)
45 assert tmpl.render() == 'False|True'
49 tmpl = env.from_string(ODD)
50 assert tmpl.render() == 'True|False'
54 tmpl = env.from_string(LOWER)
55 assert tmpl.render() == 'True|False'
58 def test_typechecks():
59 tmpl = env.from_string(TYPECHECKS)
60 assert tmpl.render() == ''
64 tmpl = env.from_string(SEQUENCE)
65 assert tmpl.render() == 'True|True|False'
69 tmpl = env.from_string(UPPER)
70 assert tmpl.render() == 'True|False'
74 tmpl = env.from_string(SAMEAS)
75 assert tmpl.render(foo=False) == 'True|False'
78 def test_typechecks():
79 tmpl = env.from_string(TYPECHECKS)
80 assert tmpl.render() == (
81 'False\nTrue\nFalse\nTrue\nTrue\nFalse\n'
82 'True\nTrue\nTrue\nTrue\nFalse\nTrue'
86 def test_no_paren_for_arg1():
87 tmpl = env.from_string(NOPARENFORARG1)
88 assert tmpl.render(foo=None) == 'True'
92 env = Environment(autoescape=True)
93 tmpl = env.from_string('{{ x is escaped }}|{{ y is escaped }}')
94 assert tmpl.render(x='foo', y=Markup('foo')) == 'False|True'