1
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
17
"""Tests for rio serialization
19
A simple, reproducible structured IO format.
21
rio itself works in Unicode strings. It is typically encoded to UTF-8,
22
but this depends on the transport.
26
from tempfile import TemporaryFile
31
from breezy.tests import TestCase
32
from breezy.rio import (
41
class TestRio(TestCase):
43
def test_stanza(self):
44
"""Construct rio stanza in memory"""
45
s = Stanza(number='42', name="fred")
46
self.assertTrue('number' in s)
47
self.assertFalse('color' in s)
48
self.assertFalse('42' in s)
49
self.assertEqual(list(s.iter_pairs()),
50
[('name', 'fred'), ('number', '42')])
51
self.assertEqual(s.get('number'), '42')
52
self.assertEqual(s.get('name'), 'fred')
54
def test_empty_value(self):
55
"""Serialize stanza with empty field"""
57
self.assertEquals(s.to_string(),
60
def test_to_lines(self):
61
"""Write simple rio stanza to string"""
62
s = Stanza(number='42', name='fred')
63
self.assertEqual(list(s.to_lines()),
67
def test_as_dict(self):
68
"""Convert rio Stanza to dictionary"""
69
s = Stanza(number='42', name='fred')
71
self.assertEqual(sd, dict(number='42', name='fred'))
73
def test_to_file(self):
74
"""Write rio to file"""
75
tmpf = TemporaryFile()
76
s = Stanza(a_thing='something with "quotes like \\"this\\""',
77
number='42', name='fred')
80
self.assertEqual(tmpf.read(), b'''\
81
a_thing: something with "quotes like \\"this\\""
86
def test_multiline_string(self):
87
tmpf = TemporaryFile()
89
motto="war is peace\nfreedom is slavery\nignorance is strength")
92
self.assertEqual(tmpf.read(), b'''\
95
\tignorance is strength
98
s2 = read_stanza(tmpf)
99
self.assertEqual(s, s2)
101
def test_read_stanza(self):
102
"""Load stanza from string"""
104
revision: mbp@sourcefrog.net-123-abc
105
timestamp: 1130653962
107
committer: Martin Pool <mbp@test.sourcefrog.net>
109
s = read_stanza(lines)
110
self.assertTrue('revision' in s)
111
self.assertEqual(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
112
self.assertEqual(list(s.iter_pairs()),
113
[('revision', 'mbp@sourcefrog.net-123-abc'),
114
('timestamp', '1130653962'),
115
('timezone', '36000'),
116
('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
117
self.assertEqual(len(s), 4)
119
def test_repeated_field(self):
120
"""Repeated field in rio"""
122
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
123
('a', '1000'), ('b', '2000')]:
125
s2 = read_stanza(s.to_lines())
126
self.assertEqual(s, s2)
127
self.assertEqual(s.get_all('a'), ['10', '100', '1000'])
128
self.assertEqual(s.get_all('b'), ['20', '200', '2000'])
130
def test_backslash(self):
133
self.assertEqual(t, b'q: \\\n')
134
s2 = read_stanza(s.to_lines())
135
self.assertEqual(s, s2)
137
def test_blank_line(self):
138
s = Stanza(none='', one='\n', two='\n\n')
139
self.assertEqual(s.to_string(), b"""\
147
s2 = read_stanza(s.to_lines())
148
self.assertEqual(s, s2)
150
def test_whitespace_value(self):
151
s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
152
self.assertEqual(s.to_string(), b"""\
159
s2 = read_stanza(s.to_lines())
160
self.assertEqual(s, s2)
161
self.rio_file_stanzas([s])
163
def test_quoted(self):
164
"""rio quoted string cases"""
165
s = Stanza(q1='"hello"',
175
s2 = read_stanza(s.to_lines())
176
self.assertEqual(s, s2)
177
# apparent bug in read_stanza
178
# s3 = read_stanza(self.stanzas_to_str([s]))
179
# self.assertEqual(s, s3)
181
def test_read_empty(self):
182
"""Detect end of rio file"""
184
self.assertEqual(s, None)
185
self.assertTrue(s is None)
187
def test_read_nul_byte(self):
188
"""File consisting of a nul byte causes an error."""
189
self.assertRaises(ValueError, read_stanza, [b'\0'])
191
def test_read_nul_bytes(self):
192
"""File consisting of many nul bytes causes an error."""
193
self.assertRaises(ValueError, read_stanza, [b'\0' * 100])
195
def test_read_iter(self):
196
"""Read several stanzas from file"""
197
tmpf = TemporaryFile()
208
reader = read_stanzas(tmpf)
209
read_iter = iter(reader)
211
self.assertEqual(stuff,
212
[Stanza(version_header='1'),
213
Stanza(name="foo", val='123'),
214
Stanza(name="bar", val='129319'), ])
216
def test_read_several(self):
217
"""Read several stanzas from file"""
218
tmpf = TemporaryFile()
226
address: "Willowglen"
234
s = read_stanza(tmpf)
235
self.assertEqual(s, Stanza(version_header='1'))
236
s = read_stanza(tmpf)
237
self.assertEqual(s, Stanza(name="foo", val='123'))
238
s = read_stanza(tmpf)
239
self.assertEqual(s.get('name'), 'quoted')
241
s.get('address'), ' "Willowglen"\n 42 Wallaby Way\n Sydney')
242
s = read_stanza(tmpf)
243
self.assertEqual(s, Stanza(name="bar", val='129319'))
244
s = read_stanza(tmpf)
245
self.assertEqual(s, None)
246
self.check_rio_file(tmpf)
248
def check_rio_file(self, real_file):
250
read_write = rio_file(RioReader(real_file)).read()
252
self.assertEqual(read_write, real_file.read())
255
def stanzas_to_str(stanzas):
256
return rio_file(stanzas).read()
258
def rio_file_stanzas(self, stanzas):
259
new_stanzas = list(RioReader(rio_file(stanzas)))
260
self.assertEqual(new_stanzas, stanzas)
262
def test_tricky_quoted(self):
263
tmpf = TemporaryFile()
297
expected_vals = ['"one"',
310
for expected in expected_vals:
311
stanza = read_stanza(tmpf)
312
self.rio_file_stanzas([stanza])
313
self.assertEqual(len(stanza), 1)
314
self.assertEqual(stanza.get('s'), expected)
316
def test_write_empty_stanza(self):
317
"""Write empty stanza"""
318
l = list(Stanza().to_lines())
319
self.assertEqual(l, [])
321
def test_rio_raises_type_error(self):
322
"""TypeError on adding invalid type to Stanza"""
324
self.assertRaises(TypeError, s.add, 'foo', {})
326
def test_rio_raises_type_error_key(self):
327
"""TypeError on adding invalid type to Stanza"""
329
self.assertRaises(TypeError, s.add, 10, {})
331
def test_rio_unicode(self):
332
uni_data = u'\N{KATAKANA LETTER O}'
333
s = Stanza(foo=uni_data)
334
self.assertEqual(s.get('foo'), uni_data)
335
raw_lines = s.to_lines()
336
self.assertEqual(raw_lines,
337
[b'foo: ' + uni_data.encode('utf-8') + b'\n'])
338
new_s = read_stanza(raw_lines)
339
self.assertEqual(new_s.get('foo'), uni_data)
341
def test_rio_to_unicode(self):
342
uni_data = u'\N{KATAKANA LETTER O}'
343
s = Stanza(foo=uni_data)
344
unicode_str = s.to_unicode()
345
self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
346
new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
347
self.assertEqual(uni_data, new_s.get('foo'))
349
def test_nested_rio_unicode(self):
350
uni_data = u'\N{KATAKANA LETTER O}'
351
s = Stanza(foo=uni_data)
352
parent_stanza = Stanza(child=s.to_unicode())
353
raw_lines = parent_stanza.to_lines()
354
self.assertEqual([b'child: foo: ' + uni_data.encode('utf-8') + b'\n',
357
new_parent = read_stanza(raw_lines)
358
child_text = new_parent.get('child')
359
self.assertEqual(u'foo: %s\n' % uni_data, child_text)
360
new_child = rio.read_stanza_unicode(child_text.splitlines(True))
361
self.assertEqual(uni_data, new_child.get('foo'))
363
def mail_munge(self, lines, dos_nl=True):
366
line = re.sub(b' *\n', b'\n', line)
368
line = re.sub(b'([^\r])\n', b'\\1\r\n', line)
369
new_lines.append(line)
372
def test_patch_rio(self):
373
stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
374
lines = rio.to_patch_lines(stanza)
376
self.assertContainsRe(line, b'^# ')
377
self.assertTrue(72 >= len(line))
378
for line in rio.to_patch_lines(stanza, max_width=12):
379
self.assertTrue(12 >= len(line))
380
new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
382
lines = self.mail_munge(lines)
383
new_stanza = rio.read_patch_stanza(lines)
384
self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
385
self.assertEqual(' ' * 255, new_stanza.get('space'))
386
self.assertEqual('#' * 255, new_stanza.get('hash'))
388
def test_patch_rio_linebreaks(self):
389
stanza = Stanza(breaktest='linebreak -/' * 30)
390
self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
392
stanza = Stanza(breaktest='linebreak-/' * 30)
393
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
395
stanza = Stanza(breaktest='linebreak/' * 30)
396
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],