35
35
def test_stanza(self):
36
36
"""Construct rio stanza in memory"""
37
s = Stanza(number=42, name="fred")
37
s = Stanza(number='42', name="fred")
38
38
self.assertTrue('number' in s)
39
39
self.assertFalse('color' in s)
40
self.assertFalse(42 in s)
40
self.assertFalse('42' in s)
41
41
self.assertEquals(list(s.iter_pairs()),
42
[('name', 'fred'), ('number', 42)])
43
self.assertEquals(s.get('number'), 42)
42
[('name', 'fred'), ('number', '42')])
43
self.assertEquals(s.get('number'), '42')
44
44
self.assertEquals(s.get('name'), 'fred')
46
46
def test_value_checks(self):
60
60
def test_to_lines(self):
61
61
"""Write simple rio stanza to string"""
62
s = Stanza(number=42, name='fred')
62
s = Stanza(number='42', name='fred')
63
63
self.assertEquals(list(s.to_lines()),
67
67
def test_to_file(self):
68
68
"""Write rio to file"""
69
69
tmpf = TemporaryFile()
70
s = Stanza(a_thing='something with "quotes like \\"this\\""', number=42, name='fred')
70
s = Stanza(a_thing='something with "quotes like \\"this\\""', number='42', name='fred')
73
73
self.assertEqualDiff(tmpf.read(), r'''
79
79
def test_multiline_string(self):
80
"""Write rio with multiline string"""
81
tmpf = TemporaryFile()
82
s = Stanza(a=123, motto="war is peace\nfreedom is slavery\nignorance is strength\n",
86
self.assertEqualDiff(tmpf.read(), r'''\
95
def test_multiline_string(self):
96
80
tmpf = TemporaryFile()
97
81
s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
135
119
self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
136
120
self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
138
def test_longint(self):
139
"""rio packing long integers"""
140
s = Stanza(x=-12345678901234567890,
143
s2 = read_stanza(lines)
144
self.assertEquals(s, s2)
146
def test_quoted_0(self):
147
"""Backslash quoted cases"""
122
def test_backslash(self):
148
123
s = Stanza(q='\\')
149
124
t = s.to_string()
150
self.assertEqualDiff(t, 'q "\\\\"\n')
151
s2 = read_stanza(s.to_lines())
152
self.assertEquals(s, s2)
154
def test_quoted_1(self):
155
"""Backslash quoted cases"""
156
s = Stanza(q=r'\"\"')
157
self.assertEqualDiff(s.to_string(), r'q "\\\"\\\""' + '\n')
159
def test_quoted_4(self):
160
s = Stanza(q=r'""""')
162
self.assertEqualDiff(t, r'q "\"\"\"\""' + '\n')
163
s2 = read_stanza(s.to_lines())
164
self.assertEquals(s, s2)
166
def test_quoted_5(self):
167
s = Stanza(q=r'\\\\\"')
169
s2 = read_stanza(s.to_lines())
170
self.assertEquals(s, s2)
172
def test_quoted_6(self):
180
s2 = read_stanza(s.to_lines())
181
self.assertEquals(s2['q'], qval)
183
def test_quoted_7(self):
192
s2 = read_stanza(s.to_lines())
193
self.assertEquals(s2['q'], qval)
195
def test_quoted_8(self):
201
s2 = read_stanza(s.to_lines())
202
self.assertEquals(s2['q'], qval)
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)
204
154
def test_quoted(self):
205
155
"""rio quoted string cases"""
206
156
s = Stanza(q1='"hello"',
226
176
"""Read several stanzas from file"""
227
177
tmpf = TemporaryFile()
238
188
reader = read_stanzas(tmpf)
239
189
read_iter = iter(reader)
240
190
stuff = list(reader)
241
191
self.assertEqual(stuff,
242
[ Stanza(version_header=1),
243
Stanza(name="foo", val=123),
244
Stanza(name="bar", val=129319), ])
192
[ Stanza(version_header='1'),
193
Stanza(name="foo", val='123'),
194
Stanza(name="bar", val='129319'), ])
246
196
def test_read_several(self):
247
197
"""Read several stanzas from file"""
248
198
tmpf = TemporaryFile()
256
address " \\"Willowglen\\"
206
address: "Willowglen"
264
214
s = read_stanza(tmpf)
265
self.assertEquals(s, Stanza(version_header=1))
215
self.assertEquals(s, Stanza(version_header='1'))
266
216
s = read_stanza(tmpf)
267
self.assertEquals(s, Stanza(name="foo", val=123))
217
self.assertEquals(s, Stanza(name="foo", val='123'))
268
218
s = read_stanza(tmpf)
269
219
self.assertEqualDiff(s.get('name'), 'quoted')
270
220
self.assertEqualDiff(s.get('address'), ' "Willowglen"\n 42 Wallaby Way\n Sydney')
271
221
s = read_stanza(tmpf)
272
self.assertEquals(s, Stanza(name="bar", val=129319))
222
self.assertEquals(s, Stanza(name="bar", val='129319'))
273
223
s = read_stanza(tmpf)
274
224
self.assertEquals(s, None)
276
226
def test_tricky_quoted(self):
277
227
tmpf = TemporaryFile()
305
s "backslashes\\\\\\"
309
"""[1:]) # remove initial newline
311
261
expected_vals = ['"one"',
326
276
self.assertEquals(len(stanza), 1)
327
277
self.assertEqualDiff(stanza.get('s'), expected)
329
def test_write_bool(self):
330
"""Write bool to rio"""
331
l = list(Stanza(my_bool=True).to_lines())
332
self.assertEquals(l, ['my_bool 1\n'])
334
279
def test_write_empty_stanza(self):
335
280
"""Write empty stanza"""
336
281
l = list(Stanza().to_lines())