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
def test_read_nul_byte(self):
192
"""File consisting of a nul byte causes an error."""
193
self.assertRaises(ValueError, read_stanza, ['\0'])
195
def test_read_nul_bytes(self):
196
"""File consisting of many nul bytes causes an error."""
197
self.assertRaises(ValueError, read_stanza, ['\0' * 100])
199
175
def test_read_iter(self):
200
176
"""Read several stanzas from file"""
201
177
tmpf = TemporaryFile()
246
222
self.assertEquals(s, Stanza(name="bar", val='129319'))
247
223
s = read_stanza(tmpf)
248
224
self.assertEquals(s, None)
249
self.check_rio_file(tmpf)
251
def check_rio_file(self, real_file):
253
read_write = rio_file(RioReader(real_file)).read()
255
self.assertEquals(read_write, real_file.read())
258
def stanzas_to_str(stanzas):
259
return rio_file(stanzas).read()
261
def rio_file_stanzas(self, stanzas):
262
new_stanzas = list(RioReader(rio_file(stanzas)))
263
self.assertEqual(new_stanzas, stanzas)
265
226
def test_tricky_quoted(self):
266
227
tmpf = TemporaryFile()
320
280
"""Write empty stanza"""
321
281
l = list(Stanza().to_lines())
322
282
self.assertEquals(l, [])
324
def test_rio_raises_type_error(self):
325
"""TypeError on adding invalid type to Stanza"""
327
self.assertRaises(TypeError, s.add, 'foo', {})
329
def test_rio_raises_type_error_key(self):
330
"""TypeError on adding invalid type to Stanza"""
332
self.assertRaises(TypeError, s.add, 10, {})
334
def test_rio_unicode(self):
335
uni_data = u'\N{KATAKANA LETTER O}'
336
s = Stanza(foo=uni_data)
337
self.assertEquals(s.get('foo'), uni_data)
338
raw_lines = s.to_lines()
339
self.assertEquals(raw_lines,
340
['foo: ' + uni_data.encode('utf-8') + '\n'])
341
new_s = read_stanza(raw_lines)
342
self.assertEquals(new_s.get('foo'), uni_data)
344
def test_rio_to_unicode(self):
345
uni_data = u'\N{KATAKANA LETTER O}'
346
s = Stanza(foo=uni_data)
347
unicode_str = s.to_unicode()
348
self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
349
new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
350
self.assertEqual(uni_data, new_s.get('foo'))
352
def test_nested_rio_unicode(self):
353
uni_data = u'\N{KATAKANA LETTER O}'
354
s = Stanza(foo=uni_data)
355
parent_stanza = Stanza(child=s.to_unicode())
356
raw_lines = parent_stanza.to_lines()
357
self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
360
new_parent = read_stanza(raw_lines)
361
child_text = new_parent.get('child')
362
self.assertEqual(u'foo: %s\n' % uni_data, child_text)
363
new_child = rio.read_stanza_unicode(child_text.splitlines(True))
364
self.assertEqual(uni_data, new_child.get('foo'))
366
def mail_munge(self, lines, dos_nl=True):
369
line = re.sub(' *\n', '\n', line)
371
line = re.sub('([^\r])\n', '\\1\r\n', line)
372
new_lines.append(line)
375
def test_patch_rio(self):
376
stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
377
lines = rio.to_patch_lines(stanza)
379
self.assertContainsRe(line, '^# ')
380
self.assertTrue(72 >= len(line))
381
for line in rio.to_patch_lines(stanza, max_width=12):
382
self.assertTrue(12 >= len(line))
383
new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
385
lines = self.mail_munge(lines)
386
new_stanza = rio.read_patch_stanza(lines)
387
self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
388
self.assertEqual(' '* 255, new_stanza.get('space'))
389
self.assertEqual('#'* 255, new_stanza.get('hash'))
391
def test_patch_rio_linebreaks(self):
392
stanza = Stanza(breaktest='linebreak -/'*30)
393
self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
395
stanza = Stanza(breaktest='linebreak-/'*30)
396
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
398
stanza = Stanza(breaktest='linebreak/'*30)
399
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],