~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Robert Collins
  • Date: 2007-04-30 05:13:58 UTC
  • mfrom: (2470 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2471.
  • Revision ID: robertc@robertcollins.net-20070430051358-8cp7kvp1q0tqhxx0
Merge Johns fix for bug 110399.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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
24
24
 
25
25
import cStringIO
26
26
import os
 
27
import re
27
28
import sys
28
29
from tempfile import TemporaryFile
29
30
 
 
31
from bzrlib import (
 
32
    rio,
 
33
    )
30
34
from bzrlib.tests import TestCaseInTempDir, TestCase
31
35
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
32
36
                        RioReader)
320
324
        self.assertRaises(TypeError, s.add, 10, {})
321
325
 
322
326
    def test_rio_unicode(self):
323
 
        # intentionally use cStringIO which doesn't accomodate unencoded unicode objects
324
 
        sio = cStringIO.StringIO()
325
327
        uni_data = u'\N{KATAKANA LETTER O}'
326
328
        s = Stanza(foo=uni_data)
327
329
        self.assertEquals(s.get('foo'), uni_data)
331
333
        new_s = read_stanza(raw_lines)
332
334
        self.assertEquals(new_s.get('foo'), uni_data)
333
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')