~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

Fix BzrDir.create_workingtree for NULL_REVISION

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
 
29
30
from bzrlib.tests import TestCaseInTempDir, TestCase
30
 
from bzrlib.rio import RioWriter, Stanza, read_stanza, read_stanzas
 
31
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
 
32
                        RioReader)
31
33
 
32
34
 
33
35
class TestRio(TestCase):
64
66
                ['name: fred\n',
65
67
                 'number: 42\n'])
66
68
 
 
69
    def test_as_dict(self):
 
70
        """Convert rio Stanza to dictionary"""
 
71
        s = Stanza(number='42', name='fred')
 
72
        sd = s.as_dict()
 
73
        self.assertEquals(sd, dict(number='42', name='fred'))
 
74
 
67
75
    def test_to_file(self):
68
76
        """Write rio to file"""
69
77
        tmpf = TemporaryFile()
150
158
""")
151
159
        s2 = read_stanza(s.to_lines())
152
160
        self.assertEquals(s, s2)
 
161
        self.rio_file_stanzas([s])
153
162
 
154
163
    def test_quoted(self):
155
164
        """rio quoted string cases"""
165
174
                   )
166
175
        s2 = read_stanza(s.to_lines())
167
176
        self.assertEquals(s, s2)
 
177
        # apparent bug in read_stanza
 
178
        # s3 = read_stanza(self.stanzas_to_str([s]))
 
179
        # self.assertEquals(s, s3)
168
180
 
169
181
    def test_read_empty(self):
170
182
        """Detect end of rio file"""
222
234
        self.assertEquals(s, Stanza(name="bar", val='129319'))
223
235
        s = read_stanza(tmpf)
224
236
        self.assertEquals(s, None)
 
237
        self.check_rio_file(tmpf)
 
238
 
 
239
    def check_rio_file(self, real_file):
 
240
        real_file.seek(0)
 
241
        read_write = rio_file(RioReader(real_file)).read()
 
242
        real_file.seek(0)
 
243
        self.assertEquals(read_write, real_file.read())
 
244
 
 
245
    @staticmethod
 
246
    def stanzas_to_str(stanzas):
 
247
        return rio_file(stanzas).read()
 
248
 
 
249
    def rio_file_stanzas(self, stanzas):
 
250
        new_stanzas = list(RioReader(rio_file(stanzas)))
 
251
        self.assertEqual(new_stanzas, stanzas)
225
252
 
226
253
    def test_tricky_quoted(self):
227
254
        tmpf = TemporaryFile()
273
300
            ]
274
301
        for expected in expected_vals:
275
302
            stanza = read_stanza(tmpf)
 
303
            self.rio_file_stanzas([stanza])
276
304
            self.assertEquals(len(stanza), 1)
277
305
            self.assertEqualDiff(stanza.get('s'), expected)
278
306
 
280
308
        """Write empty stanza"""
281
309
        l = list(Stanza().to_lines())
282
310
        self.assertEquals(l, [])
 
311
 
 
312
    def test_rio_raises_type_error(self):
 
313
        """TypeError on adding invalid type to Stanza"""
 
314
        s = Stanza()
 
315
        self.assertRaises(TypeError, s.add, 'foo', {})
 
316
 
 
317
    def test_rio_raises_type_error_key(self):
 
318
        """TypeError on adding invalid type to Stanza"""
 
319
        s = Stanza()
 
320
        self.assertRaises(TypeError, s.add, 10, {})
 
321
 
 
322
    def test_rio_unicode(self):
 
323
        # intentionally use cStringIO which doesn't accomodate unencoded unicode objects
 
324
        sio = cStringIO.StringIO()
 
325
        uni_data = u'\N{KATAKANA LETTER O}'
 
326
        s = Stanza(foo=uni_data)
 
327
        self.assertEquals(s.get('foo'), uni_data)
 
328
        raw_lines = s.to_lines()
 
329
        self.assertEquals(raw_lines,
 
330
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
 
331
        new_s = read_stanza(raw_lines)
 
332
        self.assertEquals(new_s.get('foo'), uni_data)
 
333