Merge pull request #824 from dagwieers/new-type-tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add more strict type tests
authored by David Lord · committed by GitHub · parents 9aa8296, 9bd3cb2 · Browse files · 3 files·+143 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+143 added−36 removed
- tests:+94 added−36 removed
- other:+49 added−0 removed
Provenance
- Commit
0b9d252- Tree
1d7105e- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
9aa8296- Parent 2
9bd3cb2
3 files changed+143 added−36 removed
modified:jinja2/tests.py+47 added
61 unmodified lines62 """Return true if the variable is none."""63 return value is None64 65 66+def test_boolean(value):67+ """Return true if the object is a boolean value.68+69+ .. versionadded:: 2.1170+ """71+ return value is True or value is False72+73+74+def test_false(value):75+ """Return true if the object is False.76+77+ .. versionadded:: 2.1178+ """79+ return value is False80+81+82+def test_true(value):83+ """Return true if the object is True.84+85+ .. versionadded:: 2.1186+ """87+ return value is True88+89+90+# NOTE: The existing Jinja2 'number' test matches booleans and floats91+def test_integer(value):92+ """Return true if the object is an integer.93+94+ .. versionadded:: 2.1195+ """96+ return isinstance(value, integer_types) and value is not True and value is not False97+98+99+# NOTE: The existing Jinja2 'number' test matches booleans and integers100+def test_float(value):101+ """Return true if the object is a float.102+103+ .. versionadded:: 2.11104+ """105+ return isinstance(value, float)106+107+108 def test_lower(value):109 """Return true if the variable is lowercased."""110 return text_type(value).islower()111 74 unmodified lines186 'divisibleby': test_divisibleby,187 'defined': test_defined,188 'undefined': test_undefined,189 'none': test_none,190+ 'boolean': test_boolean,191+ 'false': test_false,192+ 'true': test_true,193+ 'integer': test_integer,194+ 'float': test_float,195 'lower': test_lower,196 'upper': test_upper,197 'string': test_string,198 'mapping': test_mapping,23 unmodified linesmodified:tests/test_tests.py+94 added-36 removed
10 unmodified lines11 import pytest12 13 from jinja2 import Markup, Environment14 15+class MyDict(dict):16+ pass17 18 @pytest.mark.test_tests19 class TestTestsCase(object):20 13 unmodified lines34 def test_lower(self, env):35 tmpl = env.from_string('''{{ "foo" is lower }}|{{ "FOO" is lower }}''')36 assert tmpl.render() == 'True|False'37 36- def test_typechecks(self, env):37- tmpl = env.from_string('''38- {{ 42 is undefined }}39- {{ 42 is defined }}40- {{ 42 is none }}41- {{ none is none }}42- {{ 42 is number }}43- {{ 42 is string }}44- {{ "foo" is string }}45- {{ "foo" is sequence }}46- {{ [1] is sequence }}47- {{ range is callable }}48- {{ 42 is callable }}49- {{ range(5) is iterable }}50- {{ {} is mapping }}51- {{ mydict is mapping }}52- {{ [] is mapping }}53- {{ 10 is number }}54- {{ (10 ** 100) is number }}55- {{ 3.14159 is number }}56- {{ complex is number }}57- ''')38+ # Test type checks39+ @pytest.mark.parametrize('op,expect', (40+ ('none is none', True),41+ ('false is none', False),42+ ('true is none', False),43+ ('42 is none', False),44 59- class MyDict(dict):60- pass45+ ('none is true', False),46+ ('false is true', False),47+ ('true is true', True),48+ ('0 is true', False),49+ ('1 is true', False),50+ ('42 is true', False),51 62- assert tmpl.render(mydict=MyDict(), complex=complex(1, 2)).split() == [63- 'False', 'True', 'False', 'True', 'True', 'False',64- 'True', 'True', 'True', 'True', 'False', 'True',65- 'True', 'True', 'False', 'True', 'True', 'True', 'True'66- ]52+ ('none is false', False),53+ ('false is false', True),54+ ('true is false', False),55+ ('0 is false', False),56+ ('1 is false', False),57+ ('42 is false', False),58 68- def test_sequence(self, env):69- tmpl = env.from_string(70- '{{ [1, 2, 3] is sequence }}|'71- '{{ "foo" is sequence }}|'72- '{{ 42 is sequence }}'73- )74- assert tmpl.render() == 'True|True|False'59+ ('none is boolean', False),60+ ('false is boolean', True),61+ ('true is boolean', True),62+ ('0 is boolean', False),63+ ('1 is boolean', False),64+ ('42 is boolean', False),65+ ('0.0 is boolean', False),66+ ('1.0 is boolean', False),67+ ('3.14159 is boolean', False),68 69+ ('none is integer', False),70+ ('false is integer', False),71+ ('true is integer', False),72+ ('42 is integer', True),73+ ('3.14159 is integer', False),74+ ('(10 ** 100) is integer', True),75+76+ ('none is float', False),77+ ('false is float', False),78+ ('true is float', False),79+ ('42 is float', False),80+ ('4.2 is float', True),81+ ('(10 ** 100) is float', False),82+83+ ('none is number', False),84+ ('false is number', True),85+ ('true is number', True),86+ ('42 is number', True),87+ ('3.14159 is number', True),88+ ('complex is number', True),89+ ('(10 ** 100) is number', True),90+91+ ('none is string', False),92+ ('false is string', False),93+ ('true is string', False),94+ ('42 is string', False),95+ ('"foo" is string', True),96+97+ ('none is sequence', False),98+ ('false is sequence', False),99+ ('42 is sequence', False),100+ ('"foo" is sequence', True),101+ ('[] is sequence', True),102+ ('[1, 2, 3] is sequence', True),103+ ('{} is sequence', True),104+105+ ('none is mapping', False),106+ ('false is mapping', False),107+ ('42 is mapping', False),108+ ('"foo" is mapping', False),109+ ('[] is mapping', False),110+ ('{} is mapping', True),111+ ('mydict is mapping', True),112+113+ ('none is iterable', False),114+ ('false is iterable', False),115+ ('42 is iterable', False),116+ ('"foo" is iterable', True),117+ ('[] is iterable', True),118+ ('{} is iterable', True),119+ ('range(5) is iterable', True),120+121+ ('none is callable', False),122+ ('false is callable', False),123+ ('42 is callable', False),124+ ('"foo" is callable', False),125+ ('[] is callable', False),126+ ('{} is callable', False),127+ ('range is callable', True),128+ ))129+ def test_types(self, env, op, expect):130+ t = env.from_string('{{{{ {op} }}}}'.format(op=op))131+ assert t.render(mydict=MyDict(), complex=complex(1, 2)) == str(expect)132+133+134 def test_upper(self, env):135 tmpl = env.from_string('{{ "FOO" is upper }}|{{ "foo" is upper }}')136 assert tmpl.render() == 'True|False'137 82 unmodified linesmodified:CHANGES.rst+2 added
30 unmodified lines31 :issue:`857`, :pr:`858`32 - Parentheses around comparisons are preserved, so33 ``{{ 2 * (3 < 5) }}`` outputs "2" instead of "False".34 :issue:`755`, :pr:`938`35+- Add new ``boolean``, ``false``, ``true``, ``integer`` and ``float``36+ tests. :pr:`824`37 38 39 Version 2.10.340 --------------629 unmodified lines