44
46
self.assertTrue('number' in s)
45
47
self.assertFalse('color' in s)
46
48
self.assertFalse('42' in s)
47
self.assertEquals(list(s.iter_pairs()),
49
self.assertEqual(list(s.iter_pairs()),
48
50
[('name', 'fred'), ('number', '42')])
49
self.assertEquals(s.get('number'), '42')
50
self.assertEquals(s.get('name'), 'fred')
51
self.assertEqual(s.get('number'), '42')
52
self.assertEqual(s.get('name'), 'fred')
52
54
def test_value_checks(self):
53
55
"""rio checks types on construction"""
54
56
# these aren't enforced at construction time
55
57
## self.assertRaises(ValueError,
56
58
## Stanza, complex=42 + 3j)
57
## self.assertRaises(ValueError,
59
## self.assertRaises(ValueError,
58
60
## Stanza, several=range(10))
60
62
def test_empty_value(self):
113
115
s = read_stanza(lines)
114
116
self.assertTrue('revision' in s)
115
117
self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
116
self.assertEquals(list(s.iter_pairs()),
118
self.assertEqual(list(s.iter_pairs()),
117
119
[('revision', 'mbp@sourcefrog.net-123-abc'),
118
120
('timestamp', '1130653962'),
119
121
('timezone', '36000'),
120
122
('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
121
self.assertEquals(len(s), 4)
123
self.assertEqual(len(s), 4)
123
125
def test_repeated_field(self):
124
126
"""Repeated field in rio"""
126
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
128
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
127
129
('a', '1000'), ('b', '2000')]:
129
131
s2 = read_stanza(s.to_lines())
130
self.assertEquals(s, s2)
131
self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
132
self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
132
self.assertEqual(s, s2)
133
self.assertEqual(s.get_all('a'), map(str, [10, 100, 1000]))
134
self.assertEqual(s.get_all('b'), map(str, [20, 200, 2000]))
134
136
def test_backslash(self):
135
137
s = Stanza(q='\\')
136
138
t = s.to_string()
137
139
self.assertEqualDiff(t, 'q: \\\n')
138
140
s2 = read_stanza(s.to_lines())
139
self.assertEquals(s, s2)
141
self.assertEqual(s, s2)
141
143
def test_blank_line(self):
142
144
s = Stanza(none='', one='\n', two='\n\n')
143
145
self.assertEqualDiff(s.to_string(), """\
151
153
s2 = read_stanza(s.to_lines())
152
self.assertEquals(s, s2)
154
self.assertEqual(s, s2)
154
156
def test_whitespace_value(self):
155
157
s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
156
158
self.assertEqualDiff(s.to_string(), """\
163
165
s2 = read_stanza(s.to_lines())
164
self.assertEquals(s, s2)
166
self.assertEqual(s, s2)
165
167
self.rio_file_stanzas([s])
167
169
def test_quoted(self):
168
170
"""rio quoted string cases"""
169
s = Stanza(q1='"hello"',
171
s = Stanza(q1='"hello"',
171
173
q3='\n\n"for"\n',
172
174
q4='for\n"\nfor',
179
181
s2 = read_stanza(s.to_lines())
180
self.assertEquals(s, s2)
182
self.assertEqual(s, s2)
181
183
# apparent bug in read_stanza
182
184
# s3 = read_stanza(self.stanzas_to_str([s]))
183
# self.assertEquals(s, s3)
185
# self.assertEqual(s, s3)
185
187
def test_read_empty(self):
186
188
"""Detect end of rio file"""
187
189
s = read_stanza([])
188
190
self.assertEqual(s, None)
189
191
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])
191
201
def test_read_iter(self):
192
202
"""Read several stanzas from file"""
193
203
tmpf = TemporaryFile()
230
240
s = read_stanza(tmpf)
231
self.assertEquals(s, Stanza(version_header='1'))
241
self.assertEqual(s, Stanza(version_header='1'))
232
242
s = read_stanza(tmpf)
233
self.assertEquals(s, Stanza(name="foo", val='123'))
243
self.assertEqual(s, Stanza(name="foo", val='123'))
234
244
s = read_stanza(tmpf)
235
245
self.assertEqualDiff(s.get('name'), 'quoted')
236
246
self.assertEqualDiff(s.get('address'), ' "Willowglen"\n 42 Wallaby Way\n Sydney')
237
247
s = read_stanza(tmpf)
238
self.assertEquals(s, Stanza(name="bar", val='129319'))
248
self.assertEqual(s, Stanza(name="bar", val='129319'))
239
249
s = read_stanza(tmpf)
240
self.assertEquals(s, None)
250
self.assertEqual(s, None)
241
251
self.check_rio_file(tmpf)
243
253
def check_rio_file(self, real_file):
244
254
real_file.seek(0)
245
255
read_write = rio_file(RioReader(real_file)).read()
246
256
real_file.seek(0)
247
self.assertEquals(read_write, real_file.read())
257
self.assertEqual(read_write, real_file.read())
250
260
def stanzas_to_str(stanzas):
326
336
def test_rio_unicode(self):
327
337
uni_data = u'\N{KATAKANA LETTER O}'
328
338
s = Stanza(foo=uni_data)
329
self.assertEquals(s.get('foo'), uni_data)
339
self.assertEqual(s.get('foo'), uni_data)
330
340
raw_lines = s.to_lines()
331
self.assertEquals(raw_lines,
341
self.assertEqual(raw_lines,
332
342
['foo: ' + uni_data.encode('utf-8') + '\n'])
333
343
new_s = read_stanza(raw_lines)
334
self.assertEquals(new_s.get('foo'), uni_data)
344
self.assertEqual(new_s.get('foo'), uni_data)
336
346
def test_rio_to_unicode(self):
337
347
uni_data = u'\N{KATAKANA LETTER O}'