/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2016 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for rio serialization
18
18
 
22
22
but this depends on the transport.
23
23
"""
24
24
 
25
 
import re
 
25
import cStringIO
 
26
import os
 
27
import sys
26
28
from tempfile import TemporaryFile
27
29
 
28
 
from breezy import (
29
 
    rio,
30
 
    )
31
 
from breezy.tests import TestCase
32
 
from breezy.rio import (
33
 
    RioReader,
34
 
    Stanza,
35
 
    read_stanza,
36
 
    read_stanzas,
37
 
    rio_file,
38
 
    )
 
30
from bzrlib.tests import TestCaseInTempDir, TestCase
 
31
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
 
32
                        RioReader)
39
33
 
40
34
 
41
35
class TestRio(TestCase):
46
40
        self.assertTrue('number' in s)
47
41
        self.assertFalse('color' in s)
48
42
        self.assertFalse('42' in s)
49
 
        self.assertEqual(list(s.iter_pairs()),
50
 
                         [('name', 'fred'), ('number', '42')])
51
 
        self.assertEqual(s.get('number'), '42')
52
 
        self.assertEqual(s.get('name'), 'fred')
 
43
        self.assertEquals(list(s.iter_pairs()),
 
44
                [('name', 'fred'), ('number', '42')])
 
45
        self.assertEquals(s.get('number'), '42')
 
46
        self.assertEquals(s.get('name'), 'fred')
 
47
 
 
48
    def test_value_checks(self):
 
49
        """rio checks types on construction"""
 
50
        # these aren't enforced at construction time
 
51
        ## self.assertRaises(ValueError,
 
52
        ##        Stanza, complex=42 + 3j)
 
53
        ## self.assertRaises(ValueError, 
 
54
        ##        Stanza, several=range(10))
53
55
 
54
56
    def test_empty_value(self):
55
57
        """Serialize stanza with empty field"""
56
58
        s = Stanza(empty='')
57
 
        self.assertEquals(s.to_string(),
58
 
                          b"empty: \n")
 
59
        self.assertEqualDiff(s.to_string(),
 
60
                "empty: \n")
59
61
 
60
62
    def test_to_lines(self):
61
63
        """Write simple rio stanza to string"""
62
64
        s = Stanza(number='42', name='fred')
63
 
        self.assertEqual(list(s.to_lines()),
64
 
                         [b'name: fred\n',
65
 
                          b'number: 42\n'])
 
65
        self.assertEquals(list(s.to_lines()),
 
66
                ['name: fred\n',
 
67
                 'number: 42\n'])
66
68
 
67
69
    def test_as_dict(self):
68
70
        """Convert rio Stanza to dictionary"""
69
71
        s = Stanza(number='42', name='fred')
70
72
        sd = s.as_dict()
71
 
        self.assertEqual(sd, dict(number='42', name='fred'))
 
73
        self.assertEquals(sd, dict(number='42', name='fred'))
72
74
 
73
75
    def test_to_file(self):
74
76
        """Write rio to file"""
75
77
        tmpf = TemporaryFile()
76
 
        s = Stanza(a_thing='something with "quotes like \\"this\\""',
77
 
                   number='42', name='fred')
 
78
        s = Stanza(a_thing='something with "quotes like \\"this\\""', number='42', name='fred')
78
79
        s.write(tmpf)
79
80
        tmpf.seek(0)
80
 
        self.assertEqual(tmpf.read(), b'''\
81
 
a_thing: something with "quotes like \\"this\\""
 
81
        self.assertEqualDiff(tmpf.read(), r'''
 
82
a_thing: something with "quotes like \"this\""
82
83
name: fred
83
84
number: 42
84
 
''')
 
85
'''[1:])
85
86
 
86
87
    def test_multiline_string(self):
87
88
        tmpf = TemporaryFile()
88
 
        s = Stanza(
89
 
            motto="war is peace\nfreedom is slavery\nignorance is strength")
 
89
        s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
90
90
        s.write(tmpf)
91
91
        tmpf.seek(0)
92
 
        self.assertEqual(tmpf.read(), b'''\
 
92
        self.assertEqualDiff(tmpf.read(), '''\
93
93
motto: war is peace
94
94
\tfreedom is slavery
95
95
\tignorance is strength
96
96
''')
97
97
        tmpf.seek(0)
98
98
        s2 = read_stanza(tmpf)
99
 
        self.assertEqual(s, s2)
 
99
        self.assertEquals(s, s2)
100
100
 
101
101
    def test_read_stanza(self):
102
102
        """Load stanza from string"""
103
 
        lines = b"""\
 
103
        lines = """\
104
104
revision: mbp@sourcefrog.net-123-abc
105
105
timestamp: 1130653962
106
106
timezone: 36000
108
108
""".splitlines(True)
109
109
        s = read_stanza(lines)
110
110
        self.assertTrue('revision' in s)
111
 
        self.assertEqual(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
112
 
        self.assertEqual(list(s.iter_pairs()),
113
 
                         [('revision', 'mbp@sourcefrog.net-123-abc'),
114
 
                          ('timestamp', '1130653962'),
115
 
                          ('timezone', '36000'),
116
 
                          ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
117
 
        self.assertEqual(len(s), 4)
 
111
        self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
 
112
        self.assertEquals(list(s.iter_pairs()),
 
113
                [('revision', 'mbp@sourcefrog.net-123-abc'),
 
114
                 ('timestamp', '1130653962'),
 
115
                 ('timezone', '36000'),
 
116
                 ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
 
117
        self.assertEquals(len(s), 4)
118
118
 
119
119
    def test_repeated_field(self):
120
120
        """Repeated field in rio"""
121
121
        s = Stanza()
122
 
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
 
122
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'), 
123
123
                     ('a', '1000'), ('b', '2000')]:
124
124
            s.add(k, v)
125
125
        s2 = read_stanza(s.to_lines())
126
 
        self.assertEqual(s, s2)
127
 
        self.assertEqual(s.get_all('a'), ['10', '100', '1000'])
128
 
        self.assertEqual(s.get_all('b'), ['20', '200', '2000'])
 
126
        self.assertEquals(s, s2)
 
127
        self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
 
128
        self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
129
129
 
130
130
    def test_backslash(self):
131
131
        s = Stanza(q='\\')
132
132
        t = s.to_string()
133
 
        self.assertEqual(t, b'q: \\\n')
 
133
        self.assertEqualDiff(t, 'q: \\\n')
134
134
        s2 = read_stanza(s.to_lines())
135
 
        self.assertEqual(s, s2)
 
135
        self.assertEquals(s, s2)
136
136
 
137
137
    def test_blank_line(self):
138
138
        s = Stanza(none='', one='\n', two='\n\n')
139
 
        self.assertEqual(s.to_string(), b"""\
140
 
none:\x20
141
 
one:\x20
 
139
        self.assertEqualDiff(s.to_string(), """\
 
140
none: 
 
141
one: 
142
142
\t
143
 
two:\x20
 
143
two: 
144
144
\t
145
145
\t
146
146
""")
147
147
        s2 = read_stanza(s.to_lines())
148
 
        self.assertEqual(s, s2)
 
148
        self.assertEquals(s, s2)
149
149
 
150
150
    def test_whitespace_value(self):
151
151
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
152
 
        self.assertEqual(s.to_string(), b"""\
153
 
combo:\x20
 
152
        self.assertEqualDiff(s.to_string(), """\
 
153
combo: 
154
154
\t\t\t
155
155
\t
156
 
space:\x20\x20
 
156
space:  
157
157
tabs: \t\t\t
158
158
""")
159
159
        s2 = read_stanza(s.to_lines())
160
 
        self.assertEqual(s, s2)
 
160
        self.assertEquals(s, s2)
161
161
        self.rio_file_stanzas([s])
162
162
 
163
163
    def test_quoted(self):
164
164
        """rio quoted string cases"""
165
 
        s = Stanza(q1='"hello"',
166
 
                   q2=' "for',
 
165
        s = Stanza(q1='"hello"', 
 
166
                   q2=' "for', 
167
167
                   q3='\n\n"for"\n',
168
168
                   q4='for\n"\nfor',
169
169
                   q5='\n',
170
 
                   q6='"',
 
170
                   q6='"', 
171
171
                   q7='""',
172
172
                   q8='\\',
173
173
                   q9='\\"\\"',
174
174
                   )
175
175
        s2 = read_stanza(s.to_lines())
176
 
        self.assertEqual(s, s2)
 
176
        self.assertEquals(s, s2)
177
177
        # apparent bug in read_stanza
178
178
        # s3 = read_stanza(self.stanzas_to_str([s]))
179
 
        # self.assertEqual(s, s3)
 
179
        # self.assertEquals(s, s3)
180
180
 
181
181
    def test_read_empty(self):
182
182
        """Detect end of rio file"""
183
183
        s = read_stanza([])
184
184
        self.assertEqual(s, None)
185
185
        self.assertTrue(s is None)
186
 
 
187
 
    def test_read_nul_byte(self):
188
 
        """File consisting of a nul byte causes an error."""
189
 
        self.assertRaises(ValueError, read_stanza, [b'\0'])
190
 
 
191
 
    def test_read_nul_bytes(self):
192
 
        """File consisting of many nul bytes causes an error."""
193
 
        self.assertRaises(ValueError, read_stanza, [b'\0' * 100])
194
 
 
 
186
        
195
187
    def test_read_iter(self):
196
188
        """Read several stanzas from file"""
197
189
        tmpf = TemporaryFile()
198
 
        tmpf.write(b"""\
 
190
        tmpf.write("""\
199
191
version_header: 1
200
192
 
201
193
name: foo
208
200
        reader = read_stanzas(tmpf)
209
201
        read_iter = iter(reader)
210
202
        stuff = list(reader)
211
 
        self.assertEqual(stuff,
212
 
                         [Stanza(version_header='1'),
213
 
                          Stanza(name="foo", val='123'),
214
 
                             Stanza(name="bar", val='129319'), ])
 
203
        self.assertEqual(stuff, 
 
204
                [ Stanza(version_header='1'),
 
205
                  Stanza(name="foo", val='123'),
 
206
                  Stanza(name="bar", val='129319'), ])
215
207
 
216
208
    def test_read_several(self):
217
209
        """Read several stanzas from file"""
218
210
        tmpf = TemporaryFile()
219
 
        tmpf.write(b"""\
 
211
        tmpf.write("""\
220
212
version_header: 1
221
213
 
222
214
name: foo
232
224
""")
233
225
        tmpf.seek(0)
234
226
        s = read_stanza(tmpf)
235
 
        self.assertEqual(s, Stanza(version_header='1'))
236
 
        s = read_stanza(tmpf)
237
 
        self.assertEqual(s, Stanza(name="foo", val='123'))
238
 
        s = read_stanza(tmpf)
239
 
        self.assertEqual(s.get('name'), 'quoted')
240
 
        self.assertEqual(
241
 
            s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
242
 
        s = read_stanza(tmpf)
243
 
        self.assertEqual(s, Stanza(name="bar", val='129319'))
244
 
        s = read_stanza(tmpf)
245
 
        self.assertEqual(s, None)
 
227
        self.assertEquals(s, Stanza(version_header='1'))
 
228
        s = read_stanza(tmpf)
 
229
        self.assertEquals(s, Stanza(name="foo", val='123'))
 
230
        s = read_stanza(tmpf)
 
231
        self.assertEqualDiff(s.get('name'), 'quoted')
 
232
        self.assertEqualDiff(s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
 
233
        s = read_stanza(tmpf)
 
234
        self.assertEquals(s, Stanza(name="bar", val='129319'))
 
235
        s = read_stanza(tmpf)
 
236
        self.assertEquals(s, None)
246
237
        self.check_rio_file(tmpf)
247
238
 
248
239
    def check_rio_file(self, real_file):
249
240
        real_file.seek(0)
250
241
        read_write = rio_file(RioReader(real_file)).read()
251
242
        real_file.seek(0)
252
 
        self.assertEqual(read_write, real_file.read())
 
243
        self.assertEquals(read_write, real_file.read())
253
244
 
254
245
    @staticmethod
255
246
    def stanzas_to_str(stanzas):
261
252
 
262
253
    def test_tricky_quoted(self):
263
254
        tmpf = TemporaryFile()
264
 
        tmpf.write(b'''\
 
255
        tmpf.write('''\
265
256
s: "one"
266
257
 
267
 
s:\x20
 
258
s: 
268
259
\t"one"
269
260
\t
270
261
 
274
265
 
275
266
s: """
276
267
 
277
 
s:\x20
 
268
s: 
278
269
\t
279
270
 
280
271
s: \\
281
272
 
282
 
s:\x20
 
273
s: 
283
274
\t\\
284
275
\t\\\\
285
276
\t
295
286
''')
296
287
        tmpf.seek(0)
297
288
        expected_vals = ['"one"',
298
 
                         '\n"one"\n',
299
 
                         '"',
300
 
                         '""',
301
 
                         '"""',
302
 
                         '\n',
303
 
                         '\\',
304
 
                         '\n\\\n\\\\\n',
305
 
                         'word\\',
306
 
                         'quote\"',
307
 
                         'backslashes\\\\\\',
308
 
                         'both\\\"',
309
 
                         ]
 
289
            '\n"one"\n',
 
290
            '"',
 
291
            '""',
 
292
            '"""',
 
293
            '\n',
 
294
            '\\',
 
295
            '\n\\\n\\\\\n',
 
296
            'word\\',
 
297
            'quote\"',
 
298
            'backslashes\\\\\\',
 
299
            'both\\\"',
 
300
            ]
310
301
        for expected in expected_vals:
311
302
            stanza = read_stanza(tmpf)
312
303
            self.rio_file_stanzas([stanza])
313
 
            self.assertEqual(len(stanza), 1)
314
 
            self.assertEqual(stanza.get('s'), expected)
 
304
            self.assertEquals(len(stanza), 1)
 
305
            self.assertEqualDiff(stanza.get('s'), expected)
315
306
 
316
307
    def test_write_empty_stanza(self):
317
308
        """Write empty stanza"""
318
309
        l = list(Stanza().to_lines())
319
 
        self.assertEqual(l, [])
 
310
        self.assertEquals(l, [])
320
311
 
321
312
    def test_rio_raises_type_error(self):
322
313
        """TypeError on adding invalid type to Stanza"""
329
320
        self.assertRaises(TypeError, s.add, 10, {})
330
321
 
331
322
    def test_rio_unicode(self):
 
323
        # intentionally use cStringIO which doesn't accomodate unencoded unicode objects
 
324
        sio = cStringIO.StringIO()
332
325
        uni_data = u'\N{KATAKANA LETTER O}'
333
326
        s = Stanza(foo=uni_data)
334
 
        self.assertEqual(s.get('foo'), uni_data)
 
327
        self.assertEquals(s.get('foo'), uni_data)
335
328
        raw_lines = s.to_lines()
336
 
        self.assertEqual(raw_lines,
337
 
                         [b'foo: ' + uni_data.encode('utf-8') + b'\n'])
 
329
        self.assertEquals(raw_lines,
 
330
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
338
331
        new_s = read_stanza(raw_lines)
339
 
        self.assertEqual(new_s.get('foo'), uni_data)
340
 
 
341
 
    def test_rio_to_unicode(self):
342
 
        uni_data = u'\N{KATAKANA LETTER O}'
343
 
        s = Stanza(foo=uni_data)
344
 
        unicode_str = s.to_unicode()
345
 
        self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
346
 
        new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
347
 
        self.assertEqual(uni_data, new_s.get('foo'))
348
 
 
349
 
    def test_nested_rio_unicode(self):
350
 
        uni_data = u'\N{KATAKANA LETTER O}'
351
 
        s = Stanza(foo=uni_data)
352
 
        parent_stanza = Stanza(child=s.to_unicode())
353
 
        raw_lines = parent_stanza.to_lines()
354
 
        self.assertEqual([b'child: foo: ' + uni_data.encode('utf-8') + b'\n',
355
 
                          b'\t\n',
356
 
                          ], raw_lines)
357
 
        new_parent = read_stanza(raw_lines)
358
 
        child_text = new_parent.get('child')
359
 
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
360
 
        new_child = rio.read_stanza_unicode(child_text.splitlines(True))
361
 
        self.assertEqual(uni_data, new_child.get('foo'))
362
 
 
363
 
    def mail_munge(self, lines, dos_nl=True):
364
 
        new_lines = []
365
 
        for line in lines:
366
 
            line = re.sub(b' *\n', b'\n', line)
367
 
            if dos_nl:
368
 
                line = re.sub(b'([^\r])\n', b'\\1\r\n', line)
369
 
            new_lines.append(line)
370
 
        return new_lines
371
 
 
372
 
    def test_patch_rio(self):
373
 
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
374
 
        lines = rio.to_patch_lines(stanza)
375
 
        for line in lines:
376
 
            self.assertContainsRe(line, b'^# ')
377
 
            self.assertTrue(72 >= len(line))
378
 
        for line in rio.to_patch_lines(stanza, max_width=12):
379
 
            self.assertTrue(12 >= len(line))
380
 
        new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
381
 
                                                           dos_nl=False))
382
 
        lines = self.mail_munge(lines)
383
 
        new_stanza = rio.read_patch_stanza(lines)
384
 
        self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
385
 
        self.assertEqual(' ' * 255, new_stanza.get('space'))
386
 
        self.assertEqual('#' * 255, new_stanza.get('hash'))
387
 
 
388
 
    def test_patch_rio_linebreaks(self):
389
 
        stanza = Stanza(breaktest='linebreak -/' * 30)
390
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
391
 
                              b'linebreak\\\\\n')
392
 
        stanza = Stanza(breaktest='linebreak-/' * 30)
393
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
394
 
                              b'linebreak-\\\\\n')
395
 
        stanza = Stanza(breaktest='linebreak/' * 30)
396
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
397
 
                              b'linebreak\\\\\n')
 
332
        self.assertEquals(new_s.get('foo'), uni_data)
 
333