blob: 7e38e69bb327a09be0176533c2b4df52d0ab802a [file] [log] [blame]
Armin Ronacher41d6f712010-02-09 16:43:12 +01001# -*- coding: utf-8 -*-
2"""
3 jinja2.testsuite.tests
4 ~~~~~~~~~~~~~~~~~~~~~~
5
6 Who tests the tests?
7
8 :copyright: (c) 2010 by the Jinja Team.
9 :license: BSD, see LICENSE for more details.
10"""
11import unittest
12from jinja2.testsuite import JinjaTestCase
13
14from jinja2 import Markup, Environment
15
16env = Environment()
17
18
19class TestsTestCase(JinjaTestCase):
20
21 def test_defined(self):
22 tmpl = env.from_string('{{ missing is defined }}|{{ true is defined }}')
23 assert tmpl.render() == 'False|True'
24
25 def test_even(self):
26 tmpl = env.from_string('''{{ 1 is even }}|{{ 2 is even }}''')
27 assert tmpl.render() == 'False|True'
28
29 def test_odd(self):
30 tmpl = env.from_string('''{{ 1 is odd }}|{{ 2 is odd }}''')
31 assert tmpl.render() == 'True|False'
32
33 def test_lower(self):
34 tmpl = env.from_string('''{{ "foo" is lower }}|{{ "FOO" is lower }}''')
35 assert tmpl.render() == 'True|False'
36
37 def test_typechecks(self):
38 tmpl = env.from_string('''
39 {{ 42 is undefined }}
40 {{ 42 is defined }}
41 {{ 42 is none }}
42 {{ none is none }}
43 {{ 42 is number }}
44 {{ 42 is string }}
45 {{ "foo" is string }}
46 {{ "foo" is sequence }}
47 {{ [1] is sequence }}
48 {{ range is callable }}
49 {{ 42 is callable }}
50 {{ range(5) is iterable }}
Armin Ronacheree352ec2011-05-24 16:40:23 +020051 {{ {} is mapping }}
52 {{ mydict is mapping }}
53 {{ [] is mapping }}
Armin Ronacher41d6f712010-02-09 16:43:12 +010054 ''')
Armin Ronacheree352ec2011-05-24 16:40:23 +020055 class MyDict(dict):
56 pass
57 assert tmpl.render(mydict=MyDict()).split() == [
Armin Ronacher41d6f712010-02-09 16:43:12 +010058 'False', 'True', 'False', 'True', 'True', 'False',
Armin Ronacheree352ec2011-05-24 16:40:23 +020059 'True', 'True', 'True', 'True', 'False', 'True',
60 'True', 'True', 'False'
Armin Ronacher41d6f712010-02-09 16:43:12 +010061 ]
62
63 def test_sequence(self):
64 tmpl = env.from_string(
65 '{{ [1, 2, 3] is sequence }}|'
66 '{{ "foo" is sequence }}|'
67 '{{ 42 is sequence }}'
68 )
69 assert tmpl.render() == 'True|True|False'
70
71 def test_upper(self):
72 tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')
73 assert tmpl.render() == 'True|False'
74
Thomas van Noort40367c42013-11-22 13:50:51 +010075 def test_equalto(self):
76 tmpl = env.from_string('{{ foo is equalto 12 }}|'
77 '{{ foo is equalto 0 }}|'
78 '{{ foo is equalto (3 * 4) }}|'
79 '{{ bar is equalto "baz" }}|'
80 '{{ bar is equalto "zab" }}|'
81 '{{ bar is equalto ("ba" + "z") }}|'
82 '{{ bar is equalto bar }}|'
83 '{{ bar is equalto foo }}')
84 assert tmpl.render(foo=12, bar="baz") == 'True|False|True|True|False|True|True|False'
85
Armin Ronacher41d6f712010-02-09 16:43:12 +010086 def test_sameas(self):
87 tmpl = env.from_string('{{ foo is sameas false }}|'
88 '{{ 0 is sameas false }}')
89 assert tmpl.render(foo=False) == 'True|False'
90
91 def test_no_paren_for_arg1(self):
92 tmpl = env.from_string('{{ foo is sameas none }}')
93 assert tmpl.render(foo=None) == 'True'
94
95 def test_escaped(self):
96 env = Environment(autoescape=True)
97 tmpl = env.from_string('{{ x is escaped }}|{{ y is escaped }}')
98 assert tmpl.render(x='foo', y=Markup('foo')) == 'False|True'
99
100
101def suite():
102 suite = unittest.TestSuite()
103 suite.addTest(unittest.makeSuite(TestsTestCase))
104 return suite