/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: Jelmer Vernooij
  • Date: 2009-10-27 21:54:26 UTC
  • mfrom: (4771 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4833.
  • Revision ID: jelmer@samba.org-20091027215426-72164bkd4mq9dsd4
merge bzr.dev.

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
 
104
105
        args_300 = ['a']*300
105
106
        self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
106
107
        # not a string
107
 
        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)
108
145
        
109
146
    def test_as_tuple(self):
110
147
        k = self.module.StaticTuple('foo')
177
214
        self.assertFalse(k1 < k2)
178
215
        self.assertFalse(k1 > k2)
179
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
 
180
259
    def test_compare_same_obj(self):
181
260
        k1 = self.module.StaticTuple('foo', 'bar')
182
261
        self.assertCompareEqual(k1, k1)
183
262
        k2 = self.module.StaticTuple(k1, k1)
184
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)
185
267
 
186
268
    def test_compare_equivalent_obj(self):
187
269
        k1 = self.module.StaticTuple('foo', 'bar')
190
272
        k3 = self.module.StaticTuple(k1, k2)
191
273
        k4 = self.module.StaticTuple(k2, k1)
192
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)
193
283
 
194
284
    def test_compare_similar_obj(self):
195
285
        k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
240
330
        k3 = self.module.StaticTuple(k1, k2)
241
331
        k4 = self.module.StaticTuple(k2, k1)
242
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)
243
342
 
244
343
    def test_compare_some_different(self):
245
344
        k1 = self.module.StaticTuple('foo', 'bar')
248
347
        k3 = self.module.StaticTuple(k1, k1)
249
348
        k4 = self.module.StaticTuple(k1, k2)
250
349
        self.assertCompareDifferent(k3, k4)
 
350
        k5 = self.module.StaticTuple('foo', None)
 
351
        self.assertCompareDifferent(k5, k1)
 
352
        self.assertCompareDifferent(k5, k2)
251
353
 
252
354
    def test_compare_diff_width(self):
253
355
        k1 = self.module.StaticTuple('foo')
257
359
        k4 = self.module.StaticTuple(k1, k2)
258
360
        self.assertCompareDifferent(k3, k4)
259
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
 
260
374
    def test_compare_to_tuples(self):
261
375
        k1 = self.module.StaticTuple('foo')
262
376
        self.assertCompareEqual(k1, ('foo',))
306
420
        as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
307
421
        self.assertEqual(hash(k2), hash(as_tuple2))
308
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
 
309
428
    def test_slice(self):
310
429
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
311
430
        self.assertEqual(('foo', 'bar'), k[:2])
459
578
        self.assertRaises(TypeError,
460
579
                          self.module.StaticTuple.from_sequence, foo='a')
461
580
 
 
581
    def test_pickle(self):
 
582
        st = self.module.StaticTuple('foo', 'bar')
 
583
        pickled = cPickle.dumps(st)
 
584
        unpickled = cPickle.loads(pickled)
 
585
        self.assertEqual(unpickled, st)
 
586
 
 
587
    def test_pickle_empty(self):
 
588
        st = self.module.StaticTuple()
 
589
        pickled = cPickle.dumps(st)
 
590
        unpickled = cPickle.loads(pickled)
 
591
        self.assertIs(st, unpickled)
 
592
 
 
593
    def test_pickle_nested(self):
 
594
        st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
 
595
        pickled = cPickle.dumps(st)
 
596
        unpickled = cPickle.loads(pickled)
 
597
        self.assertEqual(unpickled, st)
 
598
 
462
599
    def test_static_tuple_thunk(self):
463
600
        # Make sure the right implementation is available from
464
601
        # bzrlib.static_tuple.StaticTuple.