~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__bencode.py

(jam) Add bzrlib.tests.permute_for_extension to simplify extension
        testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009 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 compiled_bencode_feature.available():
27
 
        scenarios.append(('C', {'bencode': compiled_bencode_feature.module}))
28
 
    else:
29
 
        # the compiled module isn't available, so we add a failing test
30
 
        class FailWithoutFeature(tests.TestCase):
31
 
            def test_fail(self):
32
 
                self.requireFeature(compiled_bencode_feature)
33
 
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
34
 
    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')
35
24
    return suite
36
25
 
37
26
 
38
 
compiled_bencode_feature = tests.ModuleAvailableFeature('bzrlib._bencode_pyx')
39
 
 
40
 
 
41
27
class TestBencodeDecode(tests.TestCase):
42
28
 
43
 
    bencode = None
 
29
    module = None
44
30
 
45
31
    def _check(self, expected, source):
46
 
        self.assertEquals(expected, self.bencode.bdecode(source))
 
32
        self.assertEquals(expected, self.module.bdecode(source))
47
33
 
48
34
    def _run_check_error(self, exc, bad):
49
35
        """Check that bdecoding a string raises a particular exception."""
50
 
        self.assertRaises(exc, self.bencode.bdecode, bad)
 
36
        self.assertRaises(exc, self.module.bdecode, bad)
51
37
 
52
38
    def test_int(self):
53
39
        self._check(0, 'i0e')
78
64
        self._check('1234567890', '10:1234567890')
79
65
 
80
66
    def test_large_string(self):
81
 
        self.assertRaises(ValueError, self.bencode.bdecode, "2147483639:foo")
 
67
        self.assertRaises(ValueError, self.module.bdecode, "2147483639:foo")
82
68
 
83
69
    def test_malformed_string(self):
84
70
        self._run_check_error(ValueError, '10:x')
133
119
        self._run_check_error(ValueError, 'd432432432432432432:e')
134
120
 
135
121
    def test_empty_string(self):
136
 
        self.assertRaises(ValueError, self.bencode.bdecode, '')
 
122
        self.assertRaises(ValueError, self.module.bdecode, '')
137
123
 
138
124
    def test_junk(self):
139
125
        self._run_check_error(ValueError, 'i6easd')
142
128
        self._run_check_error(ValueError, 'leanfdldjfh')
143
129
 
144
130
    def test_unknown_object(self):
145
 
        self.assertRaises(ValueError, self.bencode.bdecode, 'relwjhrlewjh')
 
131
        self.assertRaises(ValueError, self.module.bdecode, 'relwjhrlewjh')
146
132
 
147
133
    def test_unsupported_type(self):
148
134
        self._run_check_error(TypeError, float(1.5))
152
138
        self._run_check_error(TypeError, u"ie")
153
139
 
154
140
    def test_decoder_type_error(self):
155
 
        self.assertRaises(TypeError, self.bencode.bdecode, 1)
 
141
        self.assertRaises(TypeError, self.module.bdecode, 1)
156
142
 
157
143
 
158
144
class TestBencodeEncode(tests.TestCase):
159
145
 
160
 
    bencode = None
 
146
    module = None
161
147
 
162
148
    def _check(self, expected, source):
163
 
        self.assertEquals(expected, self.bencode.bencode(source))
 
149
        self.assertEquals(expected, self.module.bencode(source))
164
150
 
165
151
    def test_int(self):
166
152
        self._check('i4e', 4)
192
178
        for i in range(10000):
193
179
            l.append([])
194
180
            l = l[0]
195
 
        self.assertRaises(RuntimeError, self.bencode.bencode, 
 
181
        self.assertRaises(RuntimeError, self.module.bencode, 
196
182
            top)
197
183
 
198
184
    def test_dict(self):
207
193
        for i in range(10000):
208
194
            d[''] = {}
209
195
            d = d['']
210
 
        self.assertRaises(RuntimeError, self.bencode.bencode, 
 
196
        self.assertRaises(RuntimeError, self.module.bencode, 
211
197
            top)
212
198
 
213
199
    def test_bencached(self):
214
 
        self._check('i3e', self.bencode.Bencached(self.bencode.bencode(3)))
 
200
        self._check('i3e', self.module.Bencached(self.module.bencode(3)))
215
201
 
216
202
    def test_invalid_dict(self):
217
 
        self.assertRaises(TypeError, self.bencode.bencode, {1:"foo"})
 
203
        self.assertRaises(TypeError, self.module.bencode, {1:"foo"})
218
204
 
219
205
    def test_bool(self):
220
206
        self._check('i1e', True)