-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtestxoxo.py
289 lines (270 loc) · 12.4 KB
/
testxoxo.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""testxoxo.py
Unit tests for xoxo.py
This file tests the functions in xoxo.py
The underlying model here is http://diveintopython.org/unit_testing/index.html
run from command line with
python testxoxo.py -v
"""
import xoxo
reload(xoxo)
import unittest
class xoxoTestCases(unittest.TestCase):
def testSimpleList(self):
'''make a xoxo file from a list'''
l = ['1','2','3']
html = xoxo.toXOXO(l)
self.assertEqual(html,'<ol class="xoxo"><li>1</li><li>2</li><li>3</li></ol>')
def testNestedList(self):
'''make a xoxo file from a list with a list in'''
l = ['1',['2','3']]
html = xoxo.toXOXO(l)
self.assertEqual(html,'<ol class="xoxo"><li>1</li><li><ol><li>2</li><li>3</li></ol></li></ol>')
def testDictionary(self):
'''make a xoxo file from a dictionary'''
d = {'test':'1','name':'Kevin'}
html = xoxo.toXOXO(d)
self.assertEqual(html,'<ol class="xoxo"><li><dl><dt>test</dt><dd>1</dd><dt>name</dt><dd>Kevin</dd></dl></li></ol>')
def testSingleItem(self):
'''make a xoxo file from a string'''
l = "test"
html = xoxo.toXOXO(l)
self.assertEqual(html,'<ol class="xoxo"><li>test</li></ol>')
def testWrapDiffers(self):
'''make a xoxo file from a string with and without html wrapper and check they are different'''
l = "test"
html = xoxo.toXOXO(l)
htmlwrap = xoxo.toXOXO(l,addHTMLWrapper=True)
self.failIfEqual(html,htmlwrap)
def testWrapSingleItem(self):
'''make a wrapped xoxo file from a string'''
l = "test"
html = xoxo.toXOXO(l,addHTMLWrapper=True)
self.assertEqual(html,'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><ol class="xoxo"><li>test</li></ol></body></html>''')
def testWrapItemWithCSS(self):
'''make a wrapped xoxo file from a string'''
l = "test"
html = xoxo.toXOXO(l,addHTMLWrapper=True,cssUrl='reaptest.css')
self.assertEqual(html,'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css" >@import "reaptest.css";</style></head><body><ol class="xoxo"><li>test</li></ol></body></html>''')
def testDictionaryRoundTrip(self):
''' make a dictionary into a xoxo file and back again; check it is the same'''
d = {'test':'1','name':'Kevin'}
html = xoxo.toXOXO(d)
newd = xoxo.fromXOXO(html)
self.assertEqual(d,newd)
def testDictionaryWithURLRoundTrip(self):
''' make a dictionary wiht an url in into a xoxo file and back again; check it is the same'''
d = {'url':'http://example.com','name':'Kevin'}
html = xoxo.toXOXO(d)
newd = xoxo.fromXOXO(html)
self.assertEqual(d,newd)
def testNestedDictionaryRoundTrip(self):
''' make a dictionary with a dict in into a xoxo file and back again; check it is the same'''
d = {'test':'1','inner':{'name':'Kevin'}}
html = xoxo.toXOXO(d)
newd = xoxo.fromXOXO(html)
self.assertEqual(d,newd)
def testNestedDictionaryWithURLRoundTrip(self):
''' make a dictionary with an url and a dict into a xoxo file and back again; check it is the same'''
d = {'url':'http://example.com','inner':{'name':'Kevin'}}
html = xoxo.toXOXO(d)
newd = xoxo.fromXOXO(html)
self.assertEqual(d,newd)
def testNestedDictionariesWithURLsRoundTrip(self):
''' make a dictionary with an url and a dict with an url into a xoxo file and back again; check it is the same'''
d = {'url':'http://example.com','inner':{'name':'Kevin','url':'http://slashdot.org'}}
html = xoxo.toXOXO(d)
newd = xoxo.fromXOXO(html)
self.assertEqual(d,newd)
def testListRoundTrip(self):
''' make a list into a xoxo file and back again; check it is the same'''
l = ['3','2','1']
html = xoxo.toXOXO(l)
newdl= xoxo.fromXOXO(html)
self.assertEqual(l,newdl)
def testListofDictsRoundTrip(self):
''' make a list of Dicts into a xoxo file and back again; check it is the same'''
l = ['3',{'a':'2'},{'b':'1','c':'4'}]
html = xoxo.toXOXO(l)
newdl= xoxo.fromXOXO(html)
self.assertEqual(l,newdl)
def testListofListsRoundTrip(self):
''' make a list of Lists into a xoxo file and back again; check it is the same'''
l = ['3',['a','2'],['b',['1',['c','4']]]]
html = xoxo.toXOXO(l)
newdl= xoxo.fromXOXO(html)
self.assertEqual(l,newdl)
def testDictofListsRoundTrip(self):
''' make a dict with lists in into a xoxo file and back again; check it is the same'''
d = {'test':['1','2'],
'name':'Kevin',
'nestlist':['a',['b','c']],
'nestdict':{'e':'6','f':'7'}}
html = xoxo.toXOXO(d)
newd = xoxo.fromXOXO(html)
self.assertEqual(d,newd)
def testXOXOjunkInContainers(self):
'''make sure text outside <li> etc is ignored'''
d=xoxo.fromXOXO('<ol>bad<li><dl>worse<dt>good</dt><dd>buy</dd> now</dl></li></ol>')
self.assertEqual(d,{'good': 'buy'})
def testXOXOjunkInElements(self):
'''make sure text within <li> but outside a subcontainer is ignored'''
l=xoxo.fromXOXO('<ol><li>bad<dl><dt>good</dt><dd>buy</dd></dl>worse</li><li>bag<ol><li>OK</li></ol>fish</li></ol>')
self.assertEqual(l,[{'good': 'buy'},['OK']])
def testXOXOWithSpacesAndNewlines(self):
'''unmung some xoxo with spaces in and check result is right'''
xoxoSample= '''<ol class='xoxo'>
<li>
<dl>
<dt>text</dt>
<dd>item 1</dd>
<dt>description</dt>
<dd> This item represents the main point we're trying to make.</dd>
<dt>url</dt>
<dd>http://example.com/more.xoxo</dd>
<dt>title</dt>
<dd>title of item 1</dd>
<dt>type</dt>
<dd>text/xml</dd>
<dt>rel</dt>
<dd>help</dd>
</dl>
</li>
</ol>'''
d = xoxo.fromXOXO(xoxoSample)
d2={'text':'item 1',
'description':" This item represents the main point we're trying to make.",
'url':'http://example.com/more.xoxo',
'title':'title of item 1',
'type':'text/xml',
'rel':'help'
}
xoxoAgain = xoxo.toXOXO(d)
self.assertEqual(d,d2)
#this needs a smarter whitespace-sensitive comparison
#self.assertEqual(xoxoSample,xoxoAgain)
def testSpecialAttributeDecoding(self):
'''unmung some xoxo with <a href=' rel= etc in and check result is right'''
xoxoSample= '''<ol class='xoxo'>
<li>
<dl>
<dt>text</dt>
<dd>item 1</dd>
<dt>url</dt>
<dd>http://example.com/more.xoxo</dd>
<dt>title</dt>
<dd>title of item 1</dd>
<dt>type</dt>
<dd>text/xml</dd>
<dt>rel</dt>
<dd>help</dd>
</dl>
</li>
</ol>'''
d = xoxo.fromXOXO(xoxoSample)
smartxoxoSample= '''<ol class='xoxo'>
<li><a href="http://example.com/more.xoxo"
title="title of item 1"
type="text/xml"
rel="help">item 1</a>
<!-- note how the "text" property is simply the contents of the <a> element -->
</li>
</ol>'''
d2 = xoxo.fromXOXO(smartxoxoSample)
self.assertEqual(d,d2)
def testSpecialAttributeAndDLDecoding(self):
'''unmung some xoxo with <a href=' rel= etc in plus a <dl> in the same item and check result is right'''
xoxoSample= '''<ol class="xoxo">
<li>
<dl>
<dt>text</dt>
<dd>item 1</dd>
<dt>description</dt>
<dd> This item represents the main point we're trying to make.</dd>
<dt>url</dt>
<dd>http://example.com/more.xoxo</dd>
<dt>title</dt>
<dd>title of item 1</dd>
<dt>type</dt>
<dd>text/xml</dd>
<dt>rel</dt>
<dd>help</dd>
</dl>
</li>
</ol>'''
d = xoxo.fromXOXO(xoxoSample)
smartxoxoSample= '''<ol class="xoxo">
<li><a href="http://example.com/more.xoxo"
title="title of item 1"
type="text/xml"
rel="help">item 1</a>
<!-- note how the "text" property is simply the contents of the <a> element -->
<dl>
<dt>description</dt>
<dd> This item represents the main point we're trying to make.</dd>
</dl>
</li>
</ol>'''
d2 = xoxo.fromXOXO(smartxoxoSample)
self.assertEqual(d,d2)
def testSpecialAttributeEncode(self):
'''check it makes an <a href with a url parameter'''
d={'url':'http://example.com/more.xoxo','title':'sample url','type':"text/xml",'rel':'help','text':'an example'}
html=xoxo.toXOXO(d)
expectedHTML= '<ol class="xoxo"><li><a href="http://example.com/more.xoxo" title="sample url" rel="help" type="text/xml" >an example</a></li></ol>'
self.assertEqual(html,expectedHTML)
def testSpecialAttributeRoundTripFull(self):
'''check it makes an <a href with a url parameter'''
d={'url':'http://example.com/more.xoxo','title':'sample url','type':"text/xml",'rel':'help','text':'an example'}
html=xoxo.toXOXO(d)
self.assertEqual(d,xoxo.fromXOXO(html))
def testSpecialAttributeRoundTripNoText(self):
'''check it makes an <a href with a url parameter and no text attribute'''
d={'url':'http://example.com/more.xoxo','title':'sample url','type':"text/xml",'rel':'help'}
html=xoxo.toXOXO(d)
self.assertEqual(d,xoxo.fromXOXO(html))
def testSpecialAttributeRoundTripNoTextOrTitle(self):
'''check it makes an <a href with a url parameter and no text or title attribute'''
d={'url':'http://example.com/more.xoxo'}
html=xoxo.toXOXO(d)
self.assertEqual(d,xoxo.fromXOXO(html))
def testAttentionRoundTrip(self):
'''check nested <a> and <dl> and <a> are preserved'''
kmattn='''<ol class="xoxo"><li><a href="http://www.boingboing.net/" title="Boing Boing Blog" >Boing Boing Blog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://boingboing.net/rss.xml" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Boing Boing Blog</dd></dl></li><li><a href="http://www.financialcryptography.com/" title="Financial Cryptography" >Financial Cryptography</a><dl><dt>alturls</dt><dd><ol><li><a href="http://www.financialcryptography.com/mt/index.rdf" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Financial Cryptography</dd></dl></li><li><a href="http://hublog.hubmed.org/" title="HubLog" >HubLog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://hublog.hubmed.org/index.xml" >xmlurl</a></li><li><a href="http://hublog.hubmed.org/foaf.rdf" >foafurl</a></li></ol></dd><dt>description</dt><dd>HubLog</dd></dl></li></ol>''';
d = xoxo.fromXOXO(kmattn)
newattn = xoxo.toXOXO(d)
d2 = xoxo.fromXOXO(newattn)
self.assertEqual(newattn,xoxo.toXOXO(d2))
self.assertEqual(d,d2)
self.assertEqual(kmattn,newattn)
def testUnicodeRoundtrip(self):
'''check unicode characters can go to xoxo and back'''
src=unicode('Tantek \xc3\x87elik and a snowman \xe2\x98\x83','utf-8')
html = xoxo.toXOXO(src)
self.assertEqual(src,xoxo.fromXOXO(html))
def testUtf8Roundtrip(self):
'''check utf8 characters can go to xoxo and back'''
src='Tantek \xc3\x87elik and a snowman \xe2\x98\x83'
html = xoxo.toXOXO(src)
self.assertEqual(src,xoxo.fromXOXO(html).encode('utf-8'))
def testWindows1252Roundtrip(self):
'''check 1252 characters can go to xoxo and back'''
src='This is an evil\xa0space'
html = xoxo.toXOXO(src)
self.assertEqual(src,xoxo.fromXOXO(html).encode('windows-1252'))
def testUrlList(self):
'''handle the mf2 case where url is a list'''
d={'url':['http://example.com/']}
html = xoxo.toXOXO(d)
self.assertEqual(html,'<ol class="xoxo"><li><dl><dt>url</dt><dd><ol><li>http://example.com/</li></ol></dd></dl></li></ol>')
if __name__ == "__main__":
unittest.main()
else:
runner = unittest.TextTestRunner()
suite = unittest.makeSuite(xoxoTestCases,'test')
runner.run(suite)