| 1 |
import copy |
| 2 |
import pickle |
| 3 |
import random |
| 4 |
from collections import deque |
| 5 |
from copy import copy as shallow_copy |
| 6 |
|
| 7 |
import pytest |
| 8 |
from markupsafe import Markup |
| 9 |
|
| 10 |
from jinja2.utils import consume |
| 11 |
from jinja2.utils import generate_lorem_ipsum |
| 12 |
from jinja2.utils import LRUCache |
| 13 |
from jinja2.utils import missing |
| 14 |
from jinja2.utils import object_type_repr |
| 15 |
from jinja2.utils import select_autoescape |
| 16 |
from jinja2.utils import urlize |
| 17 |
|
| 18 |
|
| 19 |
class TestLRUCache: |
| 20 |
def test_simple(self): |
| 21 |
d = LRUCache(3) |
| 22 |
d["a"] = 1 |
| 23 |
d["b"] = 2 |
| 24 |
d["c"] = 3 |
| 25 |
d["a"] |
| 26 |
d["d"] = 4 |
| 27 |
assert d.keys() == ["d", "a", "c"] |
| 28 |
|
| 29 |
def test_values(self): |
| 30 |
cache = LRUCache(3) |
| 31 |
cache["b"] = 1 |
| 32 |
cache["a"] = 2 |
| 33 |
assert cache.values() == [2, 1] |
| 34 |
|
| 35 |
def test_values_empty(self): |
| 36 |
cache = LRUCache(2) |
| 37 |
assert cache.values() == [] |
| 38 |
|
| 39 |
def test_pickleable(self): |
| 40 |
cache = LRUCache(2) |
| 41 |
cache["foo"] = 42 |
| 42 |
cache["bar"] = 23 |
| 43 |
cache["foo"] |
| 44 |
|
| 45 |
for protocol in range(3): |
| 46 |
copy = pickle.loads(pickle.dumps(cache, protocol)) |
| 47 |
assert copy.capacity == cache.capacity |
| 48 |
assert copy._mapping == cache._mapping |
| 49 |
assert copy._queue == cache._queue |
| 50 |
|
| 51 |
@pytest.mark.parametrize("copy_func", [LRUCache.copy, shallow_copy]) |
| 52 |
def test_copy(self, copy_func): |
| 53 |
cache = LRUCache(2) |
| 54 |
cache["a"] = 1 |
| 55 |
cache["b"] = 2 |
| 56 |
copy = copy_func(cache) |
| 57 |
assert copy._queue == cache._queue |
| 58 |
copy["c"] = 3 |
| 59 |
assert copy._queue != cache._queue |
| 60 |
assert copy.keys() == ["c", "b"] |
| 61 |
|
| 62 |
def test_clear(self): |
| 63 |
d = LRUCache(3) |
| 64 |
d["a"] = 1 |
| 65 |
d["b"] = 2 |
| 66 |
d["c"] = 3 |
| 67 |
d.clear() |
| 68 |
assert d.__getstate__() == {"capacity": 3, "_mapping": {}, "_queue": deque([])} |
| 69 |
|
| 70 |
def test_repr(self): |
| 71 |
d = LRUCache(3) |
| 72 |
d["a"] = 1 |
| 73 |
d["b"] = 2 |
| 74 |
d["c"] = 3 |
| 75 |
# Sort the strings - mapping is unordered |
| 76 |
assert sorted(repr(d)) == sorted("<LRUCache {'a': 1, 'b': 2, 'c': 3}>") |
| 77 |
|
| 78 |
def test_items(self): |
| 79 |
"""Test various items, keys, values and iterators of LRUCache.""" |
| 80 |
d = LRUCache(3) |
| 81 |
d["a"] = 1 |
| 82 |
d["b"] = 2 |
| 83 |
d["c"] = 3 |
| 84 |
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"] |
| 88 |
|
| 89 |
# Change the cache a little |
| 90 |
d["b"] |
| 91 |
d["a"] = 4 |
| 92 |
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"] |
| 96 |
|
| 97 |
def test_setdefault(self): |
| 98 |
d = LRUCache(3) |
| 99 |
assert len(d) == 0 |
| 100 |
assert d.setdefault("a") is None |
| 101 |
assert d.setdefault("a", 1) is None |
| 102 |
assert len(d) == 1 |
| 103 |
assert d.setdefault("b", 2) == 2 |
| 104 |
assert len(d) == 2 |
| 105 |
|
| 106 |
|
| 107 |
class TestHelpers: |
| 108 |
def test_object_type_repr(self): |
| 109 |
class X: |
| 110 |
pass |
| 111 |
|
| 112 |
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" |
| 117 |
|
| 118 |
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 |
) |
| 125 |
|
| 126 |
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") |
| 133 |
|
| 134 |
|
| 135 |
class 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 |
) |
| 144 |
|
| 145 |
def test_urlize_mail_mastodon(self): |
| 146 |
fr = "[email protected]\n@[email protected]\n" |
| 147 |
to = ( |
| 148 |
'<a href="mailto:[email protected]">' |
| 149 |
"[email protected]</a>\n@[email protected]\n" |
| 150 |
) |
| 151 |
assert urlize(fr) == to |
| 152 |
|
| 153 |
|
| 154 |
class 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) |
| 158 |
|
| 159 |
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) |
| 162 |
|
| 163 |
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) * 2 |
| 168 |
|
| 169 |
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 - 1 |
| 175 |
|
| 176 |
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 - 1 |
| 182 |
|
| 183 |
|
| 184 |
def test_missing(): |
| 185 |
"""Test the repr of missing.""" |
| 186 |
assert repr(missing) == "missing" |
| 187 |
|
| 188 |
|
| 189 |
def 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) |
| 195 |
|
| 196 |
|
| 197 |
@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1)) |
| 198 |
def 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 missing |
| 201 |
|
| 202 |
|
| 203 |
def test_copy_missing() -> None: |
| 204 |
"""Test that missing can be copied while remaining a singleton.""" |
| 205 |
assert copy.copy(missing) is missing |