17
17
"""Tests for bencode structured encoding"""
19
from bzrlib.tests import TestCase, Feature
21
from bzrlib import bencode as bencode_default
22
from bzrlib import _bencode_py as bencode_py
24
from bzrlib import _bencode_c as bencode_c
29
class _BencodeCFeature(Feature):
19
from bzrlib import tests
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}))
30
# the compiled module isn't available, so we add a failing test
31
class FailWithoutFeature(tests.TestCase):
33
self.requireFeature(CompiledBencodeFeature)
34
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
35
tests.multiply_tests(standard_tests, scenarios, suite)
39
class _CompiledBencodeFeature(tests.Feature):
32
return bencode_c is not None
43
import bzrlib._bencode_c
34
48
def feature_name(self):
35
49
return 'bzrlib._bencode_c'
37
BencodeCFeature = _BencodeCFeature()
40
class TestBencodeDecode(TestCase):
42
bencode = bencode_default
51
CompiledBencodeFeature = _CompiledBencodeFeature()
54
class TestBencodeDecode(tests.TestCase):
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')
125
class TestBencodeEncode(TestCase):
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)
145
class TestBencodeEncode(tests.TestCase):
129
149
def _check(self, expected, source):
130
150
self.assertEquals(expected, self.bencode.bencode(source))
187
class TestBencodePyDecode(TestBencodeDecode):
191
class TestBencodePyEncode(TestBencodeEncode):
195
class TestBencodeCDecode(TestBencodeDecode):
196
_test_needs_features = [BencodeCFeature]
200
class TestBencodeCEncode(TestBencodeEncode):
201
_test_needs_features = [BencodeCFeature]
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)
211
class TestBencodeC(TestCase):
213
_test_needs_features = [BencodeCFeature]
207
class TestBencodeC(tests.TestCase):
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')))
218
214
def test_decoder_type_error(self):
219
self.assertRaises(TypeError, bencode_c.Decoder, 1)
215
self.assertRaises(TypeError, self.bencode.Decoder, 1)
221
217
def test_encoder_buffer_overflow(self):
222
e = bencode_c.Encoder(256)
218
e = self.bencode.Encoder(256)
224
220
for i in '1234567890':
230
226
self.assertEquals(''.join(shouldbe), str(e))
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))