~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__bencode.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 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
19
19
from bzrlib import tests
20
20
 
21
21
def load_tests(standard_tests, module, loader):
22
 
    # parameterize all tests in this module
23
 
    suite = loader.suiteClass()
24
 
    import bzrlib.util._bencode_py as py_module
25
 
    scenarios = [('python', {'bencode': py_module})]
26
 
    if CompiledBencodeFeature.available():
27
 
        import bzrlib._bencode_pyx as c_module
28
 
        scenarios.append(('C', {'bencode': c_module}))
29
 
    else:
30
 
        # the compiled module isn't available, so we add a failing test
31
 
        class FailWithoutFeature(tests.TestCase):
32
 
            def test_fail(self):
33
 
                self.requireFeature(CompiledBencodeFeature)
34
 
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
35
 
    tests.multiply_tests(standard_tests, scenarios, suite)
 
22
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
 
23
        'bzrlib.util._bencode_py', 'bzrlib._bencode_pyx')
36
24
    return suite
37
25
 
38
26
 
39
 
class _CompiledBencodeFeature(tests.Feature):
40
 
 
41
 
    def _probe(self):
42
 
        try:
43
 
            import bzrlib._bencode_pyx
44
 
        except ImportError:
45
 
            return False
46
 
        return True
47
 
 
48
 
    def feature_name(self):
49
 
        return 'bzrlib._bencode_pyx'
50
 
 
51
 
CompiledBencodeFeature = _CompiledBencodeFeature()
52
 
 
53
 
 
54
27
class TestBencodeDecode(tests.TestCase):
55
28
 
56
 
    bencode = None
 
29
    module = None
57
30
 
58
31
    def _check(self, expected, source):
59
 
        self.assertEquals(expected, self.bencode.bdecode(source))
 
32
        self.assertEquals(expected, self.module.bdecode(source))
60
33
 
61
34
    def _run_check_error(self, exc, bad):
62
35
        """Check that bdecoding a string raises a particular exception."""
63
 
        self.assertRaises(exc, self.bencode.bdecode, bad)
 
36
        self.assertRaises(exc, self.module.bdecode, bad)
64
37
 
65
38
    def test_int(self):
66
39
        self._check(0, 'i0e')
91
64
        self._check('1234567890', '10:1234567890')
92
65
 
93
66
    def test_large_string(self):
94
 
        self.assertRaises(ValueError, self.bencode.bdecode, "2147483639:foo")
 
67
        self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo")
95
68
 
96
69
    def test_malformed_string(self):
97
70
        self._run_check_error(ValueError, '10:x')
146
119
        self._run_check_error(ValueError, 'd432432432432432432:e')
147
120
 
148
121
    def test_empty_string(self):
149
 
        self.assertRaises(ValueError, self.bencode.bdecode, '')
 
122
        self.assertRaises(ValueError, self.module.bdecode, '')
150
123
 
151
124
    def test_junk(self):
152
125
        self._run_check_error(ValueError, 'i6easd')
155
128
        self._run_check_error(ValueError, 'leanfdldjfh')
156
129
 
157
130
    def test_unknown_object(self):
158
 
        self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
 
131
        self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh')
159
132
 
160
133
    def test_unsupported_type(self):
161
134
        self._run_check_error(TypeError, float(1.5))
165
138
        self._run_check_error(TypeError, u"ie")
166
139
 
167
140
    def test_decoder_type_error(self):
168
 
        self.assertRaises(TypeError, self.bencode.bdecode, 1)
 
141
        self.assertRaises(TypeError, self.module.bdecode, 1)
169
142
 
170
143
 
171
144
class TestBencodeEncode(tests.TestCase):
172
145
 
173
 
    bencode = None
 
146
    module = None
174
147
 
175
148
    def _check(self, expected, source):
176
 
        self.assertEquals(expected, self.bencode.bencode(source))
 
149
        self.assertEquals(expected, self.module.bencode(source))
177
150
 
178
151
    def test_int(self):
179
152
        self._check('i4e', 4)
205
178
        for i in range(10000):
206
179
            l.append([])
207
180
            l = l[0]
208
 
        self.assertRaises(RuntimeError, self.bencode.bencode, 
 
181
        self.assertRaises(RuntimeError, self.module.bencode, 
209
182
            top)
210
183
 
211
184
    def test_dict(self):
220
193
        for i in range(10000):
221
194
            d[''] = {}
222
195
            d = d['']
223
 
        self.assertRaises(RuntimeError, self.bencode.bencode, 
 
196
        self.assertRaises(RuntimeError, self.module.bencode, 
224
197
            top)
225
198
 
226
199
    def test_bencached(self):
227
 
        self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
 
200
        self._check('i3e', self.module.Bencached(self.module.bencode(3)))
228
201
 
229
202
    def test_invalid_dict(self):
230
 
        self.assertRaises(TypeError, self.bencode.bencode, {1:"foo"})
 
203
        self.assertRaises(TypeError, self.module.bencode, {1:"foo"})
231
204
 
232
205
    def test_bool(self):
233
206
        self._check('i1e', True)