Skip to content
Myr

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

2 files+96 added−4 removed

modified:tests/conftest.py+1 added
40 unmodified lines
41 'inheritance',42 'lexer',43 'lexnparse',44 'loaders',45 'loremIpsum',46 'lowlevel',47 'lrucache',48 'lstripblocks',49 'macros',
71 unmodified lines
modified:tests/test_utils.py+95 added-4 removed
6 unmodified lines
78 :copyright: (c) 2017 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""1112from collections import deque13import gc14import pickle15import random1617import pytest1815import pickle1619from jinja2._compat import string_types, range_type20from jinja2.utils import LRUCache, escape, object_type_repr, urlize, \18 select_autoescape21 select_autoescape, generate_lorem_ipsum, missing, consume22from markupsafe import Markup232024@pytest.mark.utils25@pytest.mark.lrucache26class TestLRUCache(object):27
39 unmodified lines
67 assert copy.capacity == cache.capacity68 assert copy._mapping == cache._mapping69 assert copy._queue == cache._queue7071 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([])}7879 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}>")8687 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']9798 # 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']105106 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) == 2114115116@pytest.mark.utils117@pytest.mark.helpers118class TestHelpers(object):119
31 unmodified lines
151 target = "<script>"152 assert urlize(url, target=target) == ('<a href="http://example.org"'153 ' target="&lt;script&gt;">'154 'http://example.org</a>')155156157@pytest.mark.utils158@pytest.mark.loremIpsum159class 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)163164 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)167168 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) * 2173174 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 - 1180181 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 - 1187188189def test_missing():190 """Test the repr of missing."""191 assert repr(missing) == u'missing'192193def 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)