3 from test
import test_support
5 seq
, res
= 'abc', [(0,'a'), (1,'b'), (2,'c')]
8 'Sequence using __getitem__'
9 def __init__(self
, seqn
):
11 def __getitem__(self
, i
):
15 'Sequence using iterator protocol'
16 def __init__(self
, seqn
):
22 if self
.i
>= len(self
.seqn
): raise StopIteration
28 'Sequence using iterator protocol defined with a generator'
29 def __init__(self
, seqn
):
37 'Missing __getitem__ and __iter__'
38 def __init__(self
, seqn
):
42 if self
.i
>= len(self
.seqn
): raise StopIteration
48 'Test propagation of exceptions'
49 def __init__(self
, seqn
):
58 'Iterator missing next()'
59 def __init__(self
, seqn
):
65 class EnumerateTestCase(unittest
.TestCase
):
69 def test_basicfunction(self
):
70 self
.assertEqual(type(self
.enum(seq
)), self
.enum
)
72 self
.assertEqual(iter(e
), e
)
73 self
.assertEqual(list(self
.enum(seq
)), res
)
76 def test_getitemseqn(self
):
77 self
.assertEqual(list(self
.enum(G(seq
))), res
)
79 self
.assertRaises(StopIteration, e
.next
)
81 def test_iteratorseqn(self
):
82 self
.assertEqual(list(self
.enum(I(seq
))), res
)
84 self
.assertRaises(StopIteration, e
.next
)
86 def test_iteratorgenerator(self
):
87 self
.assertEqual(list(self
.enum(Ig(seq
))), res
)
89 self
.assertRaises(StopIteration, e
.next
)
91 def test_noniterable(self
):
92 self
.assertRaises(TypeError, self
.enum
, X(seq
))
94 def test_illformediterable(self
):
95 self
.assertRaises(TypeError, list, self
.enum(N(seq
)))
97 def test_exception_propagation(self
):
98 self
.assertRaises(ZeroDivisionError, list, self
.enum(E(seq
)))
100 class MyEnum(enumerate):
103 class SubclassTestCase(EnumerateTestCase
):
108 suite
= unittest
.TestSuite()
109 suite
.addTest(unittest
.makeSuite(EnumerateTestCase
))
110 suite
.addTest(unittest
.makeSuite(SubclassTestCase
))
114 test_support
.run_suite(suite())
116 if __name__
== "__main__":