~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Robert Collins
  • Date: 2006-07-20 13:00:31 UTC
  • mto: (1852.9.1 Tree.compare().)
  • mto: This revision was merged to the branch mainline in revision 1890.
  • Revision ID: robertc@robertcollins.net-20060720130031-d26103a427ea10f3
StartĀ treeĀ implementationĀ tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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
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
 
24
24
 
25
25
import cStringIO
26
26
import os
27
 
import re
28
27
import sys
29
28
from tempfile import TemporaryFile
30
29
 
31
 
from bzrlib import (
32
 
    rio,
33
 
    )
34
30
from bzrlib.tests import TestCaseInTempDir, TestCase
35
31
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
36
32
                        RioReader)
54
50
        # these aren't enforced at construction time
55
51
        ## self.assertRaises(ValueError,
56
52
        ##        Stanza, complex=42 + 3j)
57
 
        ## self.assertRaises(ValueError,
 
53
        ## self.assertRaises(ValueError, 
58
54
        ##        Stanza, several=range(10))
59
55
 
60
56
    def test_empty_value(self):
123
119
    def test_repeated_field(self):
124
120
        """Repeated field in rio"""
125
121
        s = Stanza()
126
 
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
 
122
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'), 
127
123
                     ('a', '1000'), ('b', '2000')]:
128
124
            s.add(k, v)
129
125
        s2 = read_stanza(s.to_lines())
141
137
    def test_blank_line(self):
142
138
        s = Stanza(none='', one='\n', two='\n\n')
143
139
        self.assertEqualDiff(s.to_string(), """\
144
 
none:\x20
145
 
one:\x20
 
140
none: 
 
141
one: 
146
142
\t
147
 
two:\x20
 
143
two: 
148
144
\t
149
145
\t
150
146
""")
154
150
    def test_whitespace_value(self):
155
151
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
156
152
        self.assertEqualDiff(s.to_string(), """\
157
 
combo:\x20
 
153
combo: 
158
154
\t\t\t
159
155
\t
160
 
space:\x20\x20
 
156
space:  
161
157
tabs: \t\t\t
162
158
""")
163
159
        s2 = read_stanza(s.to_lines())
166
162
 
167
163
    def test_quoted(self):
168
164
        """rio quoted string cases"""
169
 
        s = Stanza(q1='"hello"',
170
 
                   q2=' "for',
 
165
        s = Stanza(q1='"hello"', 
 
166
                   q2=' "for', 
171
167
                   q3='\n\n"for"\n',
172
168
                   q4='for\n"\nfor',
173
169
                   q5='\n',
174
 
                   q6='"',
 
170
                   q6='"', 
175
171
                   q7='""',
176
172
                   q8='\\',
177
173
                   q9='\\"\\"',
187
183
        s = read_stanza([])
188
184
        self.assertEqual(s, None)
189
185
        self.assertTrue(s is None)
190
 
 
 
186
        
191
187
    def test_read_iter(self):
192
188
        """Read several stanzas from file"""
193
189
        tmpf = TemporaryFile()
204
200
        reader = read_stanzas(tmpf)
205
201
        read_iter = iter(reader)
206
202
        stuff = list(reader)
207
 
        self.assertEqual(stuff,
 
203
        self.assertEqual(stuff, 
208
204
                [ Stanza(version_header='1'),
209
205
                  Stanza(name="foo", val='123'),
210
206
                  Stanza(name="bar", val='129319'), ])
259
255
        tmpf.write('''\
260
256
s: "one"
261
257
 
262
 
s:\x20
 
258
s: 
263
259
\t"one"
264
260
\t
265
261
 
269
265
 
270
266
s: """
271
267
 
272
 
s:\x20
 
268
s: 
273
269
\t
274
270
 
275
271
s: \\
276
272
 
277
 
s:\x20
 
273
s: 
278
274
\t\\
279
275
\t\\\\
280
276
\t
324
320
        self.assertRaises(TypeError, s.add, 10, {})
325
321
 
326
322
    def test_rio_unicode(self):
 
323
        # intentionally use cStringIO which doesn't accomodate unencoded unicode objects
 
324
        sio = cStringIO.StringIO()
327
325
        uni_data = u'\N{KATAKANA LETTER O}'
328
326
        s = Stanza(foo=uni_data)
329
327
        self.assertEquals(s.get('foo'), uni_data)
333
331
        new_s = read_stanza(raw_lines)
334
332
        self.assertEquals(new_s.get('foo'), uni_data)
335
333
 
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')