Fixed python_path problem.
[smonitor.git] / lib / jinja2 / _markupsafe / tests.py
blobc1ce3943a60be26b3d178c944c29f88ad5032cce
1 import gc
2 import unittest
3 from jinja2._markupsafe import Markup, escape, escape_silent
6 class MarkupTestCase(unittest.TestCase):
8 def test_markup_operations(self):
9 # adding two strings should escape the unsafe one
10 unsafe = '<script type="application/x-some-script">alert("foo");</script>'
11 safe = Markup('<em>username</em>')
12 assert unsafe + safe == unicode(escape(unsafe)) + unicode(safe)
14 # string interpolations are safe to use too
15 assert Markup('<em>%s</em>') % '<bad user>' == \
16 '<em>&lt;bad user&gt;</em>'
17 assert Markup('<em>%(username)s</em>') % {
18 'username': '<bad user>'
19 } == '<em>&lt;bad user&gt;</em>'
21 # an escaped object is markup too
22 assert type(Markup('foo') + 'bar') is Markup
24 # and it implements __html__ by returning itself
25 x = Markup("foo")
26 assert x.__html__() is x
28 # it also knows how to treat __html__ objects
29 class Foo(object):
30 def __html__(self):
31 return '<em>awesome</em>'
32 def __unicode__(self):
33 return 'awesome'
34 assert Markup(Foo()) == '<em>awesome</em>'
35 assert Markup('<strong>%s</strong>') % Foo() == \
36 '<strong><em>awesome</em></strong>'
38 # escaping and unescaping
39 assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
40 assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
41 assert Markup("&lt;test&gt;").unescape() == "<test>"
43 def test_all_set(self):
44 import jinja2._markupsafe as markup
45 for item in markup.__all__:
46 getattr(markup, item)
48 def test_escape_silent(self):
49 assert escape_silent(None) == Markup()
50 assert escape(None) == Markup(None)
51 assert escape_silent('<foo>') == Markup(u'&lt;foo&gt;')
54 class MarkupLeakTestCase(unittest.TestCase):
56 def test_markup_leaks(self):
57 counts = set()
58 for count in xrange(20):
59 for item in xrange(1000):
60 escape("foo")
61 escape("<foo>")
62 escape(u"foo")
63 escape(u"<foo>")
64 counts.add(len(gc.get_objects()))
65 assert len(counts) == 1, 'ouch, c extension seems to leak objects'
68 def suite():
69 suite = unittest.TestSuite()
70 suite.addTest(unittest.makeSuite(MarkupTestCase))
72 # this test only tests the c extension
73 if not hasattr(escape, 'func_code'):
74 suite.addTest(unittest.makeSuite(MarkupLeakTestCase))
76 return suite
79 if __name__ == '__main__':
80 unittest.main(defaultTest='suite')