1
# Copyright (C) 2005 by 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
27
from tempfile import TemporaryFile
29
from bzrlib.tests import TestCaseInTempDir, TestCase
30
from bzrlib.rio import RioWriter, Stanza, read_stanza, read_stanzas
33
class TestRio(TestCase):
35
def test_stanza(self):
36
"""Construct rio stanza in memory"""
37
s = Stanza(number='42', name="fred")
38
self.assertTrue('number' in s)
39
self.assertFalse('color' in s)
40
self.assertFalse('42' in s)
41
self.assertEquals(list(s.iter_pairs()),
42
[('name', 'fred'), ('number', '42')])
43
self.assertEquals(s.get('number'), '42')
44
self.assertEquals(s.get('name'), 'fred')
46
def test_value_checks(self):
47
"""rio checks types on construction"""
48
# these aren't enforced at construction time
49
## self.assertRaises(ValueError,
50
## Stanza, complex=42 + 3j)
51
## self.assertRaises(ValueError,
52
## Stanza, several=range(10))
54
def test_empty_value(self):
55
"""Serialize stanza with empty field"""
57
self.assertEqualDiff(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.assertEquals(list(s.to_lines()),
67
def test_to_file(self):
68
"""Write rio to file"""
69
tmpf = TemporaryFile()
70
s = Stanza(a_thing='something with "quotes like \\"this\\""', number='42', name='fred')
73
self.assertEqualDiff(tmpf.read(), r'''
74
a_thing: something with "quotes like \"this\""
79
def test_multiline_string(self):
80
tmpf = TemporaryFile()
81
s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
84
self.assertEqualDiff(tmpf.read(), '''\
87
\tignorance is strength
90
s2 = read_stanza(tmpf)
91
self.assertEquals(s, s2)
93
def test_read_stanza(self):
94
"""Load stanza from string"""
96
revision: mbp@sourcefrog.net-123-abc
99
committer: Martin Pool <mbp@test.sourcefrog.net>
101
s = read_stanza(lines)
102
self.assertTrue('revision' in s)
103
self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
104
self.assertEquals(list(s.iter_pairs()),
105
[('revision', 'mbp@sourcefrog.net-123-abc'),
106
('timestamp', '1130653962'),
107
('timezone', '36000'),
108
('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
109
self.assertEquals(len(s), 4)
111
def test_repeated_field(self):
112
"""Repeated field in rio"""
114
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
115
('a', '1000'), ('b', '2000')]:
117
s2 = read_stanza(s.to_lines())
118
self.assertEquals(s, s2)
119
self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
120
self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
122
def test_backslash(self):
125
self.assertEqualDiff(t, 'q: \\\n')
126
s2 = read_stanza(s.to_lines())
127
self.assertEquals(s, s2)
129
def test_blank_line(self):
130
s = Stanza(none='', one='\n', two='\n\n')
131
self.assertEqualDiff(s.to_string(), """\
139
s2 = read_stanza(s.to_lines())
140
self.assertEquals(s, s2)
142
def test_whitespace_value(self):
143
s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
144
self.assertEqualDiff(s.to_string(), """\
151
s2 = read_stanza(s.to_lines())
152
self.assertEquals(s, s2)
154
def test_quoted(self):
155
"""rio quoted string cases"""
156
s = Stanza(q1='"hello"',
166
s2 = read_stanza(s.to_lines())
167
self.assertEquals(s, s2)
169
def test_read_empty(self):
170
"""Detect end of rio file"""
172
self.assertEqual(s, None)
173
self.assertTrue(s is None)
175
def test_read_iter(self):
176
"""Read several stanzas from file"""
177
tmpf = TemporaryFile()
188
reader = read_stanzas(tmpf)
189
read_iter = iter(reader)
191
self.assertEqual(stuff,
192
[ Stanza(version_header='1'),
193
Stanza(name="foo", val='123'),
194
Stanza(name="bar", val='129319'), ])
196
def test_read_several(self):
197
"""Read several stanzas from file"""
198
tmpf = TemporaryFile()
206
address: "Willowglen"
214
s = read_stanza(tmpf)
215
self.assertEquals(s, Stanza(version_header='1'))
216
s = read_stanza(tmpf)
217
self.assertEquals(s, Stanza(name="foo", val='123'))
218
s = read_stanza(tmpf)
219
self.assertEqualDiff(s.get('name'), 'quoted')
220
self.assertEqualDiff(s.get('address'), ' "Willowglen"\n 42 Wallaby Way\n Sydney')
221
s = read_stanza(tmpf)
222
self.assertEquals(s, Stanza(name="bar", val='129319'))
223
s = read_stanza(tmpf)
224
self.assertEquals(s, None)
226
def test_tricky_quoted(self):
227
tmpf = TemporaryFile()
261
expected_vals = ['"one"',
274
for expected in expected_vals:
275
stanza = read_stanza(tmpf)
276
self.assertEquals(len(stanza), 1)
277
self.assertEqualDiff(stanza.get('s'), expected)
279
def test_write_empty_stanza(self):
280
"""Write empty stanza"""
281
l = list(Stanza().to_lines())
282
self.assertEquals(l, [])