aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_contains.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_contains.py')
-rw-r--r--Lib/test/test_contains.py68
1 files changed, 25 insertions, 43 deletions
diff --git a/Lib/test/test_contains.py b/Lib/test/test_contains.py
index 7138081dc82..a667a16d8c7 100644
--- a/Lib/test/test_contains.py
+++ b/Lib/test/test_contains.py
@@ -1,4 +1,5 @@
-from test.test_support import have_unicode, run_unittest
+from collections import deque
+from test.support import run_unittest
import unittest
@@ -6,7 +7,7 @@ class base_set:
def __init__(self, el):
self.el = el
-class set(base_set):
+class myset(base_set):
def __contains__(self, el):
return self.el == el
@@ -14,11 +15,10 @@ class seq(base_set):
def __getitem__(self, n):
return [self.el][n]
-
class TestContains(unittest.TestCase):
def test_common_tests(self):
a = base_set(1)
- b = set(1)
+ b = myset(1)
c = seq(1)
self.assertIn(1, b)
self.assertNotIn(0, b)
@@ -36,28 +36,6 @@ class TestContains(unittest.TestCase):
self.assertRaises(TypeError, lambda: None in 'abc')
- if have_unicode:
- def test_char_in_unicode(self):
- self.assertIn('c', unicode('abc'))
- self.assertNotIn('d', unicode('abc'))
-
- self.assertIn('', unicode(''))
- self.assertIn(unicode(''), '')
- self.assertIn(unicode(''), unicode(''))
- self.assertIn('', unicode('abc'))
- self.assertIn(unicode(''), 'abc')
- self.assertIn(unicode(''), unicode('abc'))
-
- self.assertRaises(TypeError, lambda: None in unicode('abc'))
-
- # test Unicode char in Unicode
- self.assertIn(unicode('c'), unicode('abc'))
- self.assertNotIn(unicode('d'), unicode('abc'))
-
- # test Unicode char in string
- self.assertIn(unicode('c'), 'abc')
- self.assertNotIn(unicode('d'), 'abc')
-
def test_builtin_sequence_types(self):
# a collection of tests on builtin sequence types
a = range(10)
@@ -78,30 +56,34 @@ class TestContains(unittest.TestCase):
This class is designed to make sure that the contains code
works when the list is modified during the check.
"""
- aList = range(15)
- def __cmp__(self, other):
+ aList = list(range(15))
+ def __eq__(self, other):
if other == 12:
self.aList.remove(12)
self.aList.remove(13)
self.aList.remove(14)
- return 1
+ return 0
self.assertNotIn(Deviant1(), Deviant1.aList)
- class Deviant2:
- """Behaves strangely when compared
-
- This class raises an exception during comparison. That in
- turn causes the comparison to fail with a TypeError.
- """
- def __cmp__(self, other):
- if other == 4:
- raise RuntimeError, "gotcha"
-
- try:
- self.assertNotIn(Deviant2(), a)
- except TypeError:
- pass
+ def test_nonreflexive(self):
+ # containment and equality tests involving elements that are
+ # not necessarily equal to themselves
+
+ class MyNonReflexive(object):
+ def __eq__(self, other):
+ return False
+ def __hash__(self):
+ return 28
+
+ values = float('nan'), 1, None, 'abc', MyNonReflexive()
+ constructors = list, tuple, dict.fromkeys, set, frozenset, deque
+ for constructor in constructors:
+ container = constructor(values)
+ for elem in container:
+ self.assertIn(elem, container)
+ self.assertTrue(container == constructor(values))
+ self.assertTrue(container == container)
def test_main():