~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

Start InterRepository with InterRepository.get.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005 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 cStringIO
26
25
import os
27
 
import re
28
26
import sys
29
27
from tempfile import TemporaryFile
30
28
 
31
 
from bzrlib import (
32
 
    rio,
33
 
    )
34
29
from bzrlib.tests import TestCaseInTempDir, TestCase
35
 
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
36
 
                        RioReader)
 
30
from bzrlib.rio import RioWriter, Stanza, read_stanza, read_stanzas
37
31
 
38
32
 
39
33
class TestRio(TestCase):
54
48
        # these aren't enforced at construction time
55
49
        ## self.assertRaises(ValueError,
56
50
        ##        Stanza, complex=42 + 3j)
57
 
        ## self.assertRaises(ValueError,
 
51
        ## self.assertRaises(ValueError, 
58
52
        ##        Stanza, several=range(10))
59
53
 
60
54
    def test_empty_value(self):
70
64
                ['name: fred\n',
71
65
                 'number: 42\n'])
72
66
 
73
 
    def test_as_dict(self):
74
 
        """Convert rio Stanza to dictionary"""
75
 
        s = Stanza(number='42', name='fred')
76
 
        sd = s.as_dict()
77
 
        self.assertEquals(sd, dict(number='42', name='fred'))
78
 
 
79
67
    def test_to_file(self):
80
68
        """Write rio to file"""
81
69
        tmpf = TemporaryFile()
123
111
    def test_repeated_field(self):
124
112
        """Repeated field in rio"""
125
113
        s = Stanza()
126
 
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
 
114
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'), 
127
115
                     ('a', '1000'), ('b', '2000')]:
128
116
            s.add(k, v)
129
117
        s2 = read_stanza(s.to_lines())
141
129
    def test_blank_line(self):
142
130
        s = Stanza(none='', one='\n', two='\n\n')
143
131
        self.assertEqualDiff(s.to_string(), """\
144
 
none:\x20
145
 
one:\x20
 
132
none: 
 
133
one: 
146
134
\t
147
 
two:\x20
 
135
two: 
148
136
\t
149
137
\t
150
138
""")
154
142
    def test_whitespace_value(self):
155
143
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
156
144
        self.assertEqualDiff(s.to_string(), """\
157
 
combo:\x20
 
145
combo: 
158
146
\t\t\t
159
147
\t
160
 
space:\x20\x20
 
148
space:  
161
149
tabs: \t\t\t
162
150
""")
163
151
        s2 = read_stanza(s.to_lines())
164
152
        self.assertEquals(s, s2)
165
 
        self.rio_file_stanzas([s])
166
153
 
167
154
    def test_quoted(self):
168
155
        """rio quoted string cases"""
169
 
        s = Stanza(q1='"hello"',
170
 
                   q2=' "for',
 
156
        s = Stanza(q1='"hello"', 
 
157
                   q2=' "for', 
171
158
                   q3='\n\n"for"\n',
172
159
                   q4='for\n"\nfor',
173
160
                   q5='\n',
174
 
                   q6='"',
 
161
                   q6='"', 
175
162
                   q7='""',
176
163
                   q8='\\',
177
164
                   q9='\\"\\"',
178
165
                   )
179
166
        s2 = read_stanza(s.to_lines())
180
167
        self.assertEquals(s, s2)
181
 
        # apparent bug in read_stanza
182
 
        # s3 = read_stanza(self.stanzas_to_str([s]))
183
 
        # self.assertEquals(s, s3)
184
168
 
185
169
    def test_read_empty(self):
186
170
        """Detect end of rio file"""
187
171
        s = read_stanza([])
188
172
        self.assertEqual(s, None)
189
173
        self.assertTrue(s is None)
190
 
 
 
174
        
191
175
    def test_read_iter(self):
192
176
        """Read several stanzas from file"""
193
177
        tmpf = TemporaryFile()
204
188
        reader = read_stanzas(tmpf)
205
189
        read_iter = iter(reader)
206
190
        stuff = list(reader)
207
 
        self.assertEqual(stuff,
 
191
        self.assertEqual(stuff, 
208
192
                [ Stanza(version_header='1'),
209
193
                  Stanza(name="foo", val='123'),
210
194
                  Stanza(name="bar", val='129319'), ])
238
222
        self.assertEquals(s, Stanza(name="bar", val='129319'))
239
223
        s = read_stanza(tmpf)
240
224
        self.assertEquals(s, None)
241
 
        self.check_rio_file(tmpf)
242
 
 
243
 
    def check_rio_file(self, real_file):
244
 
        real_file.seek(0)
245
 
        read_write = rio_file(RioReader(real_file)).read()
246
 
        real_file.seek(0)
247
 
        self.assertEquals(read_write, real_file.read())
248
 
 
249
 
    @staticmethod
250
 
    def stanzas_to_str(stanzas):
251
 
        return rio_file(stanzas).read()
252
 
 
253
 
    def rio_file_stanzas(self, stanzas):
254
 
        new_stanzas = list(RioReader(rio_file(stanzas)))
255
 
        self.assertEqual(new_stanzas, stanzas)
256
225
 
257
226
    def test_tricky_quoted(self):
258
227
        tmpf = TemporaryFile()
259
228
        tmpf.write('''\
260
229
s: "one"
261
230
 
262
 
s:\x20
 
231
s: 
263
232
\t"one"
264
233
\t
265
234
 
269
238
 
270
239
s: """
271
240
 
272
 
s:\x20
 
241
s: 
273
242
\t
274
243
 
275
244
s: \\
276
245
 
277
 
s:\x20
 
246
s: 
278
247
\t\\
279
248
\t\\\\
280
249
\t
304
273
            ]
305
274
        for expected in expected_vals:
306
275
            stanza = read_stanza(tmpf)
307
 
            self.rio_file_stanzas([stanza])
308
276
            self.assertEquals(len(stanza), 1)
309
277
            self.assertEqualDiff(stanza.get('s'), expected)
310
278
 
312
280
        """Write empty stanza"""
313
281
        l = list(Stanza().to_lines())
314
282
        self.assertEquals(l, [])
315
 
 
316
 
    def test_rio_raises_type_error(self):
317
 
        """TypeError on adding invalid type to Stanza"""
318
 
        s = Stanza()
319
 
        self.assertRaises(TypeError, s.add, 'foo', {})
320
 
 
321
 
    def test_rio_raises_type_error_key(self):
322
 
        """TypeError on adding invalid type to Stanza"""
323
 
        s = Stanza()
324
 
        self.assertRaises(TypeError, s.add, 10, {})
325
 
 
326
 
    def test_rio_unicode(self):
327
 
        uni_data = u'\N{KATAKANA LETTER O}'
328
 
        s = Stanza(foo=uni_data)
329
 
        self.assertEquals(s.get('foo'), uni_data)
330
 
        raw_lines = s.to_lines()
331
 
        self.assertEquals(raw_lines,
332
 
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
333
 
        new_s = read_stanza(raw_lines)
334
 
        self.assertEquals(new_s.get('foo'), uni_data)
335
 
 
336
 
    def test_rio_to_unicode(self):
337
 
        uni_data = u'\N{KATAKANA LETTER O}'
338
 
        s = Stanza(foo=uni_data)
339
 
        unicode_str = s.to_unicode()
340
 
        self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
341
 
        new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
342
 
        self.assertEqual(uni_data, new_s.get('foo'))
343
 
 
344
 
    def test_nested_rio_unicode(self):
345
 
        uni_data = u'\N{KATAKANA LETTER O}'
346
 
        s = Stanza(foo=uni_data)
347
 
        parent_stanza = Stanza(child=s.to_unicode())
348
 
        raw_lines = parent_stanza.to_lines()
349
 
        self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
350
 
                          '\t\n',
351
 
                         ], raw_lines)
352
 
        new_parent = read_stanza(raw_lines)
353
 
        child_text = new_parent.get('child')
354
 
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
355
 
        new_child = rio.read_stanza_unicode(child_text.splitlines(True))
356
 
        self.assertEqual(uni_data, new_child.get('foo'))
357
 
 
358
 
    def mail_munge(self, lines, dos_nl=True):
359
 
        new_lines = []
360
 
        for line in lines:
361
 
            line = re.sub(' *\n', '\n', line)
362
 
            if dos_nl:
363
 
                line = re.sub('([^\r])\n', '\\1\r\n', line)
364
 
            new_lines.append(line)
365
 
        return new_lines
366
 
 
367
 
    def test_patch_rio(self):
368
 
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
369
 
        lines = rio.to_patch_lines(stanza)
370
 
        for line in lines:
371
 
            self.assertContainsRe(line, '^# ')
372
 
            self.assertTrue(72 >= len(line))
373
 
        for line in rio.to_patch_lines(stanza, max_width=12):
374
 
            self.assertTrue(12 >= len(line))
375
 
        new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
376
 
                                                           dos_nl=False))
377
 
        lines = self.mail_munge(lines)
378
 
        new_stanza = rio.read_patch_stanza(lines)
379
 
        self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
380
 
        self.assertEqual(' '* 255, new_stanza.get('space'))
381
 
        self.assertEqual('#'* 255, new_stanza.get('hash'))
382
 
 
383
 
    def test_patch_rio_linebreaks(self):
384
 
        stanza = Stanza(breaktest='linebreak -/'*30)
385
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
386
 
                              'linebreak\\\\\n')
387
 
        stanza = Stanza(breaktest='linebreak-/'*30)
388
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
389
 
                              'linebreak-\\\\\n')
390
 
        stanza = Stanza(breaktest='linebreak/'*30)
391
 
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
392
 
                              'linebreak\\\\\n')