1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005, 2006 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
but this depends on the transport.
27
28
from tempfile import TemporaryFile
29
33
from bzrlib.tests import TestCaseInTempDir, TestCase
30
from bzrlib.rio import RioWriter, Stanza, read_stanza, read_stanzas
34
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
33
38
class TestRio(TestCase):
72
def test_as_dict(self):
73
"""Convert rio Stanza to dictionary"""
74
s = Stanza(number='42', name='fred')
76
self.assertEquals(sd, dict(number='42', name='fred'))
67
78
def test_to_file(self):
68
79
"""Write rio to file"""
69
80
tmpf = TemporaryFile()
166
178
s2 = read_stanza(s.to_lines())
167
179
self.assertEquals(s, s2)
180
# apparent bug in read_stanza
181
# s3 = read_stanza(self.stanzas_to_str([s]))
182
# self.assertEquals(s, s3)
169
184
def test_read_empty(self):
170
185
"""Detect end of rio file"""
222
237
self.assertEquals(s, Stanza(name="bar", val='129319'))
223
238
s = read_stanza(tmpf)
224
239
self.assertEquals(s, None)
240
self.check_rio_file(tmpf)
242
def check_rio_file(self, real_file):
244
read_write = rio_file(RioReader(real_file)).read()
246
self.assertEquals(read_write, real_file.read())
249
def stanzas_to_str(stanzas):
250
return rio_file(stanzas).read()
252
def rio_file_stanzas(self, stanzas):
253
new_stanzas = list(RioReader(rio_file(stanzas)))
254
self.assertEqual(new_stanzas, stanzas)
226
256
def test_tricky_quoted(self):
227
257
tmpf = TemporaryFile()
274
304
for expected in expected_vals:
275
305
stanza = read_stanza(tmpf)
306
self.rio_file_stanzas([stanza])
276
307
self.assertEquals(len(stanza), 1)
277
308
self.assertEqualDiff(stanza.get('s'), expected)
280
311
"""Write empty stanza"""
281
312
l = list(Stanza().to_lines())
282
313
self.assertEquals(l, [])
315
def test_rio_raises_type_error(self):
316
"""TypeError on adding invalid type to Stanza"""
318
self.assertRaises(TypeError, s.add, 'foo', {})
320
def test_rio_raises_type_error_key(self):
321
"""TypeError on adding invalid type to Stanza"""
323
self.assertRaises(TypeError, s.add, 10, {})
325
def test_rio_unicode(self):
326
uni_data = u'\N{KATAKANA LETTER O}'
327
s = Stanza(foo=uni_data)
328
self.assertEquals(s.get('foo'), uni_data)
329
raw_lines = s.to_lines()
330
self.assertEquals(raw_lines,
331
['foo: ' + uni_data.encode('utf-8') + '\n'])
332
new_s = read_stanza(raw_lines)
333
self.assertEquals(new_s.get('foo'), uni_data)
335
def test_rio_to_unicode(self):
336
uni_data = u'\N{KATAKANA LETTER O}'
337
s = Stanza(foo=uni_data)
338
unicode_str = s.to_unicode()
339
self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
340
new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
341
self.assertEqual(uni_data, new_s.get('foo'))
343
def test_nested_rio_unicode(self):
344
uni_data = u'\N{KATAKANA LETTER O}'
345
s = Stanza(foo=uni_data)
346
parent_stanza = Stanza(child=s.to_unicode())
347
raw_lines = parent_stanza.to_lines()
348
self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
351
new_parent = read_stanza(raw_lines)
352
child_text = new_parent.get('child')
353
self.assertEqual(u'foo: %s\n' % uni_data, child_text)
354
new_child = rio.read_stanza_unicode(child_text.splitlines(True))
355
self.assertEqual(uni_data, new_child.get('foo'))