~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-09-29 15:31:52 UTC
  • mfrom: (2052.1.1 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060929153152-9a32a9b020e0f74b
Lukáš Lalinský: Windows-speficic smart server transport selftest fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by 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
22
22
but this depends on the transport.
23
23
"""
24
24
 
 
25
import cStringIO
25
26
import os
26
27
import sys
27
28
from tempfile import TemporaryFile
28
29
 
 
30
from bzrlib import (
 
31
    rio,
 
32
    )
29
33
from bzrlib.tests import TestCaseInTempDir, TestCase
30
 
from bzrlib.rio import RioWriter, Stanza, read_stanza, read_stanzas
 
34
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
 
35
                        RioReader)
31
36
 
32
37
 
33
38
class TestRio(TestCase):
64
69
                ['name: fred\n',
65
70
                 'number: 42\n'])
66
71
 
 
72
    def test_as_dict(self):
 
73
        """Convert rio Stanza to dictionary"""
 
74
        s = Stanza(number='42', name='fred')
 
75
        sd = s.as_dict()
 
76
        self.assertEquals(sd, dict(number='42', name='fred'))
 
77
 
67
78
    def test_to_file(self):
68
79
        """Write rio to file"""
69
80
        tmpf = TemporaryFile()
150
161
""")
151
162
        s2 = read_stanza(s.to_lines())
152
163
        self.assertEquals(s, s2)
 
164
        self.rio_file_stanzas([s])
153
165
 
154
166
    def test_quoted(self):
155
167
        """rio quoted string cases"""
165
177
                   )
166
178
        s2 = read_stanza(s.to_lines())
167
179
        self.assertEquals(s, s2)
 
180
        # apparent bug in read_stanza
 
181
        # s3 = read_stanza(self.stanzas_to_str([s]))
 
182
        # self.assertEquals(s, s3)
168
183
 
169
184
    def test_read_empty(self):
170
185
        """Detect end of rio file"""
222
237
        self.assertEquals(s, Stanza(name="bar", val='129319'))
223
238
        s = read_stanza(tmpf)
224
239
        self.assertEquals(s, None)
 
240
        self.check_rio_file(tmpf)
 
241
 
 
242
    def check_rio_file(self, real_file):
 
243
        real_file.seek(0)
 
244
        read_write = rio_file(RioReader(real_file)).read()
 
245
        real_file.seek(0)
 
246
        self.assertEquals(read_write, real_file.read())
 
247
 
 
248
    @staticmethod
 
249
    def stanzas_to_str(stanzas):
 
250
        return rio_file(stanzas).read()
 
251
 
 
252
    def rio_file_stanzas(self, stanzas):
 
253
        new_stanzas = list(RioReader(rio_file(stanzas)))
 
254
        self.assertEqual(new_stanzas, stanzas)
225
255
 
226
256
    def test_tricky_quoted(self):
227
257
        tmpf = TemporaryFile()
273
303
            ]
274
304
        for expected in expected_vals:
275
305
            stanza = read_stanza(tmpf)
 
306
            self.rio_file_stanzas([stanza])
276
307
            self.assertEquals(len(stanza), 1)
277
308
            self.assertEqualDiff(stanza.get('s'), expected)
278
309
 
280
311
        """Write empty stanza"""
281
312
        l = list(Stanza().to_lines())
282
313
        self.assertEquals(l, [])
 
314
 
 
315
    def test_rio_raises_type_error(self):
 
316
        """TypeError on adding invalid type to Stanza"""
 
317
        s = Stanza()
 
318
        self.assertRaises(TypeError, s.add, 'foo', {})
 
319
 
 
320
    def test_rio_raises_type_error_key(self):
 
321
        """TypeError on adding invalid type to Stanza"""
 
322
        s = Stanza()
 
323
        self.assertRaises(TypeError, s.add, 10, {})
 
324
 
 
325
    def test_rio_unicode(self):
 
326
        uni_data = u'\N{KATAKANA LETTER O}'
 
327
        s = Stanza(foo=uni_data)
 
328
        self.assertEquals(s.get('foo'), uni_data)
 
329
        raw_lines = s.to_lines()
 
330
        self.assertEquals(raw_lines,
 
331
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
 
332
        new_s = read_stanza(raw_lines)
 
333
        self.assertEquals(new_s.get('foo'), uni_data)
 
334
 
 
335
    def test_rio_to_unicode(self):
 
336
        uni_data = u'\N{KATAKANA LETTER O}'
 
337
        s = Stanza(foo=uni_data)
 
338
        unicode_str = s.to_unicode()
 
339
        self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
 
340
        new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
 
341
        self.assertEqual(uni_data, new_s.get('foo'))
 
342
 
 
343
    def test_nested_rio_unicode(self):
 
344
        uni_data = u'\N{KATAKANA LETTER O}'
 
345
        s = Stanza(foo=uni_data)
 
346
        parent_stanza = Stanza(child=s.to_unicode())
 
347
        raw_lines = parent_stanza.to_lines()
 
348
        self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
 
349
                          '\t\n',
 
350
                         ], raw_lines)
 
351
        new_parent = read_stanza(raw_lines)
 
352
        child_text = new_parent.get('child')
 
353
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
 
354
        new_child = rio.read_stanza_unicode(child_text.splitlines(True))
 
355
        self.assertEqual(uni_data, new_child.get('foo'))