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