comparison venv/lib/python2.7/site-packages/markupsafe/tests.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 # -*- coding: utf-8 -*-
2 import gc
3 import sys
4 import unittest
5 from markupsafe import Markup, escape, escape_silent
6 from markupsafe._compat import text_type
7
8
9 class MarkupTestCase(unittest.TestCase):
10
11 def test_adding(self):
12 # adding two strings should escape the unsafe one
13 unsafe = '<script type="application/x-some-script">alert("foo");</script>'
14 safe = Markup('<em>username</em>')
15 assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)
16
17 def test_string_interpolation(self):
18 # string interpolations are safe to use too
19 assert Markup('<em>%s</em>') % '<bad user>' == \
20 '<em>&lt;bad user&gt;</em>'
21 assert Markup('<em>%(username)s</em>') % {
22 'username': '<bad user>'
23 } == '<em>&lt;bad user&gt;</em>'
24
25 assert Markup('%i') % 3.14 == '3'
26 assert Markup('%.2f') % 3.14 == '3.14'
27
28 def test_type_behavior(self):
29 # an escaped object is markup too
30 assert type(Markup('foo') + 'bar') is Markup
31
32 # and it implements __html__ by returning itself
33 x = Markup("foo")
34 assert x.__html__() is x
35
36 def test_html_interop(self):
37 # it also knows how to treat __html__ objects
38 class Foo(object):
39 def __html__(self):
40 return '<em>awesome</em>'
41 def __unicode__(self):
42 return 'awesome'
43 __str__ = __unicode__
44 assert Markup(Foo()) == '<em>awesome</em>'
45 assert Markup('<strong>%s</strong>') % Foo() == \
46 '<strong><em>awesome</em></strong>'
47
48 def test_tuple_interpol(self):
49 self.assertEqual(Markup('<em>%s:%s</em>') % (
50 '<foo>',
51 '<bar>',
52 ), Markup(u'<em>&lt;foo&gt;:&lt;bar&gt;</em>'))
53
54 def test_dict_interpol(self):
55 self.assertEqual(Markup('<em>%(foo)s</em>') % {
56 'foo': '<foo>',
57 }, Markup(u'<em>&lt;foo&gt;</em>'))
58 self.assertEqual(Markup('<em>%(foo)s:%(bar)s</em>') % {
59 'foo': '<foo>',
60 'bar': '<bar>',
61 }, Markup(u'<em>&lt;foo&gt;:&lt;bar&gt;</em>'))
62
63 def test_escaping(self):
64 # escaping and unescaping
65 assert escape('"<>&\'') == '&#34;&lt;&gt;&amp;&#39;'
66 assert Markup("<em>Foo &amp; Bar</em>").striptags() == "Foo & Bar"
67 assert Markup("&lt;test&gt;").unescape() == "<test>"
68
69 def test_formatting(self):
70 for actual, expected in (
71 (Markup('%i') % 3.14, '3'),
72 (Markup('%.2f') % 3.14159, '3.14'),
73 (Markup('%s %s %s') % ('<', 123, '>'), '&lt; 123 &gt;'),
74 (Markup('<em>{awesome}</em>').format(awesome='<awesome>'),
75 '<em>&lt;awesome&gt;</em>'),
76 (Markup('{0[1][bar]}').format([0, {'bar': '<bar/>'}]),
77 '&lt;bar/&gt;'),
78 (Markup('{0[1][bar]}').format([0, {'bar': Markup('<bar/>')}]),
79 '<bar/>')):
80 assert actual == expected, "%r should be %r!" % (actual, expected)
81
82 # This is new in 2.7
83 if sys.version_info >= (2, 7):
84 def test_formatting_empty(self):
85 formatted = Markup('{}').format(0)
86 assert formatted == Markup('0')
87
88 def test_custom_formatting(self):
89 class HasHTMLOnly(object):
90 def __html__(self):
91 return Markup('<foo>')
92
93 class HasHTMLAndFormat(object):
94 def __html__(self):
95 return Markup('<foo>')
96 def __html_format__(self, spec):
97 return Markup('<FORMAT>')
98
99 assert Markup('{0}').format(HasHTMLOnly()) == Markup('<foo>')
100 assert Markup('{0}').format(HasHTMLAndFormat()) == Markup('<FORMAT>')
101
102 def test_complex_custom_formatting(self):
103 class User(object):
104 def __init__(self, id, username):
105 self.id = id
106 self.username = username
107 def __html_format__(self, format_spec):
108 if format_spec == 'link':
109 return Markup('<a href="/user/{0}">{1}</a>').format(
110 self.id,
111 self.__html__(),
112 )
113 elif format_spec:
114 raise ValueError('Invalid format spec')
115 return self.__html__()
116 def __html__(self):
117 return Markup('<span class=user>{0}</span>').format(self.username)
118
119 user = User(1, 'foo')
120 assert Markup('<p>User: {0:link}').format(user) == \
121 Markup('<p>User: <a href="/user/1"><span class=user>foo</span></a>')
122
123 def test_all_set(self):
124 import markupsafe as markup
125 for item in markup.__all__:
126 getattr(markup, item)
127
128 def test_escape_silent(self):
129 assert escape_silent(None) == Markup()
130 assert escape(None) == Markup(None)
131 assert escape_silent('<foo>') == Markup(u'&lt;foo&gt;')
132
133 def test_splitting(self):
134 self.assertEqual(Markup('a b').split(), [
135 Markup('a'),
136 Markup('b')
137 ])
138 self.assertEqual(Markup('a b').rsplit(), [
139 Markup('a'),
140 Markup('b')
141 ])
142 self.assertEqual(Markup('a\nb').splitlines(), [
143 Markup('a'),
144 Markup('b')
145 ])
146
147 def test_mul(self):
148 self.assertEqual(Markup('a') * 3, Markup('aaa'))
149
150
151 class MarkupLeakTestCase(unittest.TestCase):
152
153 def test_markup_leaks(self):
154 counts = set()
155 for count in range(20):
156 for item in range(1000):
157 escape("foo")
158 escape("<foo>")
159 escape(u"foo")
160 escape(u"<foo>")
161 counts.add(len(gc.get_objects()))
162 assert len(counts) == 1, 'ouch, c extension seems to leak objects'
163
164
165 def suite():
166 suite = unittest.TestSuite()
167 suite.addTest(unittest.makeSuite(MarkupTestCase))
168
169 # this test only tests the c extension
170 if not hasattr(escape, 'func_code'):
171 suite.addTest(unittest.makeSuite(MarkupLeakTestCase))
172
173 return suite
174
175
176 if __name__ == '__main__':
177 unittest.main(defaultTest='suite')
178
179 # vim:sts=4:sw=4:et: