diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2019-08-04 11:12:48 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-04 11:12:48 (GMT) |
| commit | 18b711c5a7f90d88fb74748f18fa8ef49d8486c7 (patch) | |
| tree | d04ab59ccadbade80f8999c921dd3a19b62a9cc9 /Lib | |
| parent | 17e52649c0e7e9389f1cc2444a53f059e24e6bca (diff) | |
| download | cpython-18b711c5a7f90d88fb74748f18fa8ef49d8486c7.zip cpython-18b711c5a7f90d88fb74748f18fa8ef49d8486c7.tar.gz cpython-18b711c5a7f90d88fb74748f18fa8ef49d8486c7.tar.bz2 | |
bpo-37648: Fixed minor inconsistency in some __contains__. (GH-14904)
The collection's item is now always at the left and
the needle is on the right of ==.
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/test/list_tests.py | 15 | ||||
| -rw-r--r-- | Lib/test/seq_tests.py | 29 | ||||
| -rw-r--r-- | Lib/test/support/__init__.py | 13 | ||||
| -rw-r--r-- | Lib/test/test_iter.py | 23 |
4 files changed, 69 insertions, 11 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index 40316de..44bc2ae 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -7,6 +7,7 @@ import os from functools import cmp_to_key from test import support, seq_tests +from test.support import ALWAYS_EQ, NEVER_EQ class CommonTest(seq_tests.CommonTest): @@ -329,6 +330,20 @@ class CommonTest(seq_tests.CommonTest): self.assertRaises(TypeError, a.remove) + a = self.type2test([1, 2]) + self.assertRaises(ValueError, a.remove, NEVER_EQ) + self.assertEqual(a, [1, 2]) + a.remove(ALWAYS_EQ) + self.assertEqual(a, [2]) + a = self.type2test([ALWAYS_EQ]) + a.remove(1) + self.assertEqual(a, []) + a = self.type2test([ALWAYS_EQ]) + a.remove(NEVER_EQ) + self.assertEqual(a, []) + a = self.type2test([NEVER_EQ]) + self.assertRaises(ValueError, a.remove, ALWAYS_EQ) + class BadExc(Exception): pass diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py index 65b110e..1d9ad58 100644 --- a/Lib/test/seq_tests.py +++ b/Lib/test/seq_tests.py @@ -6,6 +6,7 @@ import unittest import sys import pickle from test import support +from test.support import ALWAYS_EQ, NEVER_EQ # Various iterables # This is used for checking the constructor (here and in test_deque.py) @@ -221,15 +222,15 @@ 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 - __hash__ = None # Can't meet hash invariant requirements - self.assertNotIn(AllEq(), self.type2test([])) - self.assertIn(AllEq(), self.type2test([1])) + # Sequences must use rich comparison against each item + # (unless "is" is true, or an earlier item answered) + # So ALWAYS_EQ must be found in all non-empty sequences. + self.assertNotIn(ALWAYS_EQ, self.type2test([])) + self.assertIn(ALWAYS_EQ, self.type2test([1])) + self.assertIn(1, self.type2test([ALWAYS_EQ])) + self.assertNotIn(NEVER_EQ, self.type2test([])) + self.assertNotIn(ALWAYS_EQ, self.type2test([NEVER_EQ])) + self.assertIn(NEVER_EQ, self.type2test([ALWAYS_EQ])) def test_contains_order(self): # Sequences must test in-order. If a rich comparison has side @@ -350,6 +351,11 @@ class CommonTest(unittest.TestCase): self.assertEqual(a.count(1), 3) self.assertEqual(a.count(3), 0) + self.assertEqual(a.count(ALWAYS_EQ), 9) + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(1), 2) + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(NEVER_EQ), 2) + self.assertEqual(self.type2test([NEVER_EQ, NEVER_EQ]).count(ALWAYS_EQ), 0) + self.assertRaises(TypeError, a.count) class BadExc(Exception): @@ -378,6 +384,11 @@ class CommonTest(unittest.TestCase): self.assertEqual(u.index(0, 3, 4), 3) self.assertRaises(ValueError, u.index, 2, 0, -10) + self.assertEqual(u.index(ALWAYS_EQ), 0) + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(1), 0) + self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(NEVER_EQ), 0) + self.assertRaises(ValueError, self.type2test([NEVER_EQ, NEVER_EQ]).index, ALWAYS_EQ) + self.assertRaises(TypeError, u.index) class BadExc(Exception): diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c82037e..d34f2ef 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -113,7 +113,7 @@ __all__ = [ "run_with_locale", "swap_item", "swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict", "run_with_tz", "PGO", "missing_compiler_executable", "fd_count", - "ALWAYS_EQ", "LARGEST", "SMALLEST" + "ALWAYS_EQ", "NEVER_EQ", "LARGEST", "SMALLEST" ] class Error(Exception): @@ -3115,6 +3115,17 @@ class _ALWAYS_EQ: ALWAYS_EQ = _ALWAYS_EQ() +class _NEVER_EQ: + """ + Object that is not equal to anything. + """ + def __eq__(self, other): + return False + def __ne__(self, other): + return True + +NEVER_EQ = _NEVER_EQ() + @functools.total_ordering class _LARGEST: """ diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index 542b284..6aceda2 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -3,7 +3,7 @@ import sys import unittest from test.support import run_unittest, TESTFN, unlink, cpython_only -from test.support import check_free_after_iterating +from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ import pickle import collections.abc @@ -41,6 +41,14 @@ class IteratingSequenceClass: def __iter__(self): return BasicIterClass(self.n) +class IteratorProxyClass: + def __init__(self, i): + self.i = i + def __next__(self): + return next(self.i) + def __iter__(self): + return self + class SequenceClass: def __init__(self, n): self.n = n @@ -50,6 +58,12 @@ class SequenceClass: else: raise IndexError +class SequenceProxyClass: + def __init__(self, s): + self.s = s + def __getitem__(self, i): + return self.s[i] + class UnlimitedSequenceClass: def __getitem__(self, i): return i @@ -635,6 +649,13 @@ class TestCase(unittest.TestCase): for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5: self.assertNotIn(i, sc5) + self.assertIn(ALWAYS_EQ, IteratorProxyClass(iter([1]))) + self.assertIn(ALWAYS_EQ, SequenceProxyClass([1])) + self.assertNotIn(ALWAYS_EQ, IteratorProxyClass(iter([NEVER_EQ]))) + self.assertNotIn(ALWAYS_EQ, SequenceProxyClass([NEVER_EQ])) + self.assertIn(NEVER_EQ, IteratorProxyClass(iter([ALWAYS_EQ]))) + self.assertIn(NEVER_EQ, SequenceProxyClass([ALWAYS_EQ])) + self.assertRaises(TypeError, lambda: 3 in 12) self.assertRaises(TypeError, lambda: 3 not in map) |
