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 basic_io serialization
19
A simple, reproducible structured IO format.
21
basic_io 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.selftest import TestCaseInTempDir, TestCase
30
from bzrlib.basicio import BasicWriter, Stanza, read_stanza, read_stanzas
33
class TestBasicIO(TestCase):
35
def test_stanza(self):
36
"""Construct basic_io 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
"""basic_io 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_to_lines(self):
55
"""Write simple basic_io stanza to string"""
56
s = Stanza(number=42, name='fred')
57
self.assertEquals(list(s.to_lines()),
61
def test_to_file(self):
62
"""Write basic_io to file"""
63
tmpf = TemporaryFile()
64
s = Stanza(a_thing='something with "quotes like \\"this\\""', number=42, name='fred')
67
self.assertEqualDiff(tmpf.read(), r'''
68
a_thing "something with \"quotes like \\\"this\\\"\""
73
def test_multiline_string(self):
74
"""Write basic_io with multiline string"""
75
tmpf = TemporaryFile()
76
s = Stanza(a=123, motto="war is peace\nfreedom is slavery\nignorance is strength\n",
80
self.assertEqualDiff(tmpf.read(), r'''\
82
motto "war is peace\nfreedom is slavery\nignorance is strength\n"
86
def test_multiline_string(self):
87
tmpf = TemporaryFile()
88
s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
91
self.assertEqualDiff(tmpf.read(), '''\
92
motto "war is peace\\nfreedom is slavery\\nignorance is strength"
95
s2 = read_stanza(tmpf)
96
self.assertEquals(s, s2)
98
def test_read_stanza(self):
99
"""Load stanza from string"""
101
revision "mbp@sourcefrog.net-123-abc"
104
committer "Martin Pool <mbp@test.sourcefrog.net>"
106
s = read_stanza(lines)
107
self.assertTrue('revision' in s)
108
self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
109
self.assertEquals(list(s.iter_pairs()),
110
[('revision', 'mbp@sourcefrog.net-123-abc'),
111
('timestamp', 1130653962),
113
('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
114
self.assertEquals(len(s), 4)
116
def test_repeated_field(self):
117
"""Repeated field in basic_io"""
119
for k, v in [('a', 10), ('b', 20), ('a', 100), ('b', 200), ('a', 1000), ('b', 2000)]:
121
s2 = read_stanza(s.to_lines())
122
self.assertEquals(s, s2)
123
self.assertEquals(s.get_all('a'), [10, 100, 1000])
124
self.assertEquals(s.get_all('b'), [20, 200, 2000])
126
def test_longint(self):
127
"""basic_io packing long integers"""
128
s = Stanza(x=-12345678901234567890,
131
s2 = read_stanza(lines)
132
self.assertEquals(s, s2)
134
def test_quoted_0(self):
135
"""Backslash quoted cases"""
138
self.assertEqualDiff(t, 'q "\\\\"\n')
139
s2 = read_stanza(s.to_lines())
140
self.assertEquals(s, s2)
142
def test_quoted_1(self):
143
"""Backslash quoted cases"""
144
s = Stanza(q=r'\"\"')
145
self.assertEqualDiff(s.to_string(), r'q "\\\"\\\""' + '\n')
147
def test_quoted_4(self):
148
s = Stanza(q=r'""""')
150
self.assertEqualDiff(t, r'q "\"\"\"\""' + '\n')
151
s2 = read_stanza(s.to_lines())
152
self.assertEquals(s, s2)
154
def test_quoted_5(self):
155
s = Stanza(q=r'\\\\\"')
157
s2 = read_stanza(s.to_lines())
158
self.assertEquals(s, s2)
160
def test_quoted(self):
161
"""basic_io quoted string cases"""
162
s = Stanza(q1='"hello"',
172
s2 = read_stanza(s.to_lines())
173
self.assertEquals(s, s2)
175
def test_read_empty(self):
176
"""Detect end of basic_io file"""
178
self.assertEqual(s, None)
179
self.assertTrue(s is None)
181
def test_read_iter(self):
182
"""Read several stanzas from file"""
183
tmpf = TemporaryFile()
194
reader = read_stanzas(tmpf)
195
read_iter = iter(reader)
197
self.assertEqual(stuff,
198
[ Stanza(version_header=1),
199
Stanza(name="foo", val=123),
200
Stanza(name="bar", val=129319), ])
202
def test_read_several(self):
203
"""Read several stanzas from file"""
204
tmpf = TemporaryFile()
215
s = read_stanza(tmpf)
216
self.assertEquals(s, Stanza(version_header=1))
217
s = read_stanza(tmpf)
218
self.assertEquals(s, Stanza(name="foo", val=123))
219
s = read_stanza(tmpf)
220
self.assertEquals(s, Stanza(name="bar", val=129319))
221
s = read_stanza(tmpf)
222
self.assertEquals(s, None)
224
def test_write_bool(self):
225
"""Write bool to basic_io"""
226
l = list(Stanza(my_bool=True).to_lines())
227
self.assertEquals(l, ['my_bool 1\n'])