Skip to content
Myr

tests.py

This file

Path

src/jinja2/tests.py

256 lines · 5.8 KiB · Python

Last changed by

Merge branch '3.1.x'David Lordbefore adoptionde61312

History of this file

Take it

The walk stopped after 200 commits: lines older than c580dd0 point at it.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 1why, one step back
1"""Built-in template tests used with the ``is`` operator."""

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 2why, one step back
2

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 3why, one step back
3import operator

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 4why, one step back
4import typing as t

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 5–8why, one step back
5from collections import abc
6from numbers import Number78from .runtime import Undefined

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 9why, one step back
9from .utils import pass_environment

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 10why, one step back
10

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

lines 11–12why, one step back
11if t.TYPE_CHECKING:
12 from .environment import Environment

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 13–14why, one step back
13
14

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 15why, one step back
15def test_odd(value: int) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 16–19why, one step back
16 """Return true if the variable is odd."""
17 return value % 2 == 11819

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 20why, one step back
20def test_even(value: int) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 21–24why, one step back
21 """Return true if the variable is even."""
22 return value % 2 == 02324

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 25why, one step back
25def test_divisibleby(value: int, num: int) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 26–29why, one step back
26 """Check if a variable is divisible by a number."""
27 return value % num == 02829

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 30why, one step back
30def test_defined(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 31–46why, one step back
31 """Return true if the variable is defined:
3233 .. sourcecode:: jinja3435 {% if variable is defined %}36 value of variable: {{ variable }}37 {% else %}38 variable is not defined39 {% endif %}4041 See the :func:`default` filter for a simple way to set undefined42 variables.43 """44 return not isinstance(value, Undefined)4546

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 47why, one step back
47def test_undefined(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 48–51why, one step back
48 """Like :func:`defined` but the other way round."""
49 return isinstance(value, Undefined)5051

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 52why, one step back
52@pass_environment

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 53why, one step back
53def test_filter(env: "Environment", value: str) -> bool:

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 54–69why, one step back
54 """Check if a filter exists by name. Useful if a filter may be
55 optionally available.5657 .. code-block:: jinja5859 {% if 'markdown' is filter %}60 {{ value | markdown }}61 {% else %}62 {{ value }}63 {% endif %}6465 .. versionadded:: 3.066 """67 return value in env.filters6869

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 70why, one step back
70@pass_environment

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 71why, one step back
71def test_test(env: "Environment", value: str) -> bool:

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 72–91why, one step back
72 """Check if a test exists by name. Useful if a test may be
73 optionally available.7475 .. code-block:: jinja7677 {% if 'loud' is test %}78 {% if value is loud %}79 {{ value|upper }}80 {% else %}81 {{ value|lower }}82 {% endif %}83 {% else %}84 {{ value }}85 {% endif %}8687 .. versionadded:: 3.088 """89 return value in env.tests9091

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 92why, one step back
92def test_none(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 93–96why, one step back
93 """Return true if the variable is none."""
94 return value is None9596

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 97why, one step back
97def test_boolean(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 98–104why, one step back
98 """Return true if the object is a boolean value.
99100 .. versionadded:: 2.11101 """102 return value is True or value is False103104

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 105why, one step back
105def test_false(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 106–112why, one step back
106 """Return true if the object is False.
107108 .. versionadded:: 2.11109 """110 return value is False111112

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 113why, one step back
113def test_true(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 114–121why, one step back
114 """Return true if the object is True.
115116 .. versionadded:: 2.11117 """118 return value is True119120121# NOTE: The existing 'number' test matches booleans and floats

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 122why, one step back
122def test_integer(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 123–130why, one step back
123 """Return true if the object is an integer.
124125 .. versionadded:: 2.11126 """127 return isinstance(value, int) and value is not True and value is not False128129130# NOTE: The existing 'number' test matches booleans and integers

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 131why, one step back
131def test_float(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 132–138why, one step back
132 """Return true if the object is a float.
133134 .. versionadded:: 2.11135 """136 return isinstance(value, float)137138

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 139why, one step back
139def test_lower(value: str) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 140–143why, one step back
140 """Return true if the variable is lowercased."""
141 return str(value).islower()142143

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 144why, one step back
144def test_upper(value: str) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 145–148why, one step back
145 """Return true if the variable is uppercased."""
146 return str(value).isupper()147148

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 149why, one step back
149def test_string(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 150–153why, one step back
150 """Return true if the object is a string."""
151 return isinstance(value, str)152153

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 154why, one step back
154def test_mapping(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 155–161why, one step back
155 """Return true if the object is a mapping (dict etc.).
156157 .. versionadded:: 2.6158 """159 return isinstance(value, abc.Mapping)160161

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 162why, one step back
162def test_number(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 163–166why, one step back
163 """Return true if the variable is a number."""
164 return isinstance(value, Number)165166

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 167why, one step back
167def test_sequence(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 168–172why, one step back
168 """Return true if the variable is a sequence. Sequences are variables
169 that are iterable.170 """171 try:172 len(value)

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 173why, one step back
173 value.__getitem__ # noqa B018

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 174–175why, one step back
174 except Exception:
175 return False

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 176why, one step back
176

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 177–179why, one step back
177 return True
178179

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 180why, one step back
180def test_sameas(value: t.Any, other: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 181–192why, one step back
181 """Check if an object points to the same memory address than another
182 object:183184 .. sourcecode:: jinja185186 {% if foo.attribute is sameas false %}187 the foo attribute really is the `False` singleton188 {% endif %}189 """190 return value is other191192

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 193why, one step back
193def test_iterable(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 194–198why, one step back
194 """Check if it's possible to iterate over an object."""
195 try:196 iter(value)197 except TypeError:198 return False

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 199why, one step back
199

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 200–202why, one step back
200 return True
201202

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 203why, one step back
203def test_escaped(value: t.Any) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 204–207why, one step back
204 """Check if the value is escaped."""
205 return hasattr(value, "__html__")206207

switch to pyproject.toml (#1793)David Lordbefore adoption05a5f81

line 208why, one step back
208def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 209–221why, one step back
209 """Check if value is in seq.
210211 .. versionadded:: 2.10212 """213 return value in seq214215216TESTS = {217 "odd": test_odd,218 "even": test_even,219 "divisibleby": test_divisibleby,220 "defined": test_defined,221 "undefined": test_undefined,

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 222–223why, one step back
222 "filter": test_filter,
223 "test": test_test,

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 224–236why, one step back
224 "none": test_none,
225 "boolean": test_boolean,226 "false": test_false,227 "true": test_true,228 "integer": test_integer,229 "float": test_float,230 "lower": test_lower,231 "upper": test_upper,232 "string": test_string,233 "mapping": test_mapping,234 "number": test_number,235 "sequence": test_sequence,236 "iterable": test_iterable,

Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919

line 237why, one step back
237 "callable": callable,

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 238–256why, one step back
238 "sameas": test_sameas,
239 "escaped": test_escaped,240 "in": test_in,241 "==": operator.eq,242 "eq": operator.eq,243 "equalto": operator.eq,244 "!=": operator.ne,245 "ne": operator.ne,246 ">": operator.gt,247 "gt": operator.gt,248 "greaterthan": operator.gt,249 "ge": operator.ge,250 ">=": operator.ge,251 "<": operator.lt,252 "lt": operator.lt,253 "lessthan": operator.lt,254 "<=": operator.le,255 "le": operator.le,256}