~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Robert Collins
  • Date: 2009-09-07 03:08:30 UTC
  • mto: This revision was merged to the branch mainline in revision 4690.
  • Revision ID: robertc@robertcollins.net-20090907030830-rf59kt28d550eauj
Milestones language tightning, internal consistency.

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
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for rio serialization
18
18
 
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
 
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)
32
37
 
33
38
 
34
39
class TestRio(TestCase):
49
54
        # these aren't enforced at construction time
50
55
        ## self.assertRaises(ValueError,
51
56
        ##        Stanza, complex=42 + 3j)
52
 
        ## self.assertRaises(ValueError, 
 
57
        ## self.assertRaises(ValueError,
53
58
        ##        Stanza, several=range(10))
54
59
 
55
60
    def test_empty_value(self):
118
123
    def test_repeated_field(self):
119
124
        """Repeated field in rio"""
120
125
        s = Stanza()
121
 
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'), 
 
126
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
122
127
                     ('a', '1000'), ('b', '2000')]:
123
128
            s.add(k, v)
124
129
        s2 = read_stanza(s.to_lines())
136
141
    def test_blank_line(self):
137
142
        s = Stanza(none='', one='\n', two='\n\n')
138
143
        self.assertEqualDiff(s.to_string(), """\
139
 
none: 
140
 
one: 
 
144
none:\x20
 
145
one:\x20
141
146
\t
142
 
two: 
 
147
two:\x20
143
148
\t
144
149
\t
145
150
""")
149
154
    def test_whitespace_value(self):
150
155
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
151
156
        self.assertEqualDiff(s.to_string(), """\
152
 
combo: 
 
157
combo:\x20
153
158
\t\t\t
154
159
\t
155
 
space:  
 
160
space:\x20\x20
156
161
tabs: \t\t\t
157
162
""")
158
163
        s2 = read_stanza(s.to_lines())
159
164
        self.assertEquals(s, s2)
 
165
        self.rio_file_stanzas([s])
160
166
 
161
167
    def test_quoted(self):
162
168
        """rio quoted string cases"""
163
 
        s = Stanza(q1='"hello"', 
164
 
                   q2=' "for', 
 
169
        s = Stanza(q1='"hello"',
 
170
                   q2=' "for',
165
171
                   q3='\n\n"for"\n',
166
172
                   q4='for\n"\nfor',
167
173
                   q5='\n',
168
 
                   q6='"', 
 
174
                   q6='"',
169
175
                   q7='""',
170
176
                   q8='\\',
171
177
                   q9='\\"\\"',
172
178
                   )
173
179
        s2 = read_stanza(s.to_lines())
174
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)
175
184
 
176
185
    def test_read_empty(self):
177
186
        """Detect end of rio file"""
178
187
        s = read_stanza([])
179
188
        self.assertEqual(s, None)
180
189
        self.assertTrue(s is None)
181
 
        
 
190
 
182
191
    def test_read_iter(self):
183
192
        """Read several stanzas from file"""
184
193
        tmpf = TemporaryFile()
195
204
        reader = read_stanzas(tmpf)
196
205
        read_iter = iter(reader)
197
206
        stuff = list(reader)
198
 
        self.assertEqual(stuff, 
 
207
        self.assertEqual(stuff,
199
208
                [ Stanza(version_header='1'),
200
209
                  Stanza(name="foo", val='123'),
201
210
                  Stanza(name="bar", val='129319'), ])
229
238
        self.assertEquals(s, Stanza(name="bar", val='129319'))
230
239
        s = read_stanza(tmpf)
231
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)
232
256
 
233
257
    def test_tricky_quoted(self):
234
258
        tmpf = TemporaryFile()
235
259
        tmpf.write('''\
236
260
s: "one"
237
261
 
238
 
s: 
 
262
s:\x20
239
263
\t"one"
240
264
\t
241
265
 
245
269
 
246
270
s: """
247
271
 
248
 
s: 
 
272
s:\x20
249
273
\t
250
274
 
251
275
s: \\
252
276
 
253
 
s: 
 
277
s:\x20
254
278
\t\\
255
279
\t\\\\
256
280
\t
280
304
            ]
281
305
        for expected in expected_vals:
282
306
            stanza = read_stanza(tmpf)
 
307
            self.rio_file_stanzas([stanza])
283
308
            self.assertEquals(len(stanza), 1)
284
309
            self.assertEqualDiff(stanza.get('s'), expected)
285
310
 
299
324
        self.assertRaises(TypeError, s.add, 10, {})
300
325
 
301
326
    def test_rio_unicode(self):
302
 
        # intentionally use cStringIO which doesn't accomodate unencoded unicode objects
303
 
        sio = cStringIO.StringIO()
304
327
        uni_data = u'\N{KATAKANA LETTER O}'
305
328
        s = Stanza(foo=uni_data)
306
329
        self.assertEquals(s.get('foo'), uni_data)
310
333
        new_s = read_stanza(raw_lines)
311
334
        self.assertEquals(new_s.get('foo'), uni_data)
312
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')