~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: 2011-05-04 12:10:51 UTC
  • mfrom: (5819.1.4 777007-developer-doc)
  • Revision ID: pqm@pqm.ubuntu.com-20110504121051-aovlsmqiivjmc4fc
(jelmer) Small fixes to developer documentation. (Jonathan Riddell)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011 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
 
22
22
but this depends on the transport.
23
23
"""
24
24
 
25
 
import cStringIO
26
 
import os
27
 
import sys
 
25
import re
28
26
from tempfile import TemporaryFile
29
27
 
30
28
from bzrlib import (
31
29
    rio,
32
30
    )
33
 
from bzrlib.tests import TestCaseInTempDir, TestCase
34
 
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
35
 
                        RioReader)
 
31
from bzrlib.tests import TestCase
 
32
from bzrlib.rio import (
 
33
    RioReader,
 
34
    Stanza,
 
35
    read_stanza,
 
36
    read_stanzas,
 
37
    rio_file,
 
38
    )
36
39
 
37
40
 
38
41
class TestRio(TestCase):
53
56
        # these aren't enforced at construction time
54
57
        ## self.assertRaises(ValueError,
55
58
        ##        Stanza, complex=42 + 3j)
56
 
        ## self.assertRaises(ValueError, 
 
59
        ## self.assertRaises(ValueError,
57
60
        ##        Stanza, several=range(10))
58
61
 
59
62
    def test_empty_value(self):
122
125
    def test_repeated_field(self):
123
126
        """Repeated field in rio"""
124
127
        s = Stanza()
125
 
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'), 
 
128
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
126
129
                     ('a', '1000'), ('b', '2000')]:
127
130
            s.add(k, v)
128
131
        s2 = read_stanza(s.to_lines())
140
143
    def test_blank_line(self):
141
144
        s = Stanza(none='', one='\n', two='\n\n')
142
145
        self.assertEqualDiff(s.to_string(), """\
143
 
none: 
144
 
one: 
 
146
none:\x20
 
147
one:\x20
145
148
\t
146
 
two: 
 
149
two:\x20
147
150
\t
148
151
\t
149
152
""")
153
156
    def test_whitespace_value(self):
154
157
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
155
158
        self.assertEqualDiff(s.to_string(), """\
156
 
combo: 
 
159
combo:\x20
157
160
\t\t\t
158
161
\t
159
 
space:  
 
162
space:\x20\x20
160
163
tabs: \t\t\t
161
164
""")
162
165
        s2 = read_stanza(s.to_lines())
165
168
 
166
169
    def test_quoted(self):
167
170
        """rio quoted string cases"""
168
 
        s = Stanza(q1='"hello"', 
169
 
                   q2=' "for', 
 
171
        s = Stanza(q1='"hello"',
 
172
                   q2=' "for',
170
173
                   q3='\n\n"for"\n',
171
174
                   q4='for\n"\nfor',
172
175
                   q5='\n',
173
 
                   q6='"', 
 
176
                   q6='"',
174
177
                   q7='""',
175
178
                   q8='\\',
176
179
                   q9='\\"\\"',
186
189
        s = read_stanza([])
187
190
        self.assertEqual(s, None)
188
191
        self.assertTrue(s is None)
189
 
        
 
192
 
 
193
    def test_read_nul_byte(self):
 
194
        """File consisting of a nul byte causes an error."""
 
195
        self.assertRaises(ValueError, read_stanza, ['\0'])
 
196
 
 
197
    def test_read_nul_bytes(self):
 
198
        """File consisting of many nul bytes causes an error."""
 
199
        self.assertRaises(ValueError, read_stanza, ['\0' * 100])
 
200
 
190
201
    def test_read_iter(self):
191
202
        """Read several stanzas from file"""
192
203
        tmpf = TemporaryFile()
203
214
        reader = read_stanzas(tmpf)
204
215
        read_iter = iter(reader)
205
216
        stuff = list(reader)
206
 
        self.assertEqual(stuff, 
 
217
        self.assertEqual(stuff,
207
218
                [ Stanza(version_header='1'),
208
219
                  Stanza(name="foo", val='123'),
209
220
                  Stanza(name="bar", val='129319'), ])
258
269
        tmpf.write('''\
259
270
s: "one"
260
271
 
261
 
s: 
 
272
s:\x20
262
273
\t"one"
263
274
\t
264
275
 
268
279
 
269
280
s: """
270
281
 
271
 
s: 
 
282
s:\x20
272
283
\t
273
284
 
274
285
s: \\
275
286
 
276
 
s: 
 
287
s:\x20
277
288
\t\\
278
289
\t\\\\
279
290
\t
353
364
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
354
365
        new_child = rio.read_stanza_unicode(child_text.splitlines(True))
355
366
        self.assertEqual(uni_data, new_child.get('foo'))
 
367
 
 
368
    def mail_munge(self, lines, dos_nl=True):
 
369
        new_lines = []
 
370
        for line in lines:
 
371
            line = re.sub(' *\n', '\n', line)
 
372
            if dos_nl:
 
373
                line = re.sub('([^\r])\n', '\\1\r\n', line)
 
374
            new_lines.append(line)
 
375
        return new_lines
 
376
 
 
377
    def test_patch_rio(self):
 
378
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
 
379
        lines = rio.to_patch_lines(stanza)
 
380
        for line in lines:
 
381
            self.assertContainsRe(line, '^# ')
 
382
            self.assertTrue(72 >= len(line))
 
383
        for line in rio.to_patch_lines(stanza, max_width=12):
 
384
            self.assertTrue(12 >= len(line))
 
385
        new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
 
386
                                                           dos_nl=False))
 
387
        lines = self.mail_munge(lines)
 
388
        new_stanza = rio.read_patch_stanza(lines)
 
389
        self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
 
390
        self.assertEqual(' '* 255, new_stanza.get('space'))
 
391
        self.assertEqual('#'* 255, new_stanza.get('hash'))
 
392
 
 
393
    def test_patch_rio_linebreaks(self):
 
394
        stanza = Stanza(breaktest='linebreak -/'*30)
 
395
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
 
396
                              'linebreak\\\\\n')
 
397
        stanza = Stanza(breaktest='linebreak-/'*30)
 
398
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
 
399
                              'linebreak-\\\\\n')
 
400
        stanza = Stanza(breaktest='linebreak/'*30)
 
401
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
 
402
                              'linebreak\\\\\n')