Rest of tests ported, just need to hook up doctests now.
[jinja2.git] / jinja2 / testsuite / utils.py
1 # -*- coding: utf-8 -*-
2 """
3     jinja2.testsuite.utils
4     ~~~~~~~~~~~~~~~~~~~~~~
5
6     Tests utilities jinja uses.
7
8     :copyright: (c) 2010 by the Jinja Team.
9     :license: BSD, see LICENSE for more details.
10 """
11 import os
12 import unittest
13
14 import pickle
15
16 from jinja2 import Environment, Undefined, DebugUndefined, \
17      StrictUndefined, UndefinedError, Template, meta
18 from jinja2.utils import LRUCache
19
20
21 class LRUCacheTestCase(JinjaTestCase):
22
23     def test_simple(self):
24         d = LRUCache(3)
25         d["a"] = 1
26         d["b"] = 2
27         d["c"] = 3
28         d["a"]
29         d["d"] = 4
30         assert len(d) == 3
31         assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d
32
33     def test_pickleable(self):
34         cache = LRUCache(2)
35         cache["foo"] = 42
36         cache["bar"] = 23
37         cache["foo"]
38
39         for protocol in range(3):
40             copy = pickle.loads(pickle.dumps(cache, protocol))
41             assert copy.capacity == cache.capacity
42             assert copy._mapping == cache._mapping
43             assert copy._queue == cache._queue
44
45
46 class MarkupLeakTestCase(JinjaTestCase):
47
48     def test_markup_leaks(self):
49         counts = set()
50         for count in xrange(20):
51             for item in xrange(1000):
52                 escape("foo")
53                 escape("<foo>")
54                 escape(u"foo")
55                 escape(u"<foo>")
56             counts.add(len(gc.get_objects()))
57         assert len(counts) == 1, 'ouch, c extension seems to leak objects'
58
59
60 def suite():
61     suite = unittest.TestSuite()
62     suite.addTest(unittest.makeSuite(LRUCacheTestCase))
63
64     # this test only tests the c extension
65     if not hasattr(escape, 'func_code'):
66         suite.addTest(unittest.makeSuite(MarkupLeakTestCase))
67
68     return suite