~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Vincent Ladeuil
  • Date: 2010-03-10 09:33:04 UTC
  • mto: (5082.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5083.
  • Revision ID: v.ladeuil+lp@free.fr-20100310093304-4245t4tazd4sxoav
Cleanup test from overly cautious checks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011, 2016 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
22
22
but this depends on the transport.
23
23
"""
24
24
 
 
25
import cStringIO
 
26
import os
25
27
import re
 
28
import sys
26
29
from tempfile import TemporaryFile
27
30
 
28
31
from bzrlib import (
29
32
    rio,
30
33
    )
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
 
    )
 
34
from bzrlib.tests import TestCaseInTempDir, TestCase
 
35
from bzrlib.rio import (RioWriter, Stanza, read_stanza, read_stanzas, rio_file,
 
36
                        RioReader)
39
37
 
40
38
 
41
39
class TestRio(TestCase):
46
44
        self.assertTrue('number' in s)
47
45
        self.assertFalse('color' in s)
48
46
        self.assertFalse('42' in s)
49
 
        self.assertEqual(list(s.iter_pairs()),
 
47
        self.assertEquals(list(s.iter_pairs()),
50
48
                [('name', 'fred'), ('number', '42')])
51
 
        self.assertEqual(s.get('number'), '42')
52
 
        self.assertEqual(s.get('name'), 'fred')
 
49
        self.assertEquals(s.get('number'), '42')
 
50
        self.assertEquals(s.get('name'), 'fred')
53
51
 
54
52
    def test_value_checks(self):
55
53
        """rio checks types on construction"""
68
66
    def test_to_lines(self):
69
67
        """Write simple rio stanza to string"""
70
68
        s = Stanza(number='42', name='fred')
71
 
        self.assertEqual(list(s.to_lines()),
 
69
        self.assertEquals(list(s.to_lines()),
72
70
                ['name: fred\n',
73
71
                 'number: 42\n'])
74
72
 
76
74
        """Convert rio Stanza to dictionary"""
77
75
        s = Stanza(number='42', name='fred')
78
76
        sd = s.as_dict()
79
 
        self.assertEqual(sd, dict(number='42', name='fred'))
 
77
        self.assertEquals(sd, dict(number='42', name='fred'))
80
78
 
81
79
    def test_to_file(self):
82
80
        """Write rio to file"""
102
100
''')
103
101
        tmpf.seek(0)
104
102
        s2 = read_stanza(tmpf)
105
 
        self.assertEqual(s, s2)
 
103
        self.assertEquals(s, s2)
106
104
 
107
105
    def test_read_stanza(self):
108
106
        """Load stanza from string"""
115
113
        s = read_stanza(lines)
116
114
        self.assertTrue('revision' in s)
117
115
        self.assertEqualDiff(s.get('revision'), 'mbp@sourcefrog.net-123-abc')
118
 
        self.assertEqual(list(s.iter_pairs()),
 
116
        self.assertEquals(list(s.iter_pairs()),
119
117
                [('revision', 'mbp@sourcefrog.net-123-abc'),
120
118
                 ('timestamp', '1130653962'),
121
119
                 ('timezone', '36000'),
122
120
                 ('committer', "Martin Pool <mbp@test.sourcefrog.net>")])
123
 
        self.assertEqual(len(s), 4)
 
121
        self.assertEquals(len(s), 4)
124
122
 
125
123
    def test_repeated_field(self):
126
124
        """Repeated field in rio"""
129
127
                     ('a', '1000'), ('b', '2000')]:
130
128
            s.add(k, v)
131
129
        s2 = read_stanza(s.to_lines())
132
 
        self.assertEqual(s, s2)
133
 
        self.assertEqual(s.get_all('a'), map(str, [10, 100, 1000]))
134
 
        self.assertEqual(s.get_all('b'), map(str, [20, 200, 2000]))
 
130
        self.assertEquals(s, s2)
 
131
        self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
 
132
        self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
135
133
 
136
134
    def test_backslash(self):
137
135
        s = Stanza(q='\\')
138
136
        t = s.to_string()
139
137
        self.assertEqualDiff(t, 'q: \\\n')
140
138
        s2 = read_stanza(s.to_lines())
141
 
        self.assertEqual(s, s2)
 
139
        self.assertEquals(s, s2)
142
140
 
143
141
    def test_blank_line(self):
144
142
        s = Stanza(none='', one='\n', two='\n\n')
151
149
\t
152
150
""")
153
151
        s2 = read_stanza(s.to_lines())
154
 
        self.assertEqual(s, s2)
 
152
        self.assertEquals(s, s2)
155
153
 
156
154
    def test_whitespace_value(self):
157
155
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
163
161
tabs: \t\t\t
164
162
""")
165
163
        s2 = read_stanza(s.to_lines())
166
 
        self.assertEqual(s, s2)
 
164
        self.assertEquals(s, s2)
167
165
        self.rio_file_stanzas([s])
168
166
 
169
167
    def test_quoted(self):
179
177
                   q9='\\"\\"',
180
178
                   )
181
179
        s2 = read_stanza(s.to_lines())
182
 
        self.assertEqual(s, s2)
 
180
        self.assertEquals(s, s2)
183
181
        # apparent bug in read_stanza
184
182
        # s3 = read_stanza(self.stanzas_to_str([s]))
185
 
        # self.assertEqual(s, s3)
 
183
        # self.assertEquals(s, s3)
186
184
 
187
185
    def test_read_empty(self):
188
186
        """Detect end of rio file"""
190
188
        self.assertEqual(s, None)
191
189
        self.assertTrue(s is None)
192
190
 
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
 
 
201
191
    def test_read_iter(self):
202
192
        """Read several stanzas from file"""
203
193
        tmpf = TemporaryFile()
238
228
""")
239
229
        tmpf.seek(0)
240
230
        s = read_stanza(tmpf)
241
 
        self.assertEqual(s, Stanza(version_header='1'))
 
231
        self.assertEquals(s, Stanza(version_header='1'))
242
232
        s = read_stanza(tmpf)
243
 
        self.assertEqual(s, Stanza(name="foo", val='123'))
 
233
        self.assertEquals(s, Stanza(name="foo", val='123'))
244
234
        s = read_stanza(tmpf)
245
235
        self.assertEqualDiff(s.get('name'), 'quoted')
246
236
        self.assertEqualDiff(s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
247
237
        s = read_stanza(tmpf)
248
 
        self.assertEqual(s, Stanza(name="bar", val='129319'))
 
238
        self.assertEquals(s, Stanza(name="bar", val='129319'))
249
239
        s = read_stanza(tmpf)
250
 
        self.assertEqual(s, None)
 
240
        self.assertEquals(s, None)
251
241
        self.check_rio_file(tmpf)
252
242
 
253
243
    def check_rio_file(self, real_file):
254
244
        real_file.seek(0)
255
245
        read_write = rio_file(RioReader(real_file)).read()
256
246
        real_file.seek(0)
257
 
        self.assertEqual(read_write, real_file.read())
 
247
        self.assertEquals(read_write, real_file.read())
258
248
 
259
249
    @staticmethod
260
250
    def stanzas_to_str(stanzas):
315
305
        for expected in expected_vals:
316
306
            stanza = read_stanza(tmpf)
317
307
            self.rio_file_stanzas([stanza])
318
 
            self.assertEqual(len(stanza), 1)
 
308
            self.assertEquals(len(stanza), 1)
319
309
            self.assertEqualDiff(stanza.get('s'), expected)
320
310
 
321
311
    def test_write_empty_stanza(self):
322
312
        """Write empty stanza"""
323
313
        l = list(Stanza().to_lines())
324
 
        self.assertEqual(l, [])
 
314
        self.assertEquals(l, [])
325
315
 
326
316
    def test_rio_raises_type_error(self):
327
317
        """TypeError on adding invalid type to Stanza"""
336
326
    def test_rio_unicode(self):
337
327
        uni_data = u'\N{KATAKANA LETTER O}'
338
328
        s = Stanza(foo=uni_data)
339
 
        self.assertEqual(s.get('foo'), uni_data)
 
329
        self.assertEquals(s.get('foo'), uni_data)
340
330
        raw_lines = s.to_lines()
341
 
        self.assertEqual(raw_lines,
 
331
        self.assertEquals(raw_lines,
342
332
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
343
333
        new_s = read_stanza(raw_lines)
344
 
        self.assertEqual(new_s.get('foo'), uni_data)
 
334
        self.assertEquals(new_s.get('foo'), uni_data)
345
335
 
346
336
    def test_rio_to_unicode(self):
347
337
        uni_data = u'\N{KATAKANA LETTER O}'