test_utils.py
This file
Path
tests/test_utils.py
87 lines · 2.3 KiB · Python
Last changed by
Merge branch 'master' of github.com:mitsuhiko/jinja2Armin Ronacherbefore adoption67d015e
History of this fileTake it
1# -*- coding: utf-8 -*-2"""3 jinja2.testsuite.utils4 ~~~~~~~~~~~~~~~~~~~~~~56 Tests utilities jinja uses.78 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11import gc1213import pytest1415import pickle1617from jinja2.utils import LRUCache, escape, object_type_repr, urlize181920@pytest.mark.utils21@pytest.mark.lrucache22class TestLRUCache():2324 def test_simple(self):25 d = LRUCache(3)26 d["a"] = 127 d["b"] = 228 d["c"] = 329 d["a"]30 d["d"] = 431 assert len(d) == 332 assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d3334 def test_pickleable(self):35 cache = LRUCache(2)36 cache["foo"] = 4237 cache["bar"] = 2338 cache["foo"]3940 for protocol in range(3):41 copy = pickle.loads(pickle.dumps(cache, protocol))42 assert copy.capacity == cache.capacity43 assert copy._mapping == cache._mapping44 assert copy._queue == cache._queue454647@pytest.mark.utils48@pytest.mark.helpers49class TestHelpers():5051 def test_object_type_repr(self):52 class X(object):53 pass54 assert object_type_repr(42) == 'int object'55 assert object_type_repr([]) == 'list object'56 assert object_type_repr(X()) == 'test_utils.X object'57 assert object_type_repr(None) == 'None'58 assert object_type_repr(Ellipsis) == 'Ellipsis'596061@pytest.mark.utils62@pytest.mark.markupleak63@pytest.mark.skipif(hasattr(escape, 'func_code'),64 reason='this test only tests the c extension')65class TestMarkupLeak():6667 def test_markup_leaks(self):68 counts = set()69 for count in range(20):70 for item in range(1000):71 escape("foo")72 escape("<foo>")73 escape(u"foo")74 escape(u"<foo>")75 counts.add(len(gc.get_objects()))76 assert len(counts) == 1, 'ouch, c extension seems to leak objects'777879@pytest.mark.utils80@pytest.mark.escapeUrlizeTarget81class TestEscapeUrlizeTarget():82 def test_escape_urlize_target(self):83 url = "http://example.org"84 target = "<script>"85 assert urlize(url, target=target) == ('<a href="http://example.org"'86 ' target="<script>">'87 'http://example.org</a>')