From 8fd3f871f3ab172d4b6e49bb303e3554de268ae2 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 2 May 2003 22:38:07 +0000 Subject: [PATCH] Add StopIteration tests. Simplify test_main(). --- Lib/test/test_itertools.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index ef2ab26ae58..e03ce8c18d7 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -106,6 +106,28 @@ class TestBasicOps(unittest.TestCase): underten = lambda x: x<10 self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) + def test_StopIteration(self): + class StopNow: + """Test support class . Emulates an empty iterable.""" + def __iter__(self): + return self + def next(self): + raise StopIteration + + for f in (chain, cycle, izip): + self.assertRaises(StopIteration, f([]).next) + self.assertRaises(StopIteration, f(StopNow()).next) + + self.assertRaises(StopIteration, islice([], None).next) + self.assertRaises(StopIteration, islice(StopNow(), None).next) + + self.assertRaises(StopIteration, repeat(None, 0).next) + + for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap): + self.assertRaises(StopIteration, f(lambda x:x, []).next) + self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next) + + libreftest = """ Doctest for examples in the library reference, libitertools.tex @@ -226,17 +248,15 @@ False __test__ = {'libreftest' : libreftest} def test_main(verbose=None): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TestBasicOps)) - test_support.run_suite(suite) + test_support.run_unittest(TestBasicOps) # verify reference counting import sys if verbose and hasattr(sys, "gettotalrefcount"): - counts = [] - for i in xrange(5): - test_support.run_suite(suite) - counts.append(sys.gettotalrefcount()-i) + counts = [None] * 5 + for i in xrange(len(counts)): + test_support.run_unittest(TestBasicOps) + counts[i] = sys.gettotalrefcount() print counts # doctest the examples in the library reference