~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_bencode.py

  • Committer: Jelmer Vernooij
  • Date: 2009-05-26 10:56:27 UTC
  • mto: (4398.5.1 bencode_serializer)
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: jelmer@samba.org-20090526105627-i2q9w3rl05xn6r5z
Use standard infrastructure for testing python and pyrex bencode implementations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for bencode structured encoding"""
18
18
 
19
 
from bzrlib.tests import TestCase, Feature
20
 
 
21
 
from bzrlib import bencode as bencode_default
22
 
from bzrlib import _bencode_py as bencode_py
23
 
try:
24
 
    from bzrlib import _bencode_c as bencode_c
25
 
except ImportError:
26
 
    bencode_c = None
27
 
 
28
 
 
29
 
class _BencodeCFeature(Feature):
 
19
from bzrlib import tests
 
20
 
 
21
def load_tests(standard_tests, module, loader):
 
22
    # parameterize all tests in this module
 
23
    suite = loader.suiteClass()
 
24
    import bzrlib._bencode_py as py_module
 
25
    scenarios = [('python', {'bencode': py_module})]
 
26
    if CompiledBencodeFeature.available():
 
27
        import bzrlib._bencode_c 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)
 
36
    return suite
 
37
 
 
38
 
 
39
class _CompiledBencodeFeature(tests.Feature):
30
40
 
31
41
    def _probe(self):
32
 
        return bencode_c is not None
 
42
        try:
 
43
            import bzrlib._bencode_c
 
44
        except ImportError:
 
45
            return False
 
46
        return True
33
47
 
34
48
    def feature_name(self):
35
49
        return 'bzrlib._bencode_c'
36
50
 
37
 
BencodeCFeature = _BencodeCFeature()
38
 
 
39
 
 
40
 
class TestBencodeDecode(TestCase):
41
 
 
42
 
    bencode = bencode_default
 
51
CompiledBencodeFeature = _CompiledBencodeFeature()
 
52
 
 
53
 
 
54
class TestBencodeDecode(tests.TestCase):
 
55
 
 
56
    bencode = None
43
57
 
44
58
    def _check(self, expected, source):
45
59
        self.assertEquals(expected, self.bencode.bdecode(source))
121
135
    def test_unknown_object(self):
122
136
        self._check_error('relwjhrlewjh')
123
137
 
124
 
 
125
 
class TestBencodeEncode(TestCase):
126
 
 
127
 
    bencode = bencode_default
 
138
    def test_unsupported_type(self):
 
139
        self._check_error(float(1.5))
 
140
        self._check_error(None)
 
141
        self._check_error(lambda x: x)
 
142
        self._check_error(object)
 
143
 
 
144
 
 
145
class TestBencodeEncode(tests.TestCase):
 
146
 
 
147
    bencode = None
128
148
 
129
149
    def _check(self, expected, source):
130
150
        self.assertEquals(expected, self.bencode.bencode(source))
184
204
                         ('i0e', False)])
185
205
 
186
206
 
187
 
class TestBencodePyDecode(TestBencodeDecode):
188
 
    bencode = bencode_py
189
 
 
190
 
 
191
 
class TestBencodePyEncode(TestBencodeEncode):
192
 
    bencode = bencode_py
193
 
 
194
 
 
195
 
class TestBencodeCDecode(TestBencodeDecode):
196
 
    _test_needs_features = [BencodeCFeature]
197
 
    bencode = bencode_c
198
 
 
199
 
 
200
 
class TestBencodeCEncode(TestBencodeEncode):
201
 
    _test_needs_features = [BencodeCFeature]
202
 
    bencode = bencode_c
203
 
 
204
 
    def test_unsupported_type(self):
205
 
        self._check_error(float(1.5))
206
 
        self._check_error(None)
207
 
        self._check_error(lambda x: x)
208
 
        self._check_error(object)
209
 
 
210
 
 
211
 
class TestBencodeC(TestCase):
212
 
 
213
 
    _test_needs_features = [BencodeCFeature]
 
207
class TestBencodeC(tests.TestCase):
 
208
 
 
209
    bencode = None
214
210
 
215
211
    def test_decoder_repr(self):
216
 
        self.assertEquals("Decoder('123')", repr(bencode_c.Decoder('123')))
 
212
        self.assertEquals("Decoder('123')", repr(self.bencode.Decoder('123')))
217
213
 
218
214
    def test_decoder_type_error(self):
219
 
        self.assertRaises(TypeError, bencode_c.Decoder, 1)
 
215
        self.assertRaises(TypeError, self.bencode.Decoder, 1)
220
216
 
221
217
    def test_encoder_buffer_overflow(self):
222
 
        e = bencode_c.Encoder(256)
 
218
        e = self.bencode.Encoder(256)
223
219
        shouldbe = []
224
220
        for i in '1234567890':
225
221
            s = i * 124
230
226
        self.assertEquals(''.join(shouldbe), str(e))
231
227
 
232
228
    def test_encoder_buffer_overflow2(self):
233
 
        e = bencode_c.Encoder(4)
 
229
        e = self.bencode.Encoder(4)
234
230
        e.process('1234567890')
235
231
        self.assertEquals(64, e.maxsize)
236
232
        self.assertEquals('10:1234567890', str(e))