1
# -*- coding: utf-8 -*-
5
from markupsafe import Markup, escape, escape_silent
6
from markupsafe._compat import text_type
9
class MarkupTestCase(unittest.TestCase):
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)
17
def test_string_interpolation(self):
18
# string interpolations are safe to use too
19
assert Markup('<em>%s</em>') % '<bad user>' == \
20
'<em><bad user></em>'
21
assert Markup('<em>%(username)s</em>') % {
22
'username': '<bad user>'
23
} == '<em><bad user></em>'
25
assert Markup('%i') % 3.14 == '3'
26
assert Markup('%.2f') % 3.14 == '3.14'
28
def test_type_behavior(self):
29
# an escaped object is markup too
30
assert type(Markup('foo') + 'bar') is Markup
32
# and it implements __html__ by returning itself
34
assert x.__html__() is x
36
def test_html_interop(self):
37
# it also knows how to treat __html__ objects
40
return '<em>awesome</em>'
41
def __unicode__(self):
44
assert Markup(Foo()) == '<em>awesome</em>'
45
assert Markup('<strong>%s</strong>') % Foo() == \
46
'<strong><em>awesome</em></strong>'
48
def test_tuple_interpol(self):
49
self.assertEqual(Markup('<em>%s:%s</em>') % (
52
), Markup(u'<em><foo>:<bar></em>'))
54
def test_dict_interpol(self):
55
self.assertEqual(Markup('<em>%(foo)s</em>') % {
57
}, Markup(u'<em><foo></em>'))
58
self.assertEqual(Markup('<em>%(foo)s:%(bar)s</em>') % {
61
}, Markup(u'<em><foo>:<bar></em>'))
63
def test_escaping(self):
64
# escaping and unescaping
65
assert escape('"<>&\'') == '"<>&''
66
assert Markup("<em>Foo & Bar</em>").striptags() == "Foo & Bar"
67
assert Markup("<test>").unescape() == "<test>"
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, '>'), '< 123 >'),
74
(Markup('<em>{awesome}</em>').format(awesome='<awesome>'),
75
'<em><awesome></em>'),
76
(Markup('{0[1][bar]}').format([0, {'bar': '<bar/>'}]),
78
(Markup('{0[1][bar]}').format([0, {'bar': Markup('<bar/>')}]),
80
assert actual == expected, "%r should be %r!" % (actual, expected)
83
if sys.version_info >= (2, 7):
84
def test_formatting_empty(self):
85
formatted = Markup('{}').format(0)
86
assert formatted == Markup('0')
88
def test_custom_formatting(self):
89
class HasHTMLOnly(object):
91
return Markup('<foo>')
93
class HasHTMLAndFormat(object):
95
return Markup('<foo>')
96
def __html_format__(self, spec):
97
return Markup('<FORMAT>')
99
assert Markup('{0}').format(HasHTMLOnly()) == Markup('<foo>')
100
assert Markup('{0}').format(HasHTMLAndFormat()) == Markup('<FORMAT>')
102
def test_complex_custom_formatting(self):
104
def __init__(self, id, username):
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(
114
raise ValueError('Invalid format spec')
115
return self.__html__()
117
return Markup('<span class=user>{0}</span>').format(self.username)
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>')
123
def test_all_set(self):
124
import markupsafe as markup
125
for item in markup.__all__:
126
getattr(markup, item)
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'<foo>')
133
def test_splitting(self):
134
self.assertEqual(Markup('a b').split(), [
138
self.assertEqual(Markup('a b').rsplit(), [
142
self.assertEqual(Markup('a\nb').splitlines(), [
148
self.assertEqual(Markup('a') * 3, Markup('aaa'))
151
class MarkupLeakTestCase(unittest.TestCase):
153
def test_markup_leaks(self):
155
for count in range(20):
156
for item in range(1000):
161
counts.add(len(gc.get_objects()))
162
assert len(counts) == 1, 'ouch, c extension seems to leak objects'
166
suite = unittest.TestSuite()
167
suite.addTest(unittest.makeSuite(MarkupTestCase))
169
# this test only tests the c extension
170
if not hasattr(escape, 'func_code'):
171
suite.addTest(unittest.makeSuite(MarkupLeakTestCase))
176
if __name__ == '__main__':
177
unittest.main(defaultTest='suite')