~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Lib/test/test_richcmp.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
    def __setitem__(self, i, v):
49
49
        self.data[i] = v
50
50
 
51
 
    def __hash__(self):
52
 
        raise TypeError, "Vectors cannot be hashed"
 
51
    __hash__ = None # Vectors cannot be hashed
53
52
 
54
53
    def __nonzero__(self):
55
54
        raise TypeError, "Vectors cannot be used in Boolean contexts"
85
84
            raise ValueError, "Cannot compare vectors of different length"
86
85
        return other
87
86
 
88
 
 
89
 
class SimpleOrder(object):
90
 
    """
91
 
    A simple class that defines order but not full comparison.
92
 
    """
93
 
 
94
 
    def __init__(self, value):
95
 
        self.value = value
96
 
 
97
 
    def __lt__(self, other):
98
 
        if not isinstance(other, SimpleOrder):
99
 
            return True
100
 
        return self.value < other.value
101
 
 
102
 
    def __gt__(self, other):
103
 
        if not isinstance(other, SimpleOrder):
104
 
            return False
105
 
        return self.value > other.value
106
 
 
107
 
 
108
 
class DumbEqualityWithoutHash(object):
109
 
    """
110
 
    A class that define __eq__, but no __hash__: it shouldn't be hashable.
111
 
    """
112
 
 
113
 
    def __eq__(self, other):
114
 
        return False
115
 
 
116
 
 
117
87
opmap = {
118
88
    "lt": (lambda a,b: a< b, operator.lt, operator.__lt__),
119
89
    "le": (lambda a,b: a<=b, operator.le, operator.__le__),
359
329
        for op in opmap["lt"]:
360
330
            self.assertIs(op(x, y), True)
361
331
 
362
 
 
363
 
class HashableTest(unittest.TestCase):
364
 
    """
365
 
    Test hashability of classes with rich operators defined.
366
 
    """
367
 
 
368
 
    def test_simpleOrderHashable(self):
369
 
        """
370
 
        A class that only defines __gt__ and/or __lt__ should be hashable.
371
 
        """
372
 
        a = SimpleOrder(1)
373
 
        b = SimpleOrder(2)
374
 
        self.assert_(a < b)
375
 
        self.assert_(b > a)
376
 
        self.assert_(a.__hash__ is not None)
377
 
 
378
 
    def test_notHashableException(self):
379
 
        """
380
 
        If a class is not hashable, it should raise a TypeError with an
381
 
        understandable message.
382
 
        """
383
 
        a = DumbEqualityWithoutHash()
384
 
        try:
385
 
            hash(a)
386
 
        except TypeError, e:
387
 
            self.assertEquals(str(e),
388
 
                              "unhashable type: 'DumbEqualityWithoutHash'")
389
 
        else:
390
 
            raise test_support.TestFailed("Should not be here")
391
 
 
392
 
 
393
332
def test_main():
394
 
    test_support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest, HashableTest)
 
333
    test_support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest)
395
334
 
396
335
if __name__ == "__main__":
397
336
    test_main()