diff options
| author | Raymond Hettinger <python@rcn.com> | 2015-05-23 02:29:22 (GMT) |
|---|---|---|
| committer | Raymond Hettinger <python@rcn.com> | 2015-05-23 02:29:22 (GMT) |
| commit | ec219ba1c04c4514b8b004239b1a0eac914dde4a (patch) | |
| tree | 30abd24e56cc7faec9bb332728253c4a58f57e91 /Lib/test | |
| parent | 256613c605bf207ed77616c04563275cfbaf3ee4 (diff) | |
| download | cpython-ec219ba1c04c4514b8b004239b1a0eac914dde4a.zip cpython-ec219ba1c04c4514b8b004239b1a0eac914dde4a.tar.gz cpython-ec219ba1c04c4514b8b004239b1a0eac914dde4a.tar.bz2 | |
Issue #23086: Add start and stop arguments to the Sequence.index() mixin method.
Diffstat (limited to 'Lib/test')
| -rw-r--r-- | Lib/test/test_collections.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index a15651f..ec86466 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1227,6 +1227,41 @@ class TestCollectionABCs(ABCTestCase): self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__', '__getitem__') + def test_Sequence_mixins(self): + class SequenceSubclass(Sequence): + def __init__(self, seq=()): + self.seq = seq + + def __getitem__(self, index): + return self.seq[index] + + def __len__(self): + return len(self.seq) + + # Compare Sequence.index() behavior to (list|str).index() behavior + def assert_index_same(seq1, seq2, index_args): + try: + expected = seq1.index(*index_args) + except ValueError: + with self.assertRaises(ValueError): + seq2.index(*index_args) + else: + actual = seq2.index(*index_args) + self.assertEqual( + actual, expected, '%r.index%s' % (seq1, index_args)) + + for ty in list, str: + nativeseq = ty('abracadabra') + indexes = [-10000, -9999] + list(range(-3, len(nativeseq) + 3)) + seqseq = SequenceSubclass(nativeseq) + for letter in set(nativeseq) | {'z'}: + assert_index_same(nativeseq, seqseq, (letter,)) + for start in range(-3, len(nativeseq) + 3): + assert_index_same(nativeseq, seqseq, (letter, start)) + for stop in range(-3, len(nativeseq) + 3): + assert_index_same( + nativeseq, seqseq, (letter, start, stop)) + def test_ByteString(self): for sample in [bytes, bytearray]: self.assertIsInstance(sample(), ByteString) |
