17
17
"""Tests for the StaticTuple type."""
22
22
from bzrlib import (
31
30
def load_tests(standard_tests, module, loader):
32
31
"""Parameterize tests for all versions of groupcompress."""
33
global compiled_static_tuple_feature
34
suite, compiled_static_tuple_feature = tests.permute_tests_for_extension(
35
standard_tests, loader, 'bzrlib._static_tuple_py',
36
'bzrlib._static_tuple_c')
33
('python', {'module': _static_tuple_py}),
35
suite = loader.suiteClass()
36
if CompiledStaticTuple.available():
37
from bzrlib import _static_tuple_c
38
scenarios.append(('C', {'module': _static_tuple_c}))
40
# the compiled module isn't available, so we add a failing test
41
class FailWithoutFeature(tests.TestCase):
43
self.requireFeature(CompiledStaticTuple)
44
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
45
result = tests.multiply_tests(standard_tests, scenarios, suite)
49
class _CompiledStaticTuple(tests.Feature):
53
import bzrlib._static_tuple_c
58
def feature_name(self):
59
return 'bzrlib._static_tuple_c'
61
CompiledStaticTuple = _CompiledStaticTuple()
40
64
class _Meliae(tests.Feature):
75
99
def test_create_bad_args(self):
76
100
args_256 = ['a']*256
78
self.assertRaises(TypeError, self.module.StaticTuple, *args_256)
102
self.assertRaises(ValueError, self.module.StaticTuple, *args_256)
79
103
args_300 = ['a']*300
80
self.assertRaises(TypeError, self.module.StaticTuple, *args_300)
104
self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
82
self.assertRaises(TypeError, self.module.StaticTuple, object())
84
def test_concat(self):
85
st1 = self.module.StaticTuple('foo')
86
st2 = self.module.StaticTuple('bar')
87
st3 = self.module.StaticTuple('foo', 'bar')
89
self.assertEqual(st3, st4)
90
self.assertIsInstance(st4, self.module.StaticTuple)
92
def test_concat_with_tuple(self):
93
st1 = self.module.StaticTuple('foo')
95
st3 = self.module.StaticTuple('foo', 'bar')
96
st4 = self.module.StaticTuple('bar', 'foo')
99
self.assertEqual(st3, st5)
100
self.assertIsInstance(st5, self.module.StaticTuple)
101
self.assertEqual(st4, st6)
102
if self.module is _static_tuple_py:
103
# _static_tuple_py has StaticTuple(tuple), so tuple thinks it
104
# already knows how to concatenate, as such we can't "inject" our
105
# own concatenation...
106
self.assertIsInstance(st6, tuple)
108
self.assertIsInstance(st6, self.module.StaticTuple)
110
def test_concat_with_bad_tuple(self):
111
st1 = self.module.StaticTuple('foo')
113
# Using st1.__add__ doesn't give the same results as doing the '+' form
114
self.assertRaises(TypeError, lambda: st1 + t2)
116
def test_concat_with_non_tuple(self):
117
st1 = self.module.StaticTuple('foo')
118
self.assertRaises(TypeError, lambda: st1 + 10)
106
self.assertRaises(TypeError, self.module.StaticTuple, 10)
120
108
def test_as_tuple(self):
121
109
k = self.module.StaticTuple('foo')
123
111
self.assertEqual(('foo',), t)
124
self.assertIsInstance(t, tuple)
125
self.assertFalse(isinstance(t, self.module.StaticTuple))
126
112
k = self.module.StaticTuple('foo', 'bar')
128
114
self.assertEqual(('foo', 'bar'), t)
129
k2 = self.module.StaticTuple(1, k)
131
self.assertIsInstance(t, tuple)
132
# For pickling to work, we need to keep the sub-items as StaticTuple so
133
# that it knows that they also need to be converted.
134
self.assertIsInstance(t[1], self.module.StaticTuple)
135
self.assertEqual((1, ('foo', 'bar')), t)
137
def test_as_tuples(self):
138
k1 = self.module.StaticTuple('foo', 'bar')
139
t = static_tuple.as_tuples(k1)
140
self.assertIsInstance(t, tuple)
141
self.assertEqual(('foo', 'bar'), t)
142
k2 = self.module.StaticTuple(1, k1)
143
t = static_tuple.as_tuples(k2)
144
self.assertIsInstance(t, tuple)
145
self.assertIsInstance(t[1], tuple)
146
self.assertEqual((1, ('foo', 'bar')), t)
148
t = static_tuple.as_tuples(mixed)
149
self.assertIsInstance(t, tuple)
150
self.assertIsInstance(t[1], tuple)
151
self.assertEqual((1, ('foo', 'bar')), t)
153
116
def test_len(self):
154
117
k = self.module.StaticTuple()
213
176
self.assertFalse(k1 < k2)
214
177
self.assertFalse(k1 > k2)
216
def test_holds_None(self):
217
k1 = self.module.StaticTuple(None)
218
# You cannot subclass None anyway
220
def test_holds_int(self):
221
k1 = self.module.StaticTuple(1)
224
# But not a subclass, because subint could introduce refcycles
225
self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
227
def test_holds_long(self):
228
k1 = self.module.StaticTuple(2L**65)
232
self.assertRaises(TypeError, self.module.StaticTuple, sublong(1))
234
def test_holds_float(self):
235
k1 = self.module.StaticTuple(1.2)
236
class subfloat(float):
238
self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
240
def test_holds_str(self):
241
k1 = self.module.StaticTuple('astring')
244
self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
246
def test_holds_unicode(self):
247
k1 = self.module.StaticTuple(u'\xb5')
248
class subunicode(unicode):
250
self.assertRaises(TypeError, self.module.StaticTuple,
253
def test_hold_bool(self):
254
k1 = self.module.StaticTuple(True)
255
k2 = self.module.StaticTuple(False)
256
# Cannot subclass bool
258
179
def test_compare_same_obj(self):
259
180
k1 = self.module.StaticTuple('foo', 'bar')
260
181
self.assertCompareEqual(k1, k1)
261
182
k2 = self.module.StaticTuple(k1, k1)
262
183
self.assertCompareEqual(k2, k2)
263
k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
265
self.assertCompareEqual(k3, k3)
267
185
def test_compare_equivalent_obj(self):
268
186
k1 = self.module.StaticTuple('foo', 'bar')
271
189
k3 = self.module.StaticTuple(k1, k2)
272
190
k4 = self.module.StaticTuple(k2, k1)
273
191
self.assertCompareEqual(k1, k2)
274
k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
276
k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
278
self.assertCompareEqual(k5, k6)
279
k7 = self.module.StaticTuple(None)
280
k8 = self.module.StaticTuple(None)
281
self.assertCompareEqual(k7, k8)
283
193
def test_compare_similar_obj(self):
284
194
k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
329
239
k3 = self.module.StaticTuple(k1, k2)
330
240
k4 = self.module.StaticTuple(k2, k1)
331
241
self.assertCompareDifferent(k3, k4)
332
k5 = self.module.StaticTuple(1)
333
k6 = self.module.StaticTuple(2)
334
self.assertCompareDifferent(k5, k6)
335
k7 = self.module.StaticTuple(1.2)
336
k8 = self.module.StaticTuple(2.4)
337
self.assertCompareDifferent(k7, k8)
338
k9 = self.module.StaticTuple(u's\xb5')
339
k10 = self.module.StaticTuple(u's\xe5')
340
self.assertCompareDifferent(k9, k10)
342
243
def test_compare_some_different(self):
343
244
k1 = self.module.StaticTuple('foo', 'bar')
358
256
k4 = self.module.StaticTuple(k1, k2)
359
257
self.assertCompareDifferent(k3, k4)
361
def test_compare_different_types(self):
362
k1 = self.module.StaticTuple('foo', 'bar')
363
k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
365
self.assertCompareNoRelation(k1, k2)
366
k3 = self.module.StaticTuple('foo')
367
self.assertCompareDifferent(k3, k1)
368
k4 = self.module.StaticTuple(None)
369
self.assertCompareDifferent(k4, k1)
370
k5 = self.module.StaticTuple(1)
371
self.assertCompareNoRelation(k1, k5)
373
259
def test_compare_to_tuples(self):
374
260
k1 = self.module.StaticTuple('foo')
375
261
self.assertCompareEqual(k1, ('foo',))
392
278
self.assertCompareEqual(k3, (k1, ('foo', 'bar')))
393
279
self.assertCompareEqual((k1, ('foo', 'bar')), k3)
395
def test_compare_mixed_depths(self):
396
stuple = self.module.StaticTuple
397
k1 = stuple(stuple('a',), stuple('b',))
398
k2 = stuple(stuple(stuple('c',), stuple('d',)),
400
# This requires comparing a StaticTuple to a 'string', and then
401
# interpreting that value in the next higher StaticTuple. This used to
402
# generate a PyErr_BadIternalCall. We now fall back to *something*.
403
self.assertCompareNoRelation(k1, k2)
405
281
def test_hash(self):
406
282
k = self.module.StaticTuple('foo')
407
283
self.assertEqual(hash(k), hash(('foo',)))
419
295
as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
420
296
self.assertEqual(hash(k2), hash(as_tuple2))
422
k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
424
as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k)
425
self.assertEqual(hash(as_tuple3), hash(k3))
427
298
def test_slice(self):
428
299
k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
429
300
self.assertEqual(('foo', 'bar'), k[:2])
545
416
if self.module is _static_tuple_py:
547
418
self.assertIsNot(None, self.module._C_API)
549
def test_from_sequence_tuple(self):
550
st = self.module.StaticTuple.from_sequence(('foo', 'bar'))
551
self.assertIsInstance(st, self.module.StaticTuple)
552
self.assertEqual(('foo', 'bar'), st)
554
def test_from_sequence_str(self):
555
st = self.module.StaticTuple.from_sequence('foo')
556
self.assertIsInstance(st, self.module.StaticTuple)
557
self.assertEqual(('f', 'o', 'o'), st)
559
def test_from_sequence_list(self):
560
st = self.module.StaticTuple.from_sequence(['foo', 'bar'])
561
self.assertIsInstance(st, self.module.StaticTuple)
562
self.assertEqual(('foo', 'bar'), st)
564
def test_from_sequence_static_tuple(self):
565
st = self.module.StaticTuple('foo', 'bar')
566
st2 = self.module.StaticTuple.from_sequence(st)
567
# If the source is a StaticTuple already, we return the exact object
568
self.assertIs(st, st2)
570
def test_from_sequence_not_sequence(self):
571
self.assertRaises(TypeError,
572
self.module.StaticTuple.from_sequence, object())
573
self.assertRaises(TypeError,
574
self.module.StaticTuple.from_sequence, 10)
576
def test_from_sequence_incorrect_args(self):
577
self.assertRaises(TypeError,
578
self.module.StaticTuple.from_sequence, object(), 'a')
579
self.assertRaises(TypeError,
580
self.module.StaticTuple.from_sequence, foo='a')
582
def test_from_sequence_iterable(self):
583
st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
584
self.assertIsInstance(st, self.module.StaticTuple)
585
self.assertEqual(('foo', 'bar'), st)
587
def test_from_sequence_generator(self):
588
def generate_tuple():
591
st = self.module.StaticTuple.from_sequence(generate_tuple())
592
self.assertIsInstance(st, self.module.StaticTuple)
593
self.assertEqual(('foo', 'bar'), st)
595
def test_pickle(self):
596
st = self.module.StaticTuple('foo', 'bar')
597
pickled = cPickle.dumps(st)
598
unpickled = cPickle.loads(pickled)
599
self.assertEqual(unpickled, st)
601
def test_pickle_empty(self):
602
st = self.module.StaticTuple()
603
pickled = cPickle.dumps(st)
604
unpickled = cPickle.loads(pickled)
605
self.assertIs(st, unpickled)
607
def test_pickle_nested(self):
608
st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
609
pickled = cPickle.dumps(st)
610
unpickled = cPickle.loads(pickled)
611
self.assertEqual(unpickled, st)
613
def test_static_tuple_thunk(self):
614
# Make sure the right implementation is available from
615
# bzrlib.static_tuple.StaticTuple.
616
if self.module is _static_tuple_py:
617
if compiled_static_tuple_feature.available():
618
# We will be using the C version
620
self.assertIs(static_tuple.StaticTuple,
621
self.module.StaticTuple)
624
class TestEnsureStaticTuple(tests.TestCase):
626
def test_is_static_tuple(self):
627
st = static_tuple.StaticTuple('foo')
628
st2 = static_tuple.expect_static_tuple(st)
629
self.assertIs(st, st2)
631
def test_is_tuple(self):
633
st = static_tuple.expect_static_tuple(t)
634
self.assertIsInstance(st, static_tuple.StaticTuple)
635
self.assertEqual(t, st)
637
def test_flagged_is_static_tuple(self):
638
debug.debug_flags.add('static_tuple')
639
st = static_tuple.StaticTuple('foo')
640
st2 = static_tuple.expect_static_tuple(st)
641
self.assertIs(st, st2)
643
def test_flagged_is_tuple(self):
644
debug.debug_flags.add('static_tuple')
646
self.assertRaises(TypeError, static_tuple.expect_static_tuple, t)