/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__static_tuple.py

  • Committer: Andrew Bennetts
  • Date: 2009-10-28 00:12:03 UTC
  • mfrom: (4774 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4775.
  • Revision ID: andrew.bennetts@canonical.com-20091028001203-m7lgs1wtnilgo3br
Merge lp:bzr, resolving NEWS conflict.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the StaticTuple type."""
18
18
 
 
19
import cPickle
19
20
import gc
20
21
import sys
21
22
 
23
24
    _static_tuple_py,
24
25
    errors,
25
26
    osutils,
 
27
    static_tuple,
26
28
    tests,
27
29
    )
28
30
 
103
105
        args_300 = ['a']*300
104
106
        self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
105
107
        # not a string
106
 
        self.assertRaises(TypeError, self.module.StaticTuple, 10)
 
108
        self.assertRaises(TypeError, self.module.StaticTuple, object())
 
109
 
 
110
    def test_concat(self):
 
111
        st1 = self.module.StaticTuple('foo')
 
112
        st2 = self.module.StaticTuple('bar')
 
113
        st3 = self.module.StaticTuple('foo', 'bar')
 
114
        st4 = st1 + st2
 
115
        self.assertEqual(st3, st4)
 
116
        self.assertIsInstance(st4, self.module.StaticTuple)
 
117
 
 
118
    def test_concat_with_tuple(self):
 
119
        st1 = self.module.StaticTuple('foo')
 
120
        t2 = ('bar',)
 
121
        st3 = self.module.StaticTuple('foo', 'bar')
 
122
        st4 = self.module.StaticTuple('bar', 'foo')
 
123
        st5 = st1 + t2
 
124
        st6 = t2 + st1
 
125
        self.assertEqual(st3, st5)
 
126
        self.assertIsInstance(st5, self.module.StaticTuple)
 
127
        self.assertEqual(st4, st6)
 
128
        if self.module is _static_tuple_py:
 
129
            # _static_tuple_py has StaticTuple(tuple), so tuple thinks it
 
130
            # already knows how to concatenate, as such we can't "inject" our
 
131
            # own concatenation...
 
132
            self.assertIsInstance(st6, tuple)
 
133
        else:
 
134
            self.assertIsInstance(st6, self.module.StaticTuple)
 
135
 
 
136
    def test_concat_with_bad_tuple(self):
 
137
        st1 = self.module.StaticTuple('foo')
 
138
        t2 = (object(),)
 
139
        # Using st1.__add__ doesn't give the same results as doing the '+' form
 
140
        self.assertRaises(TypeError, lambda: st1 + t2)
 
141
 
 
142
    def test_concat_with_non_tuple(self):
 
143
        st1 = self.module.StaticTuple('foo')
 
144
        self.assertRaises(TypeError, lambda: st1 + 10)
107
145
        
108
146
    def test_as_tuple(self):
109
147
        k = self.module.StaticTuple('foo')
176
214
        self.assertFalse(k1 < k2)
177
215
        self.assertFalse(k1 > k2)
178
216
 
 
217
    def test_holds_None(self):
 
218
        k1 = self.module.StaticTuple(None)
 
219
        # You cannot subclass None anyway
 
220
 
 
221
    def test_holds_int(self):
 
222
        k1 = self.module.StaticTuple(1)
 
223
        class subint(int):
 
224
            pass
 
225
        # But not a subclass, because subint could introduce refcycles
 
226
        self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
 
227
 
 
228
    def test_holds_long(self):
 
229
        k1 = self.module.StaticTuple(2L**65)
 
230
        class sublong(long):
 
231
            pass
 
232
        # But not a subclass
 
233
        self.assertRaises(TypeError, self.module.StaticTuple, sublong(1))
 
234
 
 
235
    def test_holds_float(self):
 
236
        k1 = self.module.StaticTuple(1.2)
 
237
        class subfloat(float):
 
238
            pass
 
239
        self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
 
240
 
 
241
    def test_holds_str(self):
 
242
        k1 = self.module.StaticTuple('astring')
 
243
        class substr(str):
 
244
            pass
 
245
        self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
 
246
 
 
247
    def test_holds_unicode(self):
 
248
        k1 = self.module.StaticTuple(u'\xb5')
 
249
        class subunicode(unicode):
 
250
            pass
 
251
        self.assertRaises(TypeError, self.module.StaticTuple,
 
252
                          subunicode(u'\xb5'))
 
253
 
 
254
    def test_hold_bool(self):
 
255
        k1 = self.module.StaticTuple(True)
 
256
        k2 = self.module.StaticTuple(False)
 
257
        # Cannot subclass bool
 
258
 
179
259
    def test_compare_same_obj(self):
180
260
        k1 = self.module.StaticTuple('foo', 'bar')
181
261
        self.assertCompareEqual(k1, k1)
182
262
        k2 = self.module.StaticTuple(k1, k1)
183
263
        self.assertCompareEqual(k2, k2)
 
264
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
265
                                     k1)
 
266
        self.assertCompareEqual(k3, k3)
184
267
 
185
268
    def test_compare_equivalent_obj(self):
186
269
        k1 = self.module.StaticTuple('foo', 'bar')
189
272
        k3 = self.module.StaticTuple(k1, k2)
190
273
        k4 = self.module.StaticTuple(k2, k1)
191
274
        self.assertCompareEqual(k1, k2)
 
275
        k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
276
                                     k1)
 
277
        k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
278
                                     k1)
 
279
        self.assertCompareEqual(k5, k6)
 
280
        k7 = self.module.StaticTuple(None)
 
281
        k8 = self.module.StaticTuple(None)
 
282
        self.assertCompareEqual(k7, k8)
192
283
 
193
284
    def test_compare_similar_obj(self):
194
285
        k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
239
330
        k3 = self.module.StaticTuple(k1, k2)
240
331
        k4 = self.module.StaticTuple(k2, k1)
241
332
        self.assertCompareDifferent(k3, k4)
 
333
        k5 = self.module.StaticTuple(1)
 
334
        k6 = self.module.StaticTuple(2)
 
335
        self.assertCompareDifferent(k5, k6)
 
336
        k7 = self.module.StaticTuple(1.2)
 
337
        k8 = self.module.StaticTuple(2.4)
 
338
        self.assertCompareDifferent(k7, k8)
 
339
        k9 = self.module.StaticTuple(u's\xb5')
 
340
        k10 = self.module.StaticTuple(u's\xe5')
 
341
        self.assertCompareDifferent(k9, k10)
242
342
 
243
343
    def test_compare_some_different(self):
244
344
        k1 = self.module.StaticTuple('foo', 'bar')
247
347
        k3 = self.module.StaticTuple(k1, k1)
248
348
        k4 = self.module.StaticTuple(k1, k2)
249
349
        self.assertCompareDifferent(k3, k4)
 
350
        k5 = self.module.StaticTuple('foo', None)
 
351
        self.assertCompareDifferent(k5, k1)
 
352
        self.assertCompareDifferent(k5, k2)
250
353
 
251
354
    def test_compare_diff_width(self):
252
355
        k1 = self.module.StaticTuple('foo')
256
359
        k4 = self.module.StaticTuple(k1, k2)
257
360
        self.assertCompareDifferent(k3, k4)
258
361
 
 
362
    def test_compare_different_types(self):
 
363
        k1 = self.module.StaticTuple('foo', 'bar')
 
364
        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
365
                                     k1)
 
366
        self.assertCompareNoRelation(k1, k2)
 
367
        k3 = self.module.StaticTuple('foo')
 
368
        self.assertCompareDifferent(k3, k1)
 
369
        k4 = self.module.StaticTuple(None)
 
370
        self.assertCompareDifferent(k4, k1)
 
371
        k5 = self.module.StaticTuple(1)
 
372
        self.assertCompareNoRelation(k1, k5)
 
373
 
259
374
    def test_compare_to_tuples(self):
260
375
        k1 = self.module.StaticTuple('foo')
261
376
        self.assertCompareEqual(k1, ('foo',))
278
393
        self.assertCompareEqual(k3, (k1, ('foo', 'bar')))
279
394
        self.assertCompareEqual((k1, ('foo', 'bar')), k3)
280
395
 
 
396
    def test_compare_mixed_depths(self):
 
397
        stuple = self.module.StaticTuple
 
398
        k1 = stuple(stuple('a',), stuple('b',))
 
399
        k2 = stuple(stuple(stuple('c',), stuple('d',)),
 
400
                    stuple('b',))
 
401
        # This requires comparing a StaticTuple to a 'string', and then
 
402
        # interpreting that value in the next higher StaticTuple. This used to
 
403
        # generate a PyErr_BadIternalCall. We now fall back to *something*.
 
404
        self.assertCompareNoRelation(k1, k2)
 
405
 
281
406
    def test_hash(self):
282
407
        k = self.module.StaticTuple('foo')
283
408
        self.assertEqual(hash(k), hash(('foo',)))
295
420
        as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
296
421
        self.assertEqual(hash(k2), hash(as_tuple2))
297
422
 
 
423
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
424
                                     k)
 
425
        as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k)
 
426
        self.assertEqual(hash(as_tuple3), hash(k3))
 
427
 
298
428
    def test_slice(self):
299
429
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
300
430
        self.assertEqual(('foo', 'bar'), k[:2])
416
546
        if self.module is _static_tuple_py:
417
547
            return
418
548
        self.assertIsNot(None, self.module._C_API)
 
549
 
 
550
    def test_from_sequence_tuple(self):
 
551
        st = self.module.StaticTuple.from_sequence(('foo', 'bar'))
 
552
        self.assertIsInstance(st, self.module.StaticTuple)
 
553
        self.assertEqual(('foo', 'bar'), st)
 
554
 
 
555
    def test_from_sequence_str(self):
 
556
        st = self.module.StaticTuple.from_sequence('foo')
 
557
        self.assertIsInstance(st, self.module.StaticTuple)
 
558
        self.assertEqual(('f', 'o', 'o'), st)
 
559
 
 
560
    def test_from_sequence_list(self):
 
561
        st = self.module.StaticTuple.from_sequence(['foo', 'bar'])
 
562
        self.assertIsInstance(st, self.module.StaticTuple)
 
563
        self.assertEqual(('foo', 'bar'), st)
 
564
 
 
565
    def test_from_sequence_static_tuple(self):
 
566
        st = self.module.StaticTuple('foo', 'bar')
 
567
        st2 = self.module.StaticTuple.from_sequence(st)
 
568
        # If the source is a StaticTuple already, we return the exact object
 
569
        self.assertIs(st, st2)
 
570
 
 
571
    def test_from_sequence_not_sequence(self):
 
572
        self.assertRaises(TypeError,
 
573
                          self.module.StaticTuple.from_sequence, object())
 
574
        self.assertRaises(TypeError,
 
575
                          self.module.StaticTuple.from_sequence, 10)
 
576
 
 
577
    def test_from_sequence_incorrect_args(self):
 
578
        self.assertRaises(TypeError,
 
579
                          self.module.StaticTuple.from_sequence, object(), 'a')
 
580
        self.assertRaises(TypeError,
 
581
                          self.module.StaticTuple.from_sequence, foo='a')
 
582
 
 
583
    def test_from_sequence_iterable(self):
 
584
        st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
 
585
        self.assertIsInstance(st, self.module.StaticTuple)
 
586
        self.assertEqual(('foo', 'bar'), st)
 
587
 
 
588
    def test_from_sequence_generator(self):
 
589
        def generate_tuple():
 
590
            yield 'foo'
 
591
            yield 'bar'
 
592
        st = self.module.StaticTuple.from_sequence(generate_tuple())
 
593
        self.assertIsInstance(st, self.module.StaticTuple)
 
594
        self.assertEqual(('foo', 'bar'), st)
 
595
 
 
596
    def test_pickle(self):
 
597
        st = self.module.StaticTuple('foo', 'bar')
 
598
        pickled = cPickle.dumps(st)
 
599
        unpickled = cPickle.loads(pickled)
 
600
        self.assertEqual(unpickled, st)
 
601
 
 
602
    def test_pickle_empty(self):
 
603
        st = self.module.StaticTuple()
 
604
        pickled = cPickle.dumps(st)
 
605
        unpickled = cPickle.loads(pickled)
 
606
        self.assertIs(st, unpickled)
 
607
 
 
608
    def test_pickle_nested(self):
 
609
        st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
 
610
        pickled = cPickle.dumps(st)
 
611
        unpickled = cPickle.loads(pickled)
 
612
        self.assertEqual(unpickled, st)
 
613
 
 
614
    def test_static_tuple_thunk(self):
 
615
        # Make sure the right implementation is available from
 
616
        # bzrlib.static_tuple.StaticTuple.
 
617
        if self.module is _static_tuple_py:
 
618
            if CompiledStaticTuple.available():
 
619
                # We will be using the C version
 
620
                return
 
621
        self.assertIs(static_tuple.StaticTuple,
 
622
                      self.module.StaticTuple)