~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__static_tuple.py

  • Committer: John Arbash Meinel
  • Date: 2009-10-21 16:52:18 UTC
  • mfrom: (4761 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4771.
  • Revision ID: john@arbash-meinel.com-20091021165218-rxk2tt2ks7amc6m9
Bring in bzr.dev 4761 which includes CHKMap and CHKInventory tweaks.
It also brings in StaticTuple concatenation, and ability to hold None, etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
        args_300 = ['a']*300
105
105
        self.assertRaises(ValueError, self.module.StaticTuple, *args_300)
106
106
        # not a string
107
 
        self.assertRaises(TypeError, self.module.StaticTuple, 10)
 
107
        self.assertRaises(TypeError, self.module.StaticTuple, object())
 
108
 
 
109
    def test_concat(self):
 
110
        st1 = self.module.StaticTuple('foo')
 
111
        st2 = self.module.StaticTuple('bar')
 
112
        st3 = self.module.StaticTuple('foo', 'bar')
 
113
        st4 = st1 + st2
 
114
        self.assertEqual(st3, st4)
 
115
        self.assertIsInstance(st4, self.module.StaticTuple)
 
116
 
 
117
    def test_concat_with_tuple(self):
 
118
        st1 = self.module.StaticTuple('foo')
 
119
        t2 = ('bar',)
 
120
        st3 = self.module.StaticTuple('foo', 'bar')
 
121
        st4 = self.module.StaticTuple('bar', 'foo')
 
122
        st5 = st1 + t2
 
123
        st6 = t2 + st1
 
124
        self.assertEqual(st3, st5)
 
125
        self.assertIsInstance(st5, self.module.StaticTuple)
 
126
        self.assertEqual(st4, st6)
 
127
        if self.module is _static_tuple_py:
 
128
            # _static_tuple_py has StaticTuple(tuple), so tuple thinks it
 
129
            # already knows how to concatenate, as such we can't "inject" our
 
130
            # own concatenation...
 
131
            self.assertIsInstance(st6, tuple)
 
132
        else:
 
133
            self.assertIsInstance(st6, self.module.StaticTuple)
 
134
 
 
135
    def test_concat_with_bad_tuple(self):
 
136
        st1 = self.module.StaticTuple('foo')
 
137
        t2 = (object(),)
 
138
        # Using st1.__add__ doesn't give the same results as doing the '+' form
 
139
        self.assertRaises(TypeError, lambda: st1 + t2)
 
140
 
 
141
    def test_concat_with_non_tuple(self):
 
142
        st1 = self.module.StaticTuple('foo')
 
143
        self.assertRaises(TypeError, lambda: st1 + 10)
108
144
        
109
145
    def test_as_tuple(self):
110
146
        k = self.module.StaticTuple('foo')
177
213
        self.assertFalse(k1 < k2)
178
214
        self.assertFalse(k1 > k2)
179
215
 
 
216
    def test_holds_None(self):
 
217
        k1 = self.module.StaticTuple(None)
 
218
        # You cannot subclass None anyway
 
219
 
 
220
    def test_holds_int(self):
 
221
        k1 = self.module.StaticTuple(1)
 
222
        class subint(int):
 
223
            pass
 
224
        # But not a subclass, because subint could introduce refcycles
 
225
        self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
 
226
 
 
227
    def test_holds_long(self):
 
228
        k1 = self.module.StaticTuple(2L**65)
 
229
        class sublong(long):
 
230
            pass
 
231
        # But not a subclass
 
232
        self.assertRaises(TypeError, self.module.StaticTuple, sublong(1))
 
233
 
 
234
    def test_holds_float(self):
 
235
        k1 = self.module.StaticTuple(1.2)
 
236
        class subfloat(float):
 
237
            pass
 
238
        self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
 
239
 
 
240
    def test_holds_str(self):
 
241
        k1 = self.module.StaticTuple('astring')
 
242
        class substr(str):
 
243
            pass
 
244
        self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
 
245
 
 
246
    def test_holds_unicode(self):
 
247
        k1 = self.module.StaticTuple(u'\xb5')
 
248
        class subunicode(unicode):
 
249
            pass
 
250
        self.assertRaises(TypeError, self.module.StaticTuple,
 
251
                          subunicode(u'\xb5'))
 
252
 
 
253
    def test_hold_bool(self):
 
254
        k1 = self.module.StaticTuple(True)
 
255
        k2 = self.module.StaticTuple(False)
 
256
        # Cannot subclass bool
 
257
 
180
258
    def test_compare_same_obj(self):
181
259
        k1 = self.module.StaticTuple('foo', 'bar')
182
260
        self.assertCompareEqual(k1, k1)
183
261
        k2 = self.module.StaticTuple(k1, k1)
184
262
        self.assertCompareEqual(k2, k2)
 
263
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
264
                                     k1)
 
265
        self.assertCompareEqual(k3, k3)
185
266
 
186
267
    def test_compare_equivalent_obj(self):
187
268
        k1 = self.module.StaticTuple('foo', 'bar')
190
271
        k3 = self.module.StaticTuple(k1, k2)
191
272
        k4 = self.module.StaticTuple(k2, k1)
192
273
        self.assertCompareEqual(k1, k2)
 
274
        k5 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
275
                                     k1)
 
276
        k6 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
277
                                     k1)
 
278
        self.assertCompareEqual(k5, k6)
 
279
        k7 = self.module.StaticTuple(None)
 
280
        k8 = self.module.StaticTuple(None)
 
281
        self.assertCompareEqual(k7, k8)
193
282
 
194
283
    def test_compare_similar_obj(self):
195
284
        k1 = self.module.StaticTuple('foo' + ' bar', 'bar' + ' baz')
240
329
        k3 = self.module.StaticTuple(k1, k2)
241
330
        k4 = self.module.StaticTuple(k2, k1)
242
331
        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)
243
341
 
244
342
    def test_compare_some_different(self):
245
343
        k1 = self.module.StaticTuple('foo', 'bar')
248
346
        k3 = self.module.StaticTuple(k1, k1)
249
347
        k4 = self.module.StaticTuple(k1, k2)
250
348
        self.assertCompareDifferent(k3, k4)
 
349
        k5 = self.module.StaticTuple('foo', None)
 
350
        self.assertCompareDifferent(k5, k1)
 
351
        self.assertCompareDifferent(k5, k2)
251
352
 
252
353
    def test_compare_diff_width(self):
253
354
        k1 = self.module.StaticTuple('foo')
257
358
        k4 = self.module.StaticTuple(k1, k2)
258
359
        self.assertCompareDifferent(k3, k4)
259
360
 
 
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,
 
364
                                     k1)
 
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)
 
372
 
260
373
    def test_compare_to_tuples(self):
261
374
        k1 = self.module.StaticTuple('foo')
262
375
        self.assertCompareEqual(k1, ('foo',))
306
419
        as_tuple2 = (('foo', 'bar', 'baz', 'bing'),)
307
420
        self.assertEqual(hash(k2), hash(as_tuple2))
308
421
 
 
422
        k3 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
 
423
                                     k)
 
424
        as_tuple3 = ('foo', 1, None, u'\xb5', 1.2, 2**65, True, k)
 
425
        self.assertEqual(hash(as_tuple3), hash(k3))
 
426
 
309
427
    def test_slice(self):
310
428
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
311
429
        self.assertEqual(('foo', 'bar'), k[:2])