Add tests for utils.py - LRUCache, Lorem Ipsum & +
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by EtiennePelletier · committed by David Lord · parent 065b58f · Browse files · 2 files·+96 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+96 added−4 removed
- tests:+96 added−4 removed
Provenance
- Commit
53a5d0a- Tree
ac98d8d- Author
EtiennePelletier <etpelletier93@hotmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
065b58f
2 files changed+96 added−4 removed
modified:tests/conftest.py+1 added
40 unmodified lines41 'inheritance',42 'lexer',43 'lexnparse',44 'loaders',45+ 'loremIpsum',46 'lowlevel',47 'lrucache',48 'lstripblocks',49 'macros',71 unmodified linesmodified:tests/test_utils.py+95 added-4 removed
6 unmodified lines7 8 :copyright: (c) 2017 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10 """11+12+from collections import deque13 import gc14+import pickle15+import random16 17 import pytest18 15-import pickle16-19+from jinja2._compat import string_types, range_type20 from jinja2.utils import LRUCache, escape, object_type_repr, urlize, \18- select_autoescape21+ select_autoescape, generate_lorem_ipsum, missing, consume22+from markupsafe import Markup23 20-24 @pytest.mark.utils25 @pytest.mark.lrucache26 class TestLRUCache(object):27 39 unmodified lines67 assert copy.capacity == cache.capacity68 assert copy._mapping == cache._mapping69 assert copy._queue == cache._queue70 71+ def test_clear(self):72+ d = LRUCache(3)73+ d["a"] = 174+ d["b"] = 275+ d["c"] = 376+ d.clear()77+ assert d.__getstate__() == {'capacity': 3, '_mapping': {}, '_queue': deque([])}78 79+ def test_repr(self):80+ d = LRUCache(3)81+ d["a"] = 182+ d["b"] = 283+ d["c"] = 384+ # Sort the strings - mapping is unordered85+ assert sorted(repr(d)) == sorted(u"<LRUCache {'a': 1, 'b': 2, 'c': 3}>")86+87+ def test_items(self):88+ """Test various items, keys, values and iterators of LRUCache."""89+ d = LRUCache(3)90+ d["a"] = 191+ d["b"] = 292+ d["c"] = 393+ assert d.items() == list(d.iteritems()) == [('c', 3), ('b', 2), ('a', 1)]94+ assert d.keys() == list(d.iterkeys()) == ['c', 'b', 'a']95+ assert d.values() == list(d.itervalues()) == [3, 2, 1]96+ assert list(reversed(d)) == ['a', 'b', 'c']97+98+ # Change the cache a little99+ d["b"]100+ d["a"] = 4101+ assert d.items() == list(d.iteritems()) == [('a', 4), ('b', 2), ('c', 3)]102+ assert d.keys() == list(d.iterkeys()) == ['a', 'b', 'c']103+ assert d.values() == list(d.itervalues()) == [4, 2, 3]104+ assert list(reversed(d)) == ['c', 'b', 'a']105+106+ def test_setdefault(self):107+ d = LRUCache(3)108+ assert len(d) == 0109+ assert d.setdefault("a") is None110+ assert d.setdefault("a", 1) is None111+ assert len(d) == 1112+ assert d.setdefault("b", 2) == 2113+ assert len(d) == 2114+115+116 @pytest.mark.utils117 @pytest.mark.helpers118 class TestHelpers(object):119 31 unmodified lines151 target = "<script>"152 assert urlize(url, target=target) == ('<a href="http://example.org"'153 ' target="<script>">'154 'http://example.org</a>')155+156+157+@pytest.mark.utils158+@pytest.mark.loremIpsum159+class TestLoremIpsum(object):160+ def test_lorem_ipsum_markup(self):161+ """Test that output of lorem_ipsum is Markup by default."""162+ assert isinstance(generate_lorem_ipsum(), Markup)163+164+ def test_lorem_ipsum_html(self):165+ """Test that output of lorem_ipsum is a string_type when not html."""166+ assert isinstance(generate_lorem_ipsum(html=False), string_types)167+168+ def test_lorem_ipsum_n(self):169+ """Test that the n (number of lines) works as expected."""170+ assert generate_lorem_ipsum(n=0, html=False) == u''171+ for n in range_type(1, 50):172+ assert generate_lorem_ipsum(n=n, html=False).count('\n') == (n - 1) * 2173+174+ def test_lorem_ipsum_min(self):175+ """Test that at least min words are in the output of each line"""176+ for _ in range_type(5):177+ m = random.randrange(20, 99)178+ for _ in range_type(10):179+ assert generate_lorem_ipsum(n=1, min=m, html=False).count(' ') >= m - 1180+181+ def test_lorem_ipsum_max(self):182+ """Test that at least max words are in the output of each line"""183+ for _ in range_type(5):184+ m = random.randrange(21, 100)185+ for _ in range_type(10):186+ assert generate_lorem_ipsum(n=1, max=m, html=False).count(' ') < m - 1187+188+189+def test_missing():190+ """Test the repr of missing."""191+ assert repr(missing) == u'missing'192+193+def test_consume():194+ """Test that consume consumes an iterator."""195+ x = iter([1, 2, 3, 4, 5])196+ consume(x)197+ with pytest.raises(StopIteration):198+ next(x)