111
125
def test_repeated_field(self):
112
126
"""Repeated field in rio"""
114
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
128
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
115
129
('a', '1000'), ('b', '2000')]:
117
131
s2 = read_stanza(s.to_lines())
142
156
def test_whitespace_value(self):
143
157
s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
144
158
self.assertEqualDiff(s.to_string(), """\
151
165
s2 = read_stanza(s.to_lines())
152
166
self.assertEquals(s, s2)
167
self.rio_file_stanzas([s])
154
169
def test_quoted(self):
155
170
"""rio quoted string cases"""
156
s = Stanza(q1='"hello"',
171
s = Stanza(q1='"hello"',
158
173
q3='\n\n"for"\n',
159
174
q4='for\n"\nfor',
166
181
s2 = read_stanza(s.to_lines())
167
182
self.assertEquals(s, s2)
183
# apparent bug in read_stanza
184
# s3 = read_stanza(self.stanzas_to_str([s]))
185
# self.assertEquals(s, s3)
169
187
def test_read_empty(self):
170
188
"""Detect end of rio file"""
171
189
s = read_stanza([])
172
190
self.assertEqual(s, None)
173
191
self.assertTrue(s is None)
193
def test_read_nul_byte(self):
194
"""File consisting of a nul byte causes an error."""
195
self.assertRaises(ValueError, read_stanza, ['\0'])
197
def test_read_nul_bytes(self):
198
"""File consisting of many nul bytes causes an error."""
199
self.assertRaises(ValueError, read_stanza, ['\0' * 100])
175
201
def test_read_iter(self):
176
202
"""Read several stanzas from file"""
177
203
tmpf = TemporaryFile()
222
248
self.assertEquals(s, Stanza(name="bar", val='129319'))
223
249
s = read_stanza(tmpf)
224
250
self.assertEquals(s, None)
251
self.check_rio_file(tmpf)
253
def check_rio_file(self, real_file):
255
read_write = rio_file(RioReader(real_file)).read()
257
self.assertEquals(read_write, real_file.read())
260
def stanzas_to_str(stanzas):
261
return rio_file(stanzas).read()
263
def rio_file_stanzas(self, stanzas):
264
new_stanzas = list(RioReader(rio_file(stanzas)))
265
self.assertEqual(new_stanzas, stanzas)
226
267
def test_tricky_quoted(self):
227
268
tmpf = TemporaryFile()
280
322
"""Write empty stanza"""
281
323
l = list(Stanza().to_lines())
282
324
self.assertEquals(l, [])
326
def test_rio_raises_type_error(self):
327
"""TypeError on adding invalid type to Stanza"""
329
self.assertRaises(TypeError, s.add, 'foo', {})
331
def test_rio_raises_type_error_key(self):
332
"""TypeError on adding invalid type to Stanza"""
334
self.assertRaises(TypeError, s.add, 10, {})
336
def test_rio_unicode(self):
337
uni_data = u'\N{KATAKANA LETTER O}'
338
s = Stanza(foo=uni_data)
339
self.assertEquals(s.get('foo'), uni_data)
340
raw_lines = s.to_lines()
341
self.assertEquals(raw_lines,
342
['foo: ' + uni_data.encode('utf-8') + '\n'])
343
new_s = read_stanza(raw_lines)
344
self.assertEquals(new_s.get('foo'), uni_data)
346
def test_rio_to_unicode(self):
347
uni_data = u'\N{KATAKANA LETTER O}'
348
s = Stanza(foo=uni_data)
349
unicode_str = s.to_unicode()
350
self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
351
new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
352
self.assertEqual(uni_data, new_s.get('foo'))
354
def test_nested_rio_unicode(self):
355
uni_data = u'\N{KATAKANA LETTER O}'
356
s = Stanza(foo=uni_data)
357
parent_stanza = Stanza(child=s.to_unicode())
358
raw_lines = parent_stanza.to_lines()
359
self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
362
new_parent = read_stanza(raw_lines)
363
child_text = new_parent.get('child')
364
self.assertEqual(u'foo: %s\n' % uni_data, child_text)
365
new_child = rio.read_stanza_unicode(child_text.splitlines(True))
366
self.assertEqual(uni_data, new_child.get('foo'))
368
def mail_munge(self, lines, dos_nl=True):
371
line = re.sub(' *\n', '\n', line)
373
line = re.sub('([^\r])\n', '\\1\r\n', line)
374
new_lines.append(line)
377
def test_patch_rio(self):
378
stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
379
lines = rio.to_patch_lines(stanza)
381
self.assertContainsRe(line, '^# ')
382
self.assertTrue(72 >= len(line))
383
for line in rio.to_patch_lines(stanza, max_width=12):
384
self.assertTrue(12 >= len(line))
385
new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
387
lines = self.mail_munge(lines)
388
new_stanza = rio.read_patch_stanza(lines)
389
self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
390
self.assertEqual(' '* 255, new_stanza.get('space'))
391
self.assertEqual('#'* 255, new_stanza.get('hash'))
393
def test_patch_rio_linebreaks(self):
394
stanza = Stanza(breaktest='linebreak -/'*30)
395
self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
397
stanza = Stanza(breaktest='linebreak-/'*30)
398
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
400
stanza = Stanza(breaktest='linebreak/'*30)
401
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],