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 fileTake 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 back1"""Built-in template tests used with the ``is`` operator."""Merge branch '3.1.x'David Lordbefore adoptionde61312
line 2why, one step back2update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 3why, one step back3import operatorMerge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 4why, one step back4import typing as tupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 5–8why, one step back5from collections import abc6from numbers import Number78from .runtime import UndefinedMerge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8
line 9why, one step back9from .utils import pass_environmentupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 10why, one step back10Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
lines 11–12why, one step back11if t.TYPE_CHECKING:12 from .environment import Environmentupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 13–14why, one step back1314Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 15why, one step back15def test_odd(value: int) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 16–19why, one step back16 """Return true if the variable is odd."""17 return value % 2 == 11819Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 20why, one step back20def test_even(value: int) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 21–24why, one step back21 """Return true if the variable is even."""22 return value % 2 == 02324Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 25why, one step back25def test_divisibleby(value: int, num: int) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 26–29why, one step back26 """Check if a variable is divisible by a number."""27 return value % num == 02829Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 30why, one step back30def test_defined(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 31–46why, one step back31 """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)4546Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 47why, one step back47def test_undefined(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 48–51why, one step back48 """Like :func:`defined` but the other way round."""49 return isinstance(value, Undefined)5051Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8
line 52why, one step back52@pass_environmentMerge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 53why, one step back53def test_filter(env: "Environment", value: str) -> bool:Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30
lines 54–69why, one step back54 """Check if a filter exists by name. Useful if a filter may be55 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.filters6869Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8
line 70why, one step back70@pass_environmentMerge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 71why, one step back71def test_test(env: "Environment", value: str) -> bool:Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30
lines 72–91why, one step back72 """Check if a test exists by name. Useful if a test may be73 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.tests9091Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 92why, one step back92def test_none(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 93–96why, one step back93 """Return true if the variable is none."""94 return value is None9596Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 97why, one step back97def test_boolean(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 98–104why, one step back98 """Return true if the object is a boolean value.99100 .. versionadded:: 2.11101 """102 return value is True or value is False103104Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 105why, one step back105def test_false(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 106–112why, one step back106 """Return true if the object is False.107108 .. versionadded:: 2.11109 """110 return value is False111112Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 113why, one step back113def test_true(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 114–121why, one step back114 """Return true if the object is True.115116 .. versionadded:: 2.11117 """118 return value is True119120121# NOTE: The existing 'number' test matches booleans and floatsMerge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 122why, one step back122def test_integer(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 123–130why, one step back123 """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 integersMerge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 131why, one step back131def test_float(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 132–138why, one step back132 """Return true if the object is a float.133134 .. versionadded:: 2.11135 """136 return isinstance(value, float)137138Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 139why, one step back139def test_lower(value: str) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 140–143why, one step back140 """Return true if the variable is lowercased."""141 return str(value).islower()142143Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 144why, one step back144def test_upper(value: str) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 145–148why, one step back145 """Return true if the variable is uppercased."""146 return str(value).isupper()147148Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 149why, one step back149def test_string(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 150–153why, one step back150 """Return true if the object is a string."""151 return isinstance(value, str)152153Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 154why, one step back154def test_mapping(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 155–161why, one step back155 """Return true if the object is a mapping (dict etc.).156157 .. versionadded:: 2.6158 """159 return isinstance(value, abc.Mapping)160161Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 162why, one step back162def test_number(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 163–166why, one step back163 """Return true if the variable is a number."""164 return isinstance(value, Number)165166Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 167why, one step back167def test_sequence(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 168–172why, one step back168 """Return true if the variable is a sequence. Sequences are variables169 that are iterable.170 """171 try:172 len(value)Merge branch '3.1.x'David Lordbefore adoptionde61312
line 173why, one step back173 value.__getitem__ # noqa B018update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 174–175why, one step back174 except Exception:175 return FalseMerge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 176why, one step back176update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 177–179why, one step back177 return True178179Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 180why, one step back180def test_sameas(value: t.Any, other: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 181–192why, one step back181 """Check if an object points to the same memory address than another182 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 other191192Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 193why, one step back193def test_iterable(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 194–198why, one step back194 """Check if it's possible to iterate over an object."""195 try:196 iter(value)197 except TypeError:198 return FalseMerge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 199why, one step back199update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 200–202why, one step back200 return True201202Merge pull request #1384 from pallets/typing-filters-testsDavid Lordbefore adoption7bea919
line 203why, one step back203def test_escaped(value: t.Any) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 204–207why, one step back204 """Check if the value is escaped."""205 return hasattr(value, "__html__")206207switch to pyproject.toml (#1793)David Lordbefore adoption05a5f81
line 208why, one step back208def 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 back209 """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 back222 "filter": test_filter,223 "test": test_test,update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 224–236why, one step back224 "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 back237 "callable": callable,update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 238–256why, one step back238 "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}