/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 breezy/tests/test__static_tuple.py

Make StaticTuple tests pass on Python 3

Merged from https://code.launchpad.net/~gz/brz/py3_static_tuple_mismatched_types/+merge/326330

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    import cPickle as pickle
21
21
except ImportError:
22
22
    import pickle
 
23
import operator
23
24
import sys
24
25
 
25
26
from breezy import (
287
288
        k6 = self.module.StaticTuple(k3, k4)
288
289
        self.assertCompareEqual(k5, k6)
289
290
 
290
 
    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):
291
300
        self.assertFalse(k_small == k_big)
292
 
        self.assertFalse(k_small >= k_big)
293
 
        self.assertFalse(k_small > k_big)
294
301
        self.assertTrue(k_small != k_big)
295
 
        self.assertTrue(k_small <= k_big)
296
 
        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)
297
307
 
298
 
    def assertCompareNoRelation(self, k1, k2):
 
308
    def assertCompareNoRelation(self, k1, k2, mismatched_types=False):
299
309
        """Run the comparison operators, make sure they do something.
300
310
 
301
311
        However, we don't actually care what comes first or second. This is
304
314
        """
305
315
        self.assertFalse(k1 == k2)
306
316
        self.assertTrue(k1 != k2)
307
 
        # Do the comparison, but we don't care about the result
308
 
        k1 >= k2
309
 
        k1 > k2
310
 
        k1 <= k2
311
 
        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
312
323
 
313
324
    def test_compare_vs_none(self):
314
325
        k1 = self.module.StaticTuple('baz', 'bing')
315
 
        self.assertCompareDifferent(None, k1)
 
326
        self.assertCompareDifferent(None, k1, mismatched_types=True)
316
327
    
317
328
    def test_compare_cross_class(self):
318
329
        k1 = self.module.StaticTuple('baz', 'bing')
319
 
        self.assertCompareNoRelation(10, k1)
320
 
        self.assertCompareNoRelation('baz', k1)
 
330
        self.assertCompareNoRelation(10, k1, mismatched_types=True)
 
331
        self.assertCompareNoRelation('baz', k1, mismatched_types=True)
321
332
 
322
333
    def test_compare_all_different_same_width(self):
323
334
        k1 = self.module.StaticTuple('baz', 'bing')
344
355
        k4 = self.module.StaticTuple(k1, k2)
345
356
        self.assertCompareDifferent(k3, k4)
346
357
        k5 = self.module.StaticTuple('foo', None)
347
 
        self.assertCompareDifferent(k5, k1)
348
 
        self.assertCompareDifferent(k5, k2)
 
358
        self.assertCompareDifferent(k5, k1, mismatched_types=True)
 
359
        self.assertCompareDifferent(k5, k2, mismatched_types=True)
349
360
 
350
361
    def test_compare_diff_width(self):
351
362
        k1 = self.module.StaticTuple('foo')
359
370
        k1 = self.module.StaticTuple('foo', 'bar')
360
371
        k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
361
372
                                     k1)
362
 
        self.assertCompareNoRelation(k1, k2)
 
373
        self.assertCompareNoRelation(k1, k2, mismatched_types=True)
363
374
        k3 = self.module.StaticTuple('foo')
364
375
        self.assertCompareDifferent(k3, k1)
365
376
        k4 = self.module.StaticTuple(None)
366
 
        self.assertCompareDifferent(k4, k1)
 
377
        self.assertCompareDifferent(k4, k1, mismatched_types=True)
367
378
        k5 = self.module.StaticTuple(1)
368
 
        self.assertCompareNoRelation(k1, k5)
 
379
        self.assertCompareNoRelation(k1, k5, mismatched_types=True)
369
380
 
370
381
    def test_compare_to_tuples(self):
371
382
        k1 = self.module.StaticTuple('foo')
381
392
        self.assertCompareDifferent(('foo',), k2)
382
393
        self.assertCompareDifferent(('foo', 'aaa'), k2)
383
394
        self.assertCompareDifferent(('baz', 'bing'), k2)
384
 
        self.assertCompareDifferent(('foo', 10), k2)
 
395
        self.assertCompareDifferent(('foo', 10), k2, mismatched_types=True)
385
396
 
386
397
        k3 = self.module.StaticTuple(k1, k2)
387
398
        self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
397
408
        # This requires comparing a StaticTuple to a 'string', and then
398
409
        # interpreting that value in the next higher StaticTuple. This used to
399
410
        # generate a PyErr_BadIternalCall. We now fall back to *something*.
400
 
        self.assertCompareNoRelation(k1, k2)
 
411
        self.assertCompareNoRelation(k1, k2, mismatched_types=True)
401
412
 
402
413
    def test_hash(self):
403
414
        k = self.module.StaticTuple('foo')