Add 2to3.py script to run both the default and our custom fixes locally
[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 gc
12 import unittest
13
14 import pickle
15
16 from jinja2.testsuite import JinjaTestCase
17
18 from jinja2.utils import LRUCache, escape, object_type_repr
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 HelpersTestCase(JinjaTestCase):
47
48     def test_object_type_repr(self):
49         class X(object):
50             pass
51         self.assert_equal(object_type_repr(42), 'int object')
52         self.assert_equal(object_type_repr([]), 'list object')
53         self.assert_equal(object_type_repr(X()),
54                          'jinja2.testsuite.utils.X object')
55         self.assert_equal(object_type_repr(None), 'None')
56         self.assert_equal(object_type_repr(Ellipsis), 'Ellipsis')
57
58
59 class MarkupLeakTestCase(JinjaTestCase):
60
61     def test_markup_leaks(self):
62         counts = set()
63         for count in xrange(20):
64             for item in xrange(1000):
65                 escape("foo")
66                 escape("<foo>")
67                 escape(u"foo")
68                 escape(u"<foo>")
69             counts.add(len(gc.get_objects()))
70         assert len(counts) == 1, 'ouch, c extension seems to leak objects'
71
72
73 def suite():
74     suite = unittest.TestSuite()
75     suite.addTest(unittest.makeSuite(LRUCacheTestCase))
76     suite.addTest(unittest.makeSuite(HelpersTestCase))
77
78     # this test only tests the c extension
79     if not hasattr(escape, 'func_code'):
80         suite.addTest(unittest.makeSuite(MarkupLeakTestCase))
81
82     return suite