~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Martin Pool
  • Date: 2005-11-28 08:35:56 UTC
  • mto: (1185.33.61 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1518.
  • Revision ID: mbp@sourcefrog.net-20051128083556-5a3d8423b567599c
Finish rio format and tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
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')
45
45
 
46
46
    def test_value_checks(self):
59
59
 
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()),
64
64
                ['name: fred\n',
65
65
                 'number: 42\n'])
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')
71
71
        s.write(tmpf)
72
72
        tmpf.seek(0)
73
73
        self.assertEqualDiff(tmpf.read(), r'''
77
77
'''[1:])
78
78
 
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",
83
 
                   charlie_horse=456)
84
 
        s.write(tmpf)
85
 
        tmp.seek(0)
86
 
        self.assertEqualDiff(tmpf.read(), r'''\
87
 
            a 123
88
 
        motto "war is peace
89
 
freedom is slavery
90
 
ignorance is strength
91
 
"
92
 
charlie_horse 456
93
 
''')
94
 
 
95
 
    def test_multiline_string(self):
96
80
        tmpf = TemporaryFile()
97
81
        s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
98
82
        s.write(tmpf)
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]))
137
121
 
138
 
    def test_longint(self):
139
 
        """rio packing long integers"""
140
 
        s = Stanza(x=-12345678901234567890,
141
 
                   y=1<<100)
142
 
        lines = s.to_lines()
143
 
        s2 = read_stanza(lines)
144
 
        self.assertEquals(s, s2)
145
 
 
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)
153
 
 
154
 
    def test_quoted_1(self):
155
 
        """Backslash quoted cases"""
156
 
        s = Stanza(q=r'\"\"')
157
 
        self.assertEqualDiff(s.to_string(), r'q "\\\"\\\""' + '\n')
158
 
 
159
 
    def test_quoted_4(self):
160
 
        s = Stanza(q=r'""""')
161
 
        t = s.to_string()
162
 
        self.assertEqualDiff(t, r'q "\"\"\"\""' + '\n')
163
 
        s2 = read_stanza(s.to_lines())
164
 
        self.assertEquals(s, s2)
165
 
 
166
 
    def test_quoted_5(self):
167
 
        s = Stanza(q=r'\\\\\"')
168
 
        t = s.to_string()
169
 
        s2 = read_stanza(s.to_lines())
170
 
        self.assertEquals(s, s2)
171
 
 
172
 
    def test_quoted_6(self):
173
 
        qval = r'''
174
 
                "
175
 
                \"
176
 
'''
177
 
        s = Stanza(q=qval)
178
 
        t = s.to_string()
179
 
        self.log(t)
180
 
        s2 = read_stanza(s.to_lines())
181
 
        self.assertEquals(s2['q'], qval)
182
 
        
183
 
    def test_quoted_7(self):
184
 
        qval = r'''
185
 
                "
186
 
                \"
187
 
                \\"
188
 
trailing stuff'''
189
 
        s = Stanza(q=qval)
190
 
        t = s.to_string()
191
 
        self.log(t)
192
 
        s2 = read_stanza(s.to_lines())
193
 
        self.assertEquals(s2['q'], qval)
194
 
        
195
 
    def test_quoted_8(self):
196
 
        qval = r'''trailing
197
 
        quote"'''
198
 
        s = Stanza(q=qval)
199
 
        t = s.to_string()
200
 
        self.log(t)
201
 
        s2 = read_stanza(s.to_lines())
202
 
        self.assertEquals(s2['q'], qval)
203
 
        
 
125
        self.assertEqualDiff(t, 'q: \\\n')
 
126
        s2 = read_stanza(s.to_lines())
 
127
        self.assertEquals(s, s2)
 
128
 
 
129
    def test_blank_line(self):
 
130
        s = Stanza(none='', one='\n', two='\n\n')
 
131
        self.assertEqualDiff(s.to_string(), """\
 
132
none: 
 
133
one: 
 
134
\t
 
135
two: 
 
136
\t
 
137
\t
 
138
""")
 
139
        s2 = read_stanza(s.to_lines())
 
140
        self.assertEquals(s, s2)
 
141
 
 
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(), """\
 
145
combo: 
 
146
\t\t\t
 
147
\t
 
148
space:  
 
149
tabs: \t\t\t
 
150
""")
 
151
        s2 = read_stanza(s.to_lines())
 
152
        self.assertEquals(s, s2)
 
153
 
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()
228
178
        tmpf.write("""\
229
 
version_header 1
230
 
 
231
 
name "foo"
232
 
val 123
233
 
 
234
 
name "bar"
235
 
val 129319
 
179
version_header: 1
 
180
 
 
181
name: foo
 
182
val: 123
 
183
 
 
184
name: bar
 
185
val: 129319
236
186
""")
237
187
        tmpf.seek(0)
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'), ])
245
195
 
246
196
    def test_read_several(self):
247
197
        """Read several stanzas from file"""
248
198
        tmpf = TemporaryFile()
249
199
        tmpf.write("""\
250
 
version_header 1
251
 
 
252
 
name "foo"
253
 
val 123
254
 
 
255
 
name "quoted"
256
 
address "  \\"Willowglen\\"
257
 
  42 Wallaby Way
258
 
  Sydney"
259
 
 
260
 
name "bar"
261
 
val 129319
 
200
version_header: 1
 
201
 
 
202
name: foo
 
203
val: 123
 
204
 
 
205
name: quoted
 
206
address:   "Willowglen"
 
207
\t  42 Wallaby Way
 
208
\t  Sydney
 
209
 
 
210
name: bar
 
211
val: 129319
262
212
""")
263
213
        tmpf.seek(0)
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)
275
225
 
276
226
    def test_tricky_quoted(self):
277
227
        tmpf = TemporaryFile()
278
 
        tmpf.write(r"""
279
 
s "\"one\""
280
 
 
281
 
s "
282
 
\"one\"
283
 
"
284
 
 
285
 
s "\""
286
 
 
287
 
s "\"\""
288
 
 
289
 
s "\"\"\""
290
 
 
291
 
s "
292
 
"
293
 
 
294
 
s "\\"
295
 
 
296
 
s "
297
 
\\
298
 
\\\\
299
 
"
300
 
 
301
 
s "word\\"
302
 
 
303
 
s "quote\""
304
 
 
305
 
s "backslashes\\\\\\"
306
 
 
307
 
s "both\\\""
308
 
 
309
 
"""[1:]) # remove initial newline
 
228
        tmpf.write('''\
 
229
s: "one"
 
230
 
 
231
s: 
 
232
\t"one"
 
233
\t
 
234
 
 
235
s: "
 
236
 
 
237
s: ""
 
238
 
 
239
s: """
 
240
 
 
241
s: 
 
242
\t
 
243
 
 
244
s: \\
 
245
 
 
246
s: 
 
247
\t\\
 
248
\t\\\\
 
249
\t
 
250
 
 
251
s: word\\
 
252
 
 
253
s: quote"
 
254
 
 
255
s: backslashes\\\\\\
 
256
 
 
257
s: both\\\"
 
258
 
 
259
''')
310
260
        tmpf.seek(0)
311
261
        expected_vals = ['"one"',
312
262
            '\n"one"\n',
326
276
            self.assertEquals(len(stanza), 1)
327
277
            self.assertEqualDiff(stanza.get('s'), expected)
328
278
 
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'])
333
 
 
334
279
    def test_write_empty_stanza(self):
335
280
        """Write empty stanza"""
336
281
        l = list(Stanza().to_lines())