13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for rio serialization
22
22
but this depends on the transport.
26
28
from tempfile import TemporaryFile
31
from bzrlib.tests import TestCase
32
from bzrlib.rio import (
30
from bzrlib.tests import TestCaseInTempDir, TestCase
31
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
41
35
class TestRio(TestCase):
56
50
# these aren't enforced at construction time
57
51
## self.assertRaises(ValueError,
58
52
## Stanza, complex=42 + 3j)
59
## self.assertRaises(ValueError,
53
## self.assertRaises(ValueError,
60
54
## Stanza, several=range(10))
62
56
def test_empty_value(self):
125
119
def test_repeated_field(self):
126
120
"""Repeated field in rio"""
128
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
122
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
129
123
('a', '1000'), ('b', '2000')]:
131
125
s2 = read_stanza(s.to_lines())
189
183
s = read_stanza([])
190
184
self.assertEqual(s, None)
191
185
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])
201
187
def test_read_iter(self):
202
188
"""Read several stanzas from file"""
203
189
tmpf = TemporaryFile()
334
320
self.assertRaises(TypeError, s.add, 10, {})
336
322
def test_rio_unicode(self):
323
# intentionally use cStringIO which doesn't accomodate unencoded unicode objects
324
sio = cStringIO.StringIO()
337
325
uni_data = u'\N{KATAKANA LETTER O}'
338
326
s = Stanza(foo=uni_data)
339
327
self.assertEquals(s.get('foo'), uni_data)
343
331
new_s = read_stanza(raw_lines)
344
332
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],