~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Aaron Bentley
  • Date: 2007-12-12 15:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 3113.
  • Revision ID: abentley@panoramicfeedback.com-20071212151713-ox5n8rlx8m3nsspy
Add support for reconfiguring repositories into branches or trees

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
22
22
but this depends on the transport.
23
23
"""
24
24
 
 
25
import cStringIO
25
26
import os
 
27
import re
26
28
import sys
27
29
from tempfile import TemporaryFile
28
30
 
 
31
from bzrlib import (
 
32
    rio,
 
33
    )
29
34
from bzrlib.tests import TestCaseInTempDir, TestCase
30
 
from bzrlib.rio import RioWriter, Stanza, read_stanza, read_stanzas
 
35
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
 
36
                        RioReader)
31
37
 
32
38
 
33
39
class TestRio(TestCase):
64
70
                ['name: fred\n',
65
71
                 'number: 42\n'])
66
72
 
 
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
 
67
79
    def test_to_file(self):
68
80
        """Write rio to file"""
69
81
        tmpf = TemporaryFile()
150
162
""")
151
163
        s2 = read_stanza(s.to_lines())
152
164
        self.assertEquals(s, s2)
 
165
        self.rio_file_stanzas([s])
153
166
 
154
167
    def test_quoted(self):
155
168
        """rio quoted string cases"""
165
178
                   )
166
179
        s2 = read_stanza(s.to_lines())
167
180
        self.assertEquals(s, s2)
 
181
        # apparent bug in read_stanza
 
182
        # s3 = read_stanza(self.stanzas_to_str([s]))
 
183
        # self.assertEquals(s, s3)
168
184
 
169
185
    def test_read_empty(self):
170
186
        """Detect end of rio file"""
222
238
        self.assertEquals(s, Stanza(name="bar", val='129319'))
223
239
        s = read_stanza(tmpf)
224
240
        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)
225
256
 
226
257
    def test_tricky_quoted(self):
227
258
        tmpf = TemporaryFile()
273
304
            ]
274
305
        for expected in expected_vals:
275
306
            stanza = read_stanza(tmpf)
 
307
            self.rio_file_stanzas([stanza])
276
308
            self.assertEquals(len(stanza), 1)
277
309
            self.assertEqualDiff(stanza.get('s'), expected)
278
310
 
280
312
        """Write empty stanza"""
281
313
        l = list(Stanza().to_lines())
282
314
        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')