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><bad user></em>'
17 assert Markup('<em>%(username)s</em>') % {
18 'username': '<bad user>'
19 } == '<em><bad user></em>'
21 # an escaped object is markup too
22 assert type(Markup('foo') + 'bar') is Markup
24 # and it implements __html__ by returning itself
26 assert x
.__html
__() is x
28 # it also knows how to treat __html__ objects
31 return '<em>awesome</em>'
32 def __unicode__(self
):
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('"<>&\'') == '"<>&''
40 assert Markup("<em>Foo & Bar</em>").striptags() == "Foo & Bar"
41 assert Markup("<test>").unescape() == "<test>"
43 def test_all_set(self
):
44 import jinja2
._markupsafe
as markup
45 for item
in markup
.__all
__:
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
'<foo>')
54 class MarkupLeakTestCase(unittest
.TestCase
):
56 def test_markup_leaks(self
):
58 for count
in xrange(20):
59 for item
in xrange(1000):
64 counts
.add(len(gc
.get_objects()))
65 assert len(counts
) == 1, 'ouch, c extension seems to leak objects'
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
))
79 if __name__
== '__main__':
80 unittest
.main(defaultTest
='suite')