1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
import sys
from sancho.utest import UTest
from quixote.html import _py_htmltext
from quixote.html import href, url_with_query, url_quote, nl2br, htmltag
markupchars = '<>&"'
quotedchars = '<>&"'
if sys.hexversion >= 0x20400a2:
unicodechars = u'\u1234'
else:
unicodechars = 'x' # lie, Python <= 2.3 is broken
class Wrapper:
def __init__(self, s):
self.s = s
def __repr__(self):
return self.s
def __str__(self):
return self.s
class BrokenError(Exception):
pass
class Broken:
def __str__(self):
raise BrokenError, 'eieee'
def __repr__(self):
raise BrokenError, 'eieee'
htmltext = escape = htmlescape = TemplateIO = stringify = None
class HTMLTest (UTest):
def check_href(self):
assert str(href('/foo/bar', 'bar')) == '<a href="/foo/bar">bar</a>'
def check_url_with_query(self):
assert str(url_with_query('/f/b', a='1')) == '/f/b?a=1'
assert str(url_with_query(
'/f/b', a='1', b='3 4')) == '/f/b?a=1&b=3%204'
def check_nl2br(self):
assert str(nl2br('a\nb\nc')) == 'a<br />\nb<br />\nc'
def check_url_quote(self):
assert url_quote('abc') == 'abc'
assert url_quote('a b c') == 'a%20b%20c'
assert url_quote(None, fallback='abc') == 'abc'
class HTMLTextTest (UTest):
def _pre(self):
global htmltext, escape, htmlescape, TemplateIO, stringify
htmltext = _py_htmltext.htmltext
escape = _py_htmltext._escape_string
stringify = _py_htmltext.stringify
htmlescape = _py_htmltext.htmlescape
TemplateIO = _py_htmltext.TemplateIO
def _post(self):
global htmltext, escape, htmlescape, TemplateIO, stringify
htmltext = escape = htmlescape = TemplateIO = stringify = None
def _check_init(self):
assert str(htmltext('foo')) == 'foo'
assert str(htmltext(markupchars)) == markupchars
assert unicode(htmltext(unicodechars)) == unicodechars
assert str(htmltext(unicode(markupchars))) == markupchars
assert str(htmltext(None)) == 'None'
assert str(htmltext(1)) == '1'
try:
htmltext(Broken())
assert 0
except BrokenError: pass
def check_stringify(self):
assert stringify(markupchars) is markupchars
assert stringify(unicodechars) is unicodechars
assert stringify(Wrapper(unicodechars)) is unicodechars
assert stringify(Wrapper(markupchars)) is markupchars
assert stringify(Wrapper) == str(Wrapper)
assert stringify(None) == str(None)
def check_escape(self):
assert htmlescape(markupchars) == quotedchars
assert isinstance(htmlescape(markupchars), htmltext)
assert escape(markupchars) == quotedchars
assert escape(unicodechars) == unicodechars
assert escape(unicode(markupchars)) == quotedchars
assert isinstance(escape(markupchars), basestring)
assert htmlescape(htmlescape(markupchars)) == quotedchars
try:
escape(1)
assert 0
except TypeError: pass
def check_cmp(self):
s = htmltext("foo")
assert s == 'foo'
assert s != 'bar'
assert s == htmltext('foo')
assert s != htmltext('bar')
assert htmltext(u'\u1234') == u'\u1234'
assert htmltext('1') != 1
assert 1 != s
def check_len(self):
assert len(htmltext('foo')) == 3
assert len(htmltext(markupchars)) == len(markupchars)
assert len(htmlescape(markupchars)) == len(quotedchars)
def check_hash(self):
assert hash(htmltext('foo')) == hash('foo')
assert hash(htmltext(markupchars)) == hash(markupchars)
assert hash(htmlescape(markupchars)) == hash(quotedchars)
def check_concat(self):
s = htmltext("foo")
assert s + 'bar' == "foobar"
assert 'bar' + s == "barfoo"
assert s + htmltext('bar') == "foobar"
assert s + markupchars == "foo" + quotedchars
assert isinstance(s + markupchars, htmltext)
assert markupchars + s == quotedchars + "foo"
assert isinstance(markupchars + s, htmltext)
assert markupchars + htmltext(u'') == quotedchars
try:
s + 1
assert 0
except TypeError: pass
try:
1 + s
assert 0
except TypeError: pass
# mixing unicode and str
assert repr(htmltext('a') + htmltext('b')) == "<htmltext 'ab'>"
assert repr(htmltext(u'a') + htmltext('b')) == "<htmltext u'ab'>"
assert repr(htmltext('a') + htmltext(u'b')) == "<htmltext u'ab'>"
def check_repeat(self):
s = htmltext('a')
assert s * 3 == "aaa"
assert isinstance(s * 3, htmltext)
assert htmlescape(markupchars) * 3 == quotedchars * 3
try:
s * 'a'
assert 0
except TypeError: pass
try:
'a' * s
assert 0
except TypeError: pass
try:
s * s
assert 0
except TypeError: pass
def check_format(self):
s_fmt = htmltext('%s')
u_fmt = htmltext(u'%s')
assert s_fmt % 'foo' == "foo"
assert u_fmt % 'foo' == u"foo"
assert isinstance(s_fmt % 'foo', htmltext)
assert isinstance(u_fmt % 'foo', htmltext)
assert s_fmt % markupchars == quotedchars
assert u_fmt % markupchars == quotedchars
assert s_fmt % None == "None"
assert u_fmt % None == "None"
assert u_fmt % unicodechars == unicodechars
assert htmltext('%r') % Wrapper(markupchars) == quotedchars
assert htmltext('%s%s') % ('foo', htmltext(markupchars)) \
== ("foo" + markupchars)
assert htmltext('%d') % 10 == "10"
assert htmltext('%.1f') % 10 == "10.0"
try:
s_fmt % Broken()
assert 0
except BrokenError: pass
try:
htmltext('%r') % Broken()
assert 0
except BrokenError: pass
try:
s_fmt % (1, 2)
assert 0
except TypeError: pass
assert htmltext('%d') % 12300000000000000000L == "12300000000000000000"
def check_dict_format(self):
args = {'a': 'foo&', 'b': htmltext('bar&')}
result = "foo& 'foo&' bar&"
assert htmltext('%(a)s %(a)r %(b)s') % args == result
assert htmltext('%(a)s') % {'a': 'foo&'} == "foo&"
assert isinstance(htmltext('%(a)s') % {'a': 'a'}, htmltext)
assert htmltext('%s') % {'a': 'foo&'} == "{'a': 'foo&'}"
try:
htmltext('%(a)s') % 1
assert 0
except TypeError: pass
try:
htmltext('%(a)s') % {}
assert 0
except KeyError: pass
assert htmltext('') % {} == ''
assert htmltext('%%') % {} == '%'
def check_join(self):
assert htmltext(' ').join(['foo', 'bar']) == "foo bar"
assert htmltext(' ').join(['foo', markupchars]) == \
"foo " + quotedchars
assert htmlescape(markupchars).join(['foo', 'bar']) == \
"foo" + quotedchars + "bar"
assert htmltext(' ').join([htmltext(markupchars), 'bar']) == \
markupchars + " bar"
assert isinstance(htmltext('').join([]), htmltext)
assert htmltext(u' ').join([unicodechars]) == unicodechars
assert htmltext(u' ').join(['']) == u''
try:
htmltext('').join(1)
assert 0
except TypeError: pass
try:
htmltext('').join([1])
assert 0
except TypeError: pass
def check_startswith(self):
assert htmltext('foo').startswith('fo')
assert htmlescape(markupchars).startswith(markupchars[:3])
assert htmltext(markupchars).startswith(htmltext(markupchars[:3]))
try:
htmltext('').startswith(1)
assert 0
except TypeError: pass
def check_endswith(self):
assert htmltext('foo').endswith('oo')
assert htmlescape(markupchars).endswith(markupchars[-3:])
assert htmltext(markupchars).endswith(htmltext(markupchars[-3:]))
try:
htmltext('').endswith(1)
assert 0
except TypeError: pass
def check_replace(self):
assert htmlescape('&').replace('&', 'foo') == "foo"
assert htmltext('&').replace(htmltext('&'), 'foo') == "foo"
assert htmltext('foo').replace('foo', htmltext('&')) == "&"
assert isinstance(htmltext('a').replace('a', 'b'), htmltext)
try:
htmltext('').replace(1, 'a')
assert 0
except TypeError: pass
def check_lower(self):
assert htmltext('aB').lower() == "ab"
assert isinstance(htmltext('a').lower(), htmltext)
def check_upper(self):
assert htmltext('aB').upper() == "AB"
assert isinstance(htmltext('a').upper(), htmltext)
def check_capitalize(self):
assert htmltext('aB').capitalize() == "Ab"
assert isinstance(htmltext('a').capitalize(), htmltext)
class TemplateTest (UTest):
def _pre(self):
global TemplateIO
TemplateIO = _py_htmltext.TemplateIO
def _post(self):
global TemplateIO
TemplateIO = None
def check_init(self):
TemplateIO()
TemplateIO(html=True)
TemplateIO(html=False)
def check_text_iadd(self):
t = TemplateIO()
assert t.getvalue() == ''
t += "abcd"
assert t.getvalue() == 'abcd'
t += None
assert t.getvalue() == 'abcd'
t += 123
assert t.getvalue() == 'abcd123'
t += u'\u1234'
assert t.getvalue() == u'abcd123\u1234'
try:
t += Broken(); t.getvalue()
assert 0
except BrokenError: pass
def check_html_iadd(self):
t = TemplateIO(html=1)
assert t.getvalue() == ''
t += "abcd"
assert t.getvalue() == 'abcd'
t += None
assert t.getvalue() == 'abcd'
t += 123
assert t.getvalue() == 'abcd123'
try:
t += Broken(); t.getvalue()
assert 0
except BrokenError: pass
t = TemplateIO(html=1)
t += markupchars
assert t.getvalue() == quotedchars
def check_repr(self):
t = TemplateIO()
t += "abcd"
assert "TemplateIO" in repr(t)
def check_str(self):
t = TemplateIO()
t += "abcd"
assert str(t) == "abcd"
try:
from quixote.html import _c_htmltext
except ImportError:
_c_htmltext = None
if _c_htmltext:
class CHTMLTest(HTMLTest):
def _pre(self):
# using globals like this is a bit of a hack since it assumes
# Sancho tests each class individually, oh well
global htmltext, escape, htmlescape, stringify
htmltext = _c_htmltext.htmltext
escape = _c_htmltext._escape_string
stringify = _py_htmltext.stringify
htmlescape = _c_htmltext.htmlescape
class CHTMLTextTest(HTMLTextTest):
def _pre(self):
global htmltext, escape, htmlescape, stringify
htmltext = _c_htmltext.htmltext
escape = _c_htmltext._escape_string
stringify = _py_htmltext.stringify
htmlescape = _c_htmltext.htmlescape
class CTemplateTest(TemplateTest):
def _pre(self):
global TemplateIO
TemplateIO = _c_htmltext.TemplateIO
if __name__ == "__main__":
HTMLTest()
HTMLTextTest()
TemplateTest()
if _c_htmltext:
CHTMLTest()
CHTMLTextTest()
CTemplateTest()
|