~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: 2010-10-05 21:15:13 UTC
  • mfrom: (5448.3.5 374700-Add-gnu-lsh-support)
  • Revision ID: pqm@pqm.ubuntu.com-20101005211513-whouyj5t7oo92gmq
(gz) Add support for GNU lsh as a secure shell client (Matthew Gordon)

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, 2007, 2009, 2010 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"""
238
236
""")
239
237
        tmpf.seek(0)
240
238
        s = read_stanza(tmpf)
241
 
        self.assertEqual(s, Stanza(version_header='1'))
 
239
        self.assertEquals(s, Stanza(version_header='1'))
242
240
        s = read_stanza(tmpf)
243
 
        self.assertEqual(s, Stanza(name="foo", val='123'))
 
241
        self.assertEquals(s, Stanza(name="foo", val='123'))
244
242
        s = read_stanza(tmpf)
245
243
        self.assertEqualDiff(s.get('name'), 'quoted')
246
244
        self.assertEqualDiff(s.get('address'), '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
247
245
        s = read_stanza(tmpf)
248
 
        self.assertEqual(s, Stanza(name="bar", val='129319'))
 
246
        self.assertEquals(s, Stanza(name="bar", val='129319'))
249
247
        s = read_stanza(tmpf)
250
 
        self.assertEqual(s, None)
 
248
        self.assertEquals(s, None)
251
249
        self.check_rio_file(tmpf)
252
250
 
253
251
    def check_rio_file(self, real_file):
254
252
        real_file.seek(0)
255
253
        read_write = rio_file(RioReader(real_file)).read()
256
254
        real_file.seek(0)
257
 
        self.assertEqual(read_write, real_file.read())
 
255
        self.assertEquals(read_write, real_file.read())
258
256
 
259
257
    @staticmethod
260
258
    def stanzas_to_str(stanzas):
315
313
        for expected in expected_vals:
316
314
            stanza = read_stanza(tmpf)
317
315
            self.rio_file_stanzas([stanza])
318
 
            self.assertEqual(len(stanza), 1)
 
316
            self.assertEquals(len(stanza), 1)
319
317
            self.assertEqualDiff(stanza.get('s'), expected)
320
318
 
321
319
    def test_write_empty_stanza(self):
322
320
        """Write empty stanza"""
323
321
        l = list(Stanza().to_lines())
324
 
        self.assertEqual(l, [])
 
322
        self.assertEquals(l, [])
325
323
 
326
324
    def test_rio_raises_type_error(self):
327
325
        """TypeError on adding invalid type to Stanza"""
336
334
    def test_rio_unicode(self):
337
335
        uni_data = u'\N{KATAKANA LETTER O}'
338
336
        s = Stanza(foo=uni_data)
339
 
        self.assertEqual(s.get('foo'), uni_data)
 
337
        self.assertEquals(s.get('foo'), uni_data)
340
338
        raw_lines = s.to_lines()
341
 
        self.assertEqual(raw_lines,
 
339
        self.assertEquals(raw_lines,
342
340
                ['foo: ' + uni_data.encode('utf-8') + '\n'])
343
341
        new_s = read_stanza(raw_lines)
344
 
        self.assertEqual(new_s.get('foo'), uni_data)
 
342
        self.assertEquals(new_s.get('foo'), uni_data)
345
343
 
346
344
    def test_rio_to_unicode(self):
347
345
        uni_data = u'\N{KATAKANA LETTER O}'