diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-06-15 00:17:41 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-06-15 00:17:41 +0200 |
commit | fda40cb64041aacdb776e0b1f4f4a635bdc9d70b (patch) | |
tree | c0d30d702c6886e5f8542daffb7aef080137ed0b /tests/test_utils.py | |
parent | 1ba20b0ed7b920fa2d161df94a0dda3d93b1e14b (diff) | |
download | factory-boy-fda40cb64041aacdb776e0b1f4f4a635bdc9d70b.tar factory-boy-fda40cb64041aacdb776e0b1f4f4a635bdc9d70b.tar.gz |
Add Iterator.reset() (Closes #63).
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r-- | tests/test_utils.py | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py index b353c9d..8c73935 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -20,6 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +import itertools from factory import utils @@ -230,3 +231,108 @@ class ImportObjectTestCase(unittest.TestCase): def test_invalid_module(self): self.assertRaises(ImportError, utils.import_object, 'this-is-an-invalid-module', '__name__') + + +class ResetableIteratorTestCase(unittest.TestCase): + def test_no_reset(self): + i = utils.ResetableIterator([1, 2, 3]) + self.assertEqual([1, 2, 3], list(i)) + + def test_no_reset_new_iterator(self): + i = utils.ResetableIterator([1, 2, 3]) + iterator = iter(i) + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + + iterator2 = iter(i) + self.assertEqual(3, next(iterator2)) + + def test_infinite(self): + i = utils.ResetableIterator(itertools.cycle([1, 2, 3])) + iterator = iter(i) + values = [next(iterator) for _i in range(10)] + self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1], values) + + def test_reset_simple(self): + i = utils.ResetableIterator([1, 2, 3]) + iterator = iter(i) + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + + i.reset() + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + + def test_reset_at_begin(self): + i = utils.ResetableIterator([1, 2, 3]) + iterator = iter(i) + i.reset() + i.reset() + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + + def test_reset_at_end(self): + i = utils.ResetableIterator([1, 2, 3]) + iterator = iter(i) + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + + i.reset() + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + + def test_reset_after_end(self): + i = utils.ResetableIterator([1, 2, 3]) + iterator = iter(i) + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + self.assertRaises(StopIteration, next, iterator) + + i.reset() + # Previous iter() has stopped + iterator = iter(i) + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + + def test_reset_twice(self): + i = utils.ResetableIterator([1, 2, 3, 4, 5]) + iterator = iter(i) + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + + i.reset() + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + self.assertEqual(4, next(iterator)) + + i.reset() + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + self.assertEqual(4, next(iterator)) + + def test_reset_shorter(self): + i = utils.ResetableIterator([1, 2, 3, 4, 5]) + iterator = iter(i) + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + self.assertEqual(4, next(iterator)) + + i.reset() + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + + i.reset() + self.assertEqual(1, next(iterator)) + self.assertEqual(2, next(iterator)) + self.assertEqual(3, next(iterator)) + self.assertEqual(4, next(iterator)) + |