~jelmer/brz/fix-c-extensions

« back to all changes in this revision

Viewing changes to breezy/tests/test__static_tuple.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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
try:
 
20
    import cPickle as pickle
 
21
except ImportError:
 
22
    import pickle
 
23
import operator
20
24
import sys
21
25
 
22
26
from breezy import (
26
30
    static_tuple,
27
31
    tests,
28
32
    )
 
33
from breezy.sixish import (
 
34
    PY3,
 
35
    text_type,
 
36
    )
29
37
from breezy.tests import (
30
38
    features,
31
39
    )
213
221
        self.assertRaises(TypeError, self.module.StaticTuple, subint(2))
214
222
 
215
223
    def test_holds_long(self):
 
224
        if PY3:
 
225
            self.skipTest("No long type on Python 3")
216
226
        k1 = self.module.StaticTuple(2**65)
217
227
        class sublong(long):
218
228
            pass
225
235
            pass
226
236
        self.assertRaises(TypeError, self.module.StaticTuple, subfloat(1.5))
227
237
 
228
 
    def test_holds_str(self):
229
 
        k1 = self.module.StaticTuple('astring')
230
 
        class substr(str):
 
238
    def test_holds_bytes(self):
 
239
        k1 = self.module.StaticTuple(b'astring')
 
240
        class substr(bytes):
231
241
            pass
232
 
        self.assertRaises(TypeError, self.module.StaticTuple, substr('a'))
 
242
        self.assertRaises(TypeError, self.module.StaticTuple, substr(b'a'))
233
243
 
234
244
    def test_holds_unicode(self):
235
245
        k1 = self.module.StaticTuple(u'\xb5')
236
 
        class subunicode(unicode):
 
246
        class subunicode(text_type):
237
247
            pass
238
248
        self.assertRaises(TypeError, self.module.StaticTuple,
239
249
                          subunicode(u'\xb5'))
278
288
        k6 = self.module.StaticTuple(k3, k4)
279
289
        self.assertCompareEqual(k5, k6)
280
290
 
281
 
    def assertCompareDifferent(self, k_small, k_big):
 
291
    def check_strict_compare(self, k1, k2, mismatched_types):
 
292
        """True if on Python 3 and stricter comparison semantics are used."""
 
293
        if PY3 and mismatched_types:
 
294
            for op in ("ge", "gt", "le", "lt"):
 
295
                self.assertRaises(TypeError, getattr(operator, op), k1, k2)
 
296
            return True
 
297
        return False
 
298
 
 
299
    def assertCompareDifferent(self, k_small, k_big, mismatched_types=False):
282
300
        self.assertFalse(k_small == k_big)
283
 
        self.assertFalse(k_small >= k_big)
284
 
        self.assertFalse(k_small > k_big)
285
301
        self.assertTrue(k_small != k_big)
286
 
        self.assertTrue(k_small <= k_big)
287
 
        self.assertTrue(k_small < k_big)
 
302
        if not self.check_strict_compare(k_small, k_big, mismatched_types):
 
303
            self.assertFalse(k_small >= k_big)
 
304
            self.assertFalse(k_small > k_big)
 
305
            self.assertTrue(k_small <= k_big)
 
306
            self.assertTrue(k_small < k_big)
288
307
 
289
 
    def assertCompareNoRelation(self, k1, k2):
 
308
    def assertCompareNoRelation(self, k1, k2, mismatched_types=False):
290
309
        """Run the comparison operators, make sure they do something.
291
310
 
292
311
        However, we don't actually care what comes first or second. This is
295
314
        """
296
315
        self.assertFalse(k1 == k2)
297
316
        self.assertTrue(k1 != k2)
298
 
        # Do the comparison, but we don't care about the result
299
 
        k1 >= k2
300
 
        k1 > k2
301
 
        k1 <= k2
302
 
        k1 < k2
 
317
        if not self.check_strict_compare(k1, k2, mismatched_types):
 
318
            # Do the comparison, but we don't care about the result
 
319
            k1 >= k2
 
320
            k1 > k2
 
321
            k1 <= k2
 
322
            k1 < k2
303
323
 
304
324
    def test_compare_vs_none(self):
305
325
        k1 = self.module.StaticTuple('baz', 'bing')
306
 
        self.assertCompareDifferent(None, k1)
 
326
        self.assertCompareDifferent(None, k1, mismatched_types=True)
307
327
    
308
328
    def test_compare_cross_class(self):
309
329
        k1 = self.module.StaticTuple('baz', 'bing')
310
 
        self.assertCompareNoRelation(10, k1)
311
 
        self.assertCompareNoRelation('baz', k1)
 
330
        self.assertCompareNoRelation(10, k1, mismatched_types=True)
 
331
        self.assertCompareNoRelation('baz', k1, mismatched_types=True)
312
332
 
313
333
    def test_compare_all_different_same_width(self):
314
334
        k1 = self.module.StaticTuple('baz', 'bing')
335
355
        k4 = self.module.StaticTuple(k1, k2)
336
356
        self.assertCompareDifferent(k3, k4)
337
357
        k5 = self.module.StaticTuple('foo', None)
338
 
        self.assertCompareDifferent(k5, k1)
339
 
        self.assertCompareDifferent(k5, k2)
 
358
        self.assertCompareDifferent(k5, k1, mismatched_types=True)
 
359
        self.assertCompareDifferent(k5, k2, mismatched_types=True)
340
360
 
341
361
    def test_compare_diff_width(self):
342
362
        k1 = self.module.StaticTuple('foo')
350
370
        k1 = self.module.StaticTuple('foo', 'bar')
351
371
        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
352
372
                                     k1)
353
 
        self.assertCompareNoRelation(k1, k2)
 
373
        self.assertCompareNoRelation(k1, k2, mismatched_types=True)
354
374
        k3 = self.module.StaticTuple('foo')
355
375
        self.assertCompareDifferent(k3, k1)
356
376
        k4 = self.module.StaticTuple(None)
357
 
        self.assertCompareDifferent(k4, k1)
 
377
        self.assertCompareDifferent(k4, k1, mismatched_types=True)
358
378
        k5 = self.module.StaticTuple(1)
359
 
        self.assertCompareNoRelation(k1, k5)
 
379
        self.assertCompareNoRelation(k1, k5, mismatched_types=True)
360
380
 
361
381
    def test_compare_to_tuples(self):
362
382
        k1 = self.module.StaticTuple('foo')
372
392
        self.assertCompareDifferent(('foo',), k2)
373
393
        self.assertCompareDifferent(('foo', 'aaa'), k2)
374
394
        self.assertCompareDifferent(('baz', 'bing'), k2)
375
 
        self.assertCompareDifferent(('foo', 10), k2)
 
395
        self.assertCompareDifferent(('foo', 10), k2, mismatched_types=True)
376
396
 
377
397
        k3 = self.module.StaticTuple(k1, k2)
378
398
        self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
388
408
        # This requires comparing a StaticTuple to a 'string', and then
389
409
        # interpreting that value in the next higher StaticTuple. This used to
390
410
        # generate a PyErr_BadIternalCall. We now fall back to *something*.
391
 
        self.assertCompareNoRelation(k1, k2)
 
411
        self.assertCompareNoRelation(k1, k2, mismatched_types=True)
392
412
 
393
413
    def test_hash(self):
394
414
        k = self.module.StaticTuple('foo')
416
436
        k = self.module.StaticTuple('foo', 'bar', 'baz', 'bing')
417
437
        self.assertEqual(('foo', 'bar'), k[:2])
418
438
        self.assertEqual(('baz',), k[2:-1])
419
 
        try:
420
 
            val = k[::2]
421
 
        except TypeError:
422
 
            # C implementation raises a TypeError, we don't need the
423
 
            # implementation yet, so allow this to pass
424
 
            pass
425
 
        else:
426
 
            # Python implementation uses a regular Tuple, so make sure it gives
427
 
            # the right result
428
 
            self.assertEqual(('foo', 'baz'), val)
 
439
        self.assertEqual(('foo', 'baz',), k[::2])
 
440
        self.assertRaises(TypeError, k.__getitem__, 'not_slice')
429
441
 
430
442
    def test_referents(self):
431
443
        # We implement tp_traverse so that things like 'meliae' can measure the
582
594
 
583
595
    def test_pickle(self):
584
596
        st = self.module.StaticTuple('foo', 'bar')
585
 
        pickled = cPickle.dumps(st)
586
 
        unpickled = cPickle.loads(pickled)
 
597
        pickled = pickle.dumps(st)
 
598
        unpickled = pickle.loads(pickled)
587
599
        self.assertEqual(unpickled, st)
588
600
 
589
601
    def test_pickle_empty(self):
590
602
        st = self.module.StaticTuple()
591
 
        pickled = cPickle.dumps(st)
592
 
        unpickled = cPickle.loads(pickled)
 
603
        pickled = pickle.dumps(st)
 
604
        unpickled = pickle.loads(pickled)
593
605
        self.assertIs(st, unpickled)
594
606
 
595
607
    def test_pickle_nested(self):
596
608
        st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
597
 
        pickled = cPickle.dumps(st)
598
 
        unpickled = cPickle.loads(pickled)
 
609
        pickled = pickle.dumps(st)
 
610
        unpickled = pickle.loads(pickled)
599
611
        self.assertEqual(unpickled, st)
600
612
 
601
613
    def test_static_tuple_thunk(self):