[Zope-Checkins] CVS: Releases/Zope/lib/python/ZTUtils/tests - testIterator.py:1.5
Evan Simpson
evan@zope.com
Wed, 9 Oct 2002 13:33:50 -0400
Update of /cvs-repository/Releases/Zope/lib/python/ZTUtils/tests
In directory cvs.zope.org:/tmp/cvs-serv12964/lib/python/ZTUtils/tests
Modified Files:
testIterator.py
Log Message:
Close Collector #385, #577 and simplify ZTUtils.Iterator implementation.
=== Releases/Zope/lib/python/ZTUtils/tests/testIterator.py 1.4 => 1.5 ===
--- Releases/Zope/lib/python/ZTUtils/tests/testIterator.py:1.4 Wed Aug 14 18:10:12 2002
+++ Releases/Zope/lib/python/ZTUtils/tests/testIterator.py Wed Oct 9 13:33:49 2002
@@ -1,3 +1,4 @@
+from __future__ import generators
import os, sys, unittest
from ZTUtils import Iterator
@@ -8,6 +9,26 @@
except NameError:
do_piter_test = 0
+class itemIterator:
+ 'Ignore the __getitem__ argument in order to catch non-monotonic access.'
+ def __init__(self, n):
+ self.n = n
+ self.i = 0
+ def __getitem__(self, i):
+ if self.i >= self.n:
+ raise IndexError
+ i = self.i
+ self.i = self.i + 1
+ return i
+
+class genIterator:
+ 'Generator-based iteration'
+ def __init__(self, n):
+ self.n = n
+ def __iter__(self):
+ for i in range(self.n):
+ yield i
+
class IteratorTests(unittest.TestCase):
def testIterator0(self):
@@ -48,13 +69,20 @@
assert not it.end, (
"End false on element %s of %s" % (el, size))
- def testIndex(self):
- it = Iterator(range(5))
- for el in range(5):
+ def assertRangeMatch(self, ob, n):
+ it = Iterator(ob)
+ for el in range(n):
assert it.next(), "Iterator stopped too soon"
assert it.index == el, "Incorrect index"
assert it.number() == el + 1, "Incorrect number"
assert it.item == el, "Incorrect item"
+
+ def testIndex(self):
+ self.assertRangeMatch(range(5), 5)
+ self.assertRangeMatch((0,1,2,3,4), 5)
+ self.assertRangeMatch({0:0, 1:1, 2:2, 3:3, 4:4}, 5)
+ self.assertRangeMatch(itemIterator(5), 5)
+ self.assertRangeMatch(genIterator(5), 5)
def testFirstLast(self):
it = Iterator([1])