123
111
def test_repeated_field(self):
124
112
"""Repeated field in rio"""
126
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
114
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
127
115
('a', '1000'), ('b', '2000')]:
129
117
s2 = read_stanza(s.to_lines())
154
142
def test_whitespace_value(self):
155
143
s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
156
144
self.assertEqualDiff(s.to_string(), """\
163
151
s2 = read_stanza(s.to_lines())
164
152
self.assertEquals(s, s2)
165
self.rio_file_stanzas([s])
167
154
def test_quoted(self):
168
155
"""rio quoted string cases"""
169
s = Stanza(q1='"hello"',
156
s = Stanza(q1='"hello"',
171
158
q3='\n\n"for"\n',
172
159
q4='for\n"\nfor',
179
166
s2 = read_stanza(s.to_lines())
180
167
self.assertEquals(s, s2)
181
# apparent bug in read_stanza
182
# s3 = read_stanza(self.stanzas_to_str([s]))
183
# self.assertEquals(s, s3)
185
169
def test_read_empty(self):
186
170
"""Detect end of rio file"""
187
171
s = read_stanza([])
188
172
self.assertEqual(s, None)
189
173
self.assertTrue(s is None)
191
175
def test_read_iter(self):
192
176
"""Read several stanzas from file"""
193
177
tmpf = TemporaryFile()
238
222
self.assertEquals(s, Stanza(name="bar", val='129319'))
239
223
s = read_stanza(tmpf)
240
224
self.assertEquals(s, None)
241
self.check_rio_file(tmpf)
243
def check_rio_file(self, real_file):
245
read_write = rio_file(RioReader(real_file)).read()
247
self.assertEquals(read_write, real_file.read())
250
def stanzas_to_str(stanzas):
251
return rio_file(stanzas).read()
253
def rio_file_stanzas(self, stanzas):
254
new_stanzas = list(RioReader(rio_file(stanzas)))
255
self.assertEqual(new_stanzas, stanzas)
257
226
def test_tricky_quoted(self):
258
227
tmpf = TemporaryFile()
312
280
"""Write empty stanza"""
313
281
l = list(Stanza().to_lines())
314
282
self.assertEquals(l, [])
316
def test_rio_raises_type_error(self):
317
"""TypeError on adding invalid type to Stanza"""
319
self.assertRaises(TypeError, s.add, 'foo', {})
321
def test_rio_raises_type_error_key(self):
322
"""TypeError on adding invalid type to Stanza"""
324
self.assertRaises(TypeError, s.add, 10, {})
326
def test_rio_unicode(self):
327
uni_data = u'\N{KATAKANA LETTER O}'
328
s = Stanza(foo=uni_data)
329
self.assertEquals(s.get('foo'), uni_data)
330
raw_lines = s.to_lines()
331
self.assertEquals(raw_lines,
332
['foo: ' + uni_data.encode('utf-8') + '\n'])
333
new_s = read_stanza(raw_lines)
334
self.assertEquals(new_s.get('foo'), uni_data)
336
def test_rio_to_unicode(self):
337
uni_data = u'\N{KATAKANA LETTER O}'
338
s = Stanza(foo=uni_data)
339
unicode_str = s.to_unicode()
340
self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
341
new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
342
self.assertEqual(uni_data, new_s.get('foo'))
344
def test_nested_rio_unicode(self):
345
uni_data = u'\N{KATAKANA LETTER O}'
346
s = Stanza(foo=uni_data)
347
parent_stanza = Stanza(child=s.to_unicode())
348
raw_lines = parent_stanza.to_lines()
349
self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
352
new_parent = read_stanza(raw_lines)
353
child_text = new_parent.get('child')
354
self.assertEqual(u'foo: %s\n' % uni_data, child_text)
355
new_child = rio.read_stanza_unicode(child_text.splitlines(True))
356
self.assertEqual(uni_data, new_child.get('foo'))
358
def mail_munge(self, lines, dos_nl=True):
361
line = re.sub(' *\n', '\n', line)
363
line = re.sub('([^\r])\n', '\\1\r\n', line)
364
new_lines.append(line)
367
def test_patch_rio(self):
368
stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
369
lines = rio.to_patch_lines(stanza)
371
self.assertContainsRe(line, '^# ')
372
self.assertTrue(72 >= len(line))
373
for line in rio.to_patch_lines(stanza, max_width=12):
374
self.assertTrue(12 >= len(line))
375
new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
377
lines = self.mail_munge(lines)
378
new_stanza = rio.read_patch_stanza(lines)
379
self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
380
self.assertEqual(' '* 255, new_stanza.get('space'))
381
self.assertEqual('#'* 255, new_stanza.get('hash'))
383
def test_patch_rio_linebreaks(self):
384
stanza = Stanza(breaktest='linebreak -/'*30)
385
self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
387
stanza = Stanza(breaktest='linebreak-/'*30)
388
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
390
stanza = Stanza(breaktest='linebreak/'*30)
391
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],