]> granicus.if.org Git - python/commitdiff
patch [ 1141428 ] more __contains__ tests
authorGeorg Brandl <georg@python.org>
Wed, 24 Aug 2005 09:08:57 +0000 (09:08 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 24 Aug 2005 09:08:57 +0000 (09:08 +0000)
Lib/test/seq_tests.py
Lib/test/test_richcmp.py

index 89996a36e4cc6f1c8fa06db1fbfcb069918a0989..ae5bba3d6eb62a4136e19a96d25c02702f124e3d 100644 (file)
@@ -207,6 +207,33 @@ class CommonTest(unittest.TestCase):
 
         self.assertRaises(TypeError, u.__contains__)
 
+    def test_contains_fake(self):
+        class AllEq:
+            # Sequences must use rich comparison against each item
+            # (unless "is" is true, or an earlier item answered)
+            # So instances of AllEq must be found in all non-empty sequences.
+            def __eq__(self, other):
+                return True
+            def __hash__(self):
+                raise NotImplemented
+        self.assert_(AllEq() not in self.type2test([]))
+        self.assert_(AllEq() in self.type2test([1]))
+
+    def test_contains_order(self):
+        # Sequences must test in-order.  If a rich comparison has side
+        # effects, these will be visible to tests against later members.
+        # In this test, the "side effect" is a short-circuiting raise.
+        class DoNotTestEq(Exception):
+            pass
+        class StopCompares:
+            def __eq__(self, other):
+                raise DoNotTestEq
+        
+        checkfirst = self.type2test([1, StopCompares()])
+        self.assert_(1 in checkfirst)
+        checklast = self.type2test([StopCompares(), 1])
+        self.assertRaises(DoNotTestEq, checklast.__contains__, 1)
+
     def test_len(self):
         self.assertEqual(len(self.type2test()), 0)
         self.assertEqual(len(self.type2test([])), 0)
index 006b1528c947d6d7b376a9061935564dbade774c..687298d26e50cfa78ef1d096817423a489c1c539 100644 (file)
@@ -259,8 +259,8 @@ class DictTest(unittest.TestCase):
 
     def test_dicts(self):
         # Verify that __eq__ and __ne__ work for dicts even if the keys and
-        # values don't support anything other than __eq__ and __ne__.  Complex
-        # numbers are a fine example of that.
+        # values don't support anything other than __eq__ and __ne__ (and
+        # __hash__).  Complex numbers are a fine example of that.
         import random
         imag1a = {}
         for i in range(50):