test_utils.py
This file
Path
tests/test_utils.py
205 lines · 6.2 KiB · Python
Last changed by
Merge branch 'stable'David Lordbefore adoption6aeab5d
History of this fileTake it
The walk stopped after 200 commits: lines older than c580dd0 point at it.
Merge branch 'stable'David Lordbefore adoption6aeab5d
line 1why, one step back1import copyupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 2–26why, one step back2import pickle3import random4from collections import deque5from copy import copy as shallow_copy67import pytest8from markupsafe import Markup910from jinja2.utils import consume11from jinja2.utils import generate_lorem_ipsum12from jinja2.utils import LRUCache13from jinja2.utils import missing14from jinja2.utils import object_type_repr15from jinja2.utils import select_autoescape16from jinja2.utils import urlize171819class TestLRUCache:20 def test_simple(self):21 d = LRUCache(3)22 d["a"] = 123 d["b"] = 224 d["c"] = 325 d["a"]26 d["d"] = 4Merge branch '3.0.x'David Lordbefore adoptioncdbda09
line 27why, one step back27 assert d.keys() == ["d", "a", "c"]update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 28why, one step back28Merge branch '3.0.x'David Lordbefore adoptioncdbda09
line 29why, one step back29 def test_values(self):update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 30–32why, one step back30 cache = LRUCache(3)31 cache["b"] = 132 cache["a"] = 2Merge branch '3.0.x'David Lordbefore adoptioncdbda09
line 33why, one step back33 assert cache.values() == [2, 1]update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 34why, one step back34Merge branch '3.0.x'David Lordbefore adoptioncdbda09
line 35why, one step back35 def test_values_empty(self):update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 36why, one step back36 cache = LRUCache(2)Merge branch '3.0.x'David Lordbefore adoptioncdbda09
line 37why, one step back37 assert cache.values() == []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 38–59why, one step back3839 def test_pickleable(self):40 cache = LRUCache(2)41 cache["foo"] = 4242 cache["bar"] = 2343 cache["foo"]4445 for protocol in range(3):46 copy = pickle.loads(pickle.dumps(cache, protocol))47 assert copy.capacity == cache.capacity48 assert copy._mapping == cache._mapping49 assert copy._queue == cache._queue5051 @pytest.mark.parametrize("copy_func", [LRUCache.copy, shallow_copy])52 def test_copy(self, copy_func):53 cache = LRUCache(2)54 cache["a"] = 155 cache["b"] = 256 copy = copy_func(cache)57 assert copy._queue == cache._queue58 copy["c"] = 359 assert copy._queue != cache._queueMerge branch '3.0.x'David Lordbefore adoptioncdbda09
line 60why, one step back60 assert copy.keys() == ["c", "b"]update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 61–144why, one step back6162 def test_clear(self):63 d = LRUCache(3)64 d["a"] = 165 d["b"] = 266 d["c"] = 367 d.clear()68 assert d.__getstate__() == {"capacity": 3, "_mapping": {}, "_queue": deque([])}6970 def test_repr(self):71 d = LRUCache(3)72 d["a"] = 173 d["b"] = 274 d["c"] = 375 # Sort the strings - mapping is unordered76 assert sorted(repr(d)) == sorted("<LRUCache {'a': 1, 'b': 2, 'c': 3}>")7778 def test_items(self):79 """Test various items, keys, values and iterators of LRUCache."""80 d = LRUCache(3)81 d["a"] = 182 d["b"] = 283 d["c"] = 384 assert d.items() == [("c", 3), ("b", 2), ("a", 1)]85 assert d.keys() == ["c", "b", "a"]86 assert d.values() == [3, 2, 1]87 assert list(reversed(d)) == ["a", "b", "c"]8889 # Change the cache a little90 d["b"]91 d["a"] = 492 assert d.items() == [("a", 4), ("b", 2), ("c", 3)]93 assert d.keys() == ["a", "b", "c"]94 assert d.values() == [4, 2, 3]95 assert list(reversed(d)) == ["c", "b", "a"]9697 def test_setdefault(self):98 d = LRUCache(3)99 assert len(d) == 0100 assert d.setdefault("a") is None101 assert d.setdefault("a", 1) is None102 assert len(d) == 1103 assert d.setdefault("b", 2) == 2104 assert len(d) == 2105106107class TestHelpers:108 def test_object_type_repr(self):109 class X:110 pass111112 assert object_type_repr(42) == "int object"113 assert object_type_repr([]) == "list object"114 assert object_type_repr(X()) == "test_utils.X object"115 assert object_type_repr(None) == "None"116 assert object_type_repr(Ellipsis) == "Ellipsis"117118 def test_autoescape_select(self):119 func = select_autoescape(120 enabled_extensions=("html", ".htm"),121 disabled_extensions=("txt",),122 default_for_string="STRING",123 default="NONE",124 )125126 assert func(None) == "STRING"127 assert func("unknown.foo") == "NONE"128 assert func("foo.html")129 assert func("foo.htm")130 assert not func("foo.txt")131 assert func("FOO.HTML")132 assert not func("FOO.TXT")133134135class TestEscapeUrlizeTarget:136 def test_escape_urlize_target(self):137 url = "http://example.org"138 target = "<script>"139 assert urlize(url, target=target) == (140 '<a href="http://example.org"'141 ' target="<script>">'142 "http://example.org</a>"143 )144Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 145–151why, one step back145 def test_urlize_mail_mastodon(self):146 fr = "nabijaczleweli@nabijaczleweli.xyz\n@eater@cijber.social\n"147 to = (148 '<a href="mailto:nabijaczleweli@nabijaczleweli.xyz">'149 "nabijaczleweli@nabijaczleweli.xyz</a>\n@eater@cijber.social\n"150 )151 assert urlize(fr) == toupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 152why, one step back152Merge branch 'stable'David Lordbefore adoption6aeab5d
line 153why, one step back153update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 154–194why, one step back154class TestLoremIpsum:155 def test_lorem_ipsum_markup(self):156 """Test that output of lorem_ipsum is Markup by default."""157 assert isinstance(generate_lorem_ipsum(), Markup)158159 def test_lorem_ipsum_html(self):160 """Test that output of lorem_ipsum is a string_type when not html."""161 assert isinstance(generate_lorem_ipsum(html=False), str)162163 def test_lorem_ipsum_n(self):164 """Test that the n (number of lines) works as expected."""165 assert generate_lorem_ipsum(n=0, html=False) == ""166 for n in range(1, 50):167 assert generate_lorem_ipsum(n=n, html=False).count("\n") == (n - 1) * 2168169 def test_lorem_ipsum_min(self):170 """Test that at least min words are in the output of each line"""171 for _ in range(5):172 m = random.randrange(20, 99)173 for _ in range(10):174 assert generate_lorem_ipsum(n=1, min=m, html=False).count(" ") >= m - 1175176 def test_lorem_ipsum_max(self):177 """Test that at least max words are in the output of each line"""178 for _ in range(5):179 m = random.randrange(21, 100)180 for _ in range(10):181 assert generate_lorem_ipsum(n=1, max=m, html=False).count(" ") < m - 1182183184def test_missing():185 """Test the repr of missing."""186 assert repr(missing) == "missing"187188189def test_consume():190 """Test that consume consumes an iterator."""191 x = iter([1, 2, 3, 4, 5])192 consume(x)193 with pytest.raises(StopIteration):194 next(x)Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 195–205why, one step back195196197@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1))198def test_pickle_missing(protocol: int) -> None:199 """Test that missing can be pickled while remaining a singleton."""200 assert pickle.loads(pickle.dumps(missing, protocol)) is missing201202203def test_copy_missing() -> None:204 """Test that missing can be copied while remaining a singleton."""205 assert copy.copy(missing) is missing