File size: 2,197 Bytes
3943768 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import unittest
from iterators import IteratorPipe
class TestQueueToIterator(unittest.TestCase):
def test_normal_iteration(self):
it = IteratorPipe()
it.put(1)
it.put(2)
it.put(3)
it.close() # stop iteration
self.assertEqual(next(it), 1)
self.assertEqual(next(it), 2)
self.assertEqual(next(it), 3)
self.assertRaises(StopIteration, next, it)
def test_normal_custom_sentinel(self):
sentinel = object()
it = IteratorPipe(sentinel=sentinel)
it.put(1)
it.put(2)
it.put(3)
it.put(sentinel) # stop iteration
self.assertEqual(next(it), 1)
self.assertEqual(next(it), 2)
self.assertEqual(next(it), 3)
self.assertRaises(StopIteration, next, it)
def test_multiple_close(self):
sentinel = object()
it = IteratorPipe(sentinel=sentinel)
it.put(1)
it.put(2)
it.put(3)
it.close() # stop iteration
it.close() # stop iteration
it.close() # stop iteration
self.assertEqual(next(it), 1)
self.assertEqual(next(it), 2)
self.assertEqual(next(it), 3)
self.assertRaises(StopIteration, next, it)
def test_multiple_next_after_close(self):
sentinel = object()
it = IteratorPipe(sentinel=sentinel)
it.put(1)
it.put(2)
it.put(3)
it.close() # stop iteration
self.assertEqual(next(it), 1)
self.assertEqual(next(it), 2)
self.assertEqual(next(it), 3)
self.assertRaises(StopIteration, next, it)
self.assertRaises(StopIteration, next, it)
self.assertRaises(StopIteration, next, it)
def test_put_after_close(self):
sentinel = object()
it = IteratorPipe(sentinel=sentinel)
self.assertTrue(it.put(1))
it.close() # stop iteration
self.assertFalse(it.put(2))
it.close() # stop iteration
self.assertFalse(it.put(3))
it.close() # stop iteration
self.assertEqual(next(it), 1)
self.assertRaises(StopIteration, next, it)
self.assertRaises(StopIteration, next, it)
|