~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__chk_map.py

  • Committer: Vincent Ladeuil
  • Date: 2010-01-25 15:55:48 UTC
  • mto: (4985.1.4 add-attr-cleanup)
  • mto: This revision was merged to the branch mainline in revision 4988.
  • Revision ID: v.ladeuil+lp@free.fr-20100125155548-0l352pujvt5bzl5e
Deploy addAttrCleanup on the whole test suite.

Several use case worth mentioning:

- setting a module or any other object attribute is the majority
by far. In some cases the setting itself is deferred but most of
the time we want to set at the same time we add the cleanup.

- there multiple occurrences of protecting hooks or ui factory
which are now useless (the test framework takes care of that now),

- there was some lambda uses that can now be avoided.

That first cleanup already simplifies things a lot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    chk_map,
21
21
    tests,
22
22
    )
 
23
from bzrlib.static_tuple import StaticTuple
 
24
stuple = StaticTuple
23
25
 
24
26
 
25
27
def load_tests(standard_tests, module, loader):
26
 
    # parameterize all tests in this module
27
 
    suite = loader.suiteClass()
28
 
    import bzrlib._chk_map_py as py_module
29
 
    scenarios = [('python', {'module': py_module})]
30
 
    if CompiledChkMapFeature.available():
31
 
        import bzrlib._chk_map_pyx as c_module
32
 
        scenarios.append(('C', {'module': c_module}))
33
 
    else:
34
 
        # the compiled module isn't available, so we add a failing test
35
 
        class FailWithoutFeature(tests.TestCase):
36
 
            def test_fail(self):
37
 
                self.requireFeature(CompiledChkMapFeature)
38
 
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
39
 
    tests.multiply_tests(standard_tests, scenarios, suite)
 
28
    suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
 
29
        'bzrlib._chk_map_py', 'bzrlib._chk_map_pyx')
40
30
    return suite
41
31
 
42
32
 
43
 
class _CompiledChkMapFeature(tests.Feature):
44
 
 
45
 
    def _probe(self):
46
 
        try:
47
 
            import bzrlib._chk_map_pyx
48
 
        except ImportError:
49
 
            return False
50
 
        return True
51
 
 
52
 
    def feature_name(self):
53
 
        return 'bzrlib._chk_map_pyx'
54
 
 
55
 
CompiledChkMapFeature = _CompiledChkMapFeature()
56
 
 
57
 
 
58
33
class TestSearchKeys(tests.TestCase):
59
34
 
60
35
    module = None # Filled in by test parameterization
67
42
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
68
43
 
69
44
    def test_simple_16(self):
70
 
        self.assertSearchKey16('8C736521', ('foo',))
71
 
        self.assertSearchKey16('8C736521\x008C736521', ('foo', 'foo'))
72
 
        self.assertSearchKey16('8C736521\x0076FF8CAA', ('foo', 'bar'))
73
 
        self.assertSearchKey16('ED82CD11', ('abcd',))
 
45
        self.assertSearchKey16('8C736521', stuple('foo',))
 
46
        self.assertSearchKey16('8C736521\x008C736521', stuple('foo', 'foo'))
 
47
        self.assertSearchKey16('8C736521\x0076FF8CAA', stuple('foo', 'bar'))
 
48
        self.assertSearchKey16('ED82CD11', stuple('abcd',))
74
49
 
75
50
    def test_simple_255(self):
76
 
        self.assertSearchKey255('\x8cse!', ('foo',))
77
 
        self.assertSearchKey255('\x8cse!\x00\x8cse!', ('foo', 'foo'))
78
 
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', ('foo', 'bar'))
 
51
        self.assertSearchKey255('\x8cse!', stuple('foo',))
 
52
        self.assertSearchKey255('\x8cse!\x00\x8cse!', stuple('foo', 'foo'))
 
53
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', stuple('foo', 'bar'))
79
54
        # The standard mapping for these would include '\n', so it should be
80
55
        # mapped to '_'
81
 
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', ('<', 'V'))
 
56
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
82
57
 
83
58
    def test_255_does_not_include_newline(self):
84
59
        # When mapping via _search_key_255, we should never have the '\n'
85
60
        # character, but all other 255 values should be present
86
61
        chars_used = set()
87
62
        for char_in in range(256):
88
 
            search_key = self.module._search_key_255((chr(char_in),))
 
63
            search_key = self.module._search_key_255(stuple(chr(char_in),))
89
64
            chars_used.update(search_key)
90
65
        all_chars = set([chr(x) for x in range(256)])
91
66
        unused_chars = all_chars.symmetric_difference(chars_used)
113
88
 
114
89
    def test_deserialise_empty(self):
115
90
        node = self.module._deserialise_leaf_node(
116
 
            "chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
 
91
            "chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
117
92
        self.assertEqual(0, len(node))
118
93
        self.assertEqual(10, node.maximum_size)
119
94
        self.assertEqual(("sha1:1234",), node.key())
 
95
        self.assertIsInstance(node.key(), StaticTuple)
120
96
        self.assertIs(None, node._search_prefix)
121
97
        self.assertIs(None, node._common_serialised_prefix)
122
98
 
194
170
 
195
171
    def assertDeserialiseErrors(self, text):
196
172
        self.assertRaises((ValueError, IndexError),
197
 
            self.module._deserialise_internal_node, text, 'not-a-real-sha')
 
173
            self.module._deserialise_internal_node, text,
 
174
                stuple('not-a-real-sha',))
198
175
 
199
176
    def test_raises_on_non_internal(self):
200
177
        self.assertDeserialiseErrors('')
211
188
 
212
189
    def test_deserialise_one(self):
213
190
        node = self.module._deserialise_internal_node(
214
 
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", ('sha1:1234',))
 
191
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
215
192
        self.assertIsInstance(node, chk_map.InternalNode)
216
193
        self.assertEqual(1, len(node))
217
194
        self.assertEqual(10, node.maximum_size)
221
198
 
222
199
    def test_deserialise_with_prefix(self):
223
200
        node = self.module._deserialise_internal_node(
224
 
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", ('sha1:1234',))
 
201
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
225
202
        self.assertIsInstance(node, chk_map.InternalNode)
226
203
        self.assertEqual(1, len(node))
227
204
        self.assertEqual(10, node.maximum_size)
230
207
        self.assertEqual({'prefa': ('sha1:abcd',)}, node._items)
231
208
 
232
209
        node = self.module._deserialise_internal_node(
233
 
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", ('sha1:1234',))
 
210
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
234
211
        self.assertIsInstance(node, chk_map.InternalNode)
235
212
        self.assertEqual(1, len(node))
236
213
        self.assertEqual(10, node.maximum_size)
240
217
 
241
218
    def test_deserialise_pref_with_null(self):
242
219
        node = self.module._deserialise_internal_node(
243
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n", ('sha1:1234',))
 
220
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
 
221
            stuple('sha1:1234',))
244
222
        self.assertIsInstance(node, chk_map.InternalNode)
245
223
        self.assertEqual(1, len(node))
246
224
        self.assertEqual(10, node.maximum_size)
250
228
 
251
229
    def test_deserialise_with_null_pref(self):
252
230
        node = self.module._deserialise_internal_node(
253
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n", ('sha1:1234',))
 
231
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
 
232
            stuple('sha1:1234',))
254
233
        self.assertIsInstance(node, chk_map.InternalNode)
255
234
        self.assertEqual(1, len(node))
256
235
        self.assertEqual(10, node.maximum_size)