def test_peek_with_one(self): a = [] it = peek_iter(a) self.assertFalseTwice(it.has_next) self.assertEqualTwice([it.sentinel], it.peek, 1) self.assertFalseTwice(it.has_next) a = ['1'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1'], it.peek, 1) self.assertEqual('1', next(it)) self.assertFalseTwice(it.has_next) self.assertEqualTwice([it.sentinel], it.peek, 1) self.assertFalseTwice(it.has_next) a = ['1', '2'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1'], it.peek, 1) self.assertEqual('1', next(it)) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['2'], it.peek, 1) self.assertEqual('2', next(it)) self.assertFalseTwice(it.has_next) self.assertEqualTwice([it.sentinel], it.peek, 1) self.assertFalseTwice(it.has_next)
def test_next_with_multi(self): a = [] it = peek_iter(a) self.assertFalseTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 2) a = ['1'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 2) self.assertTrueTwice(it.has_next) a = ['1', '2'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqual(['1', '2'], it.next(2)) self.assertFalseTwice(it.has_next) a = ['1', '2', '3'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqual(['1', '2'], it.next(2)) self.assertTrueTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 2) self.assertTrueTwice(it.has_next) a = ['1', '2', '3', '4'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqual(['1', '2'], it.next(2)) self.assertTrueTwice(it.has_next) self.assertEqual(['3', '4'], it.next(2)) self.assertFalseTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 2) self.assertFalseTwice(it.has_next)
def test_next_with_none(self): a = [] it = peek_iter(a) self.assertFalseTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next) self.assertFalseTwice(it.has_next) a = ['1'] it = peek_iter(a) self.assertEqual('1', it.__next__()) a = ['1'] it = peek_iter(a) self.assertNext(it, '1', is_last=True) a = ['1', '2'] it = peek_iter(a) self.assertNext(it, '1', is_last=False) self.assertNext(it, '2', is_last=True) a = ['1', '2', '3'] it = peek_iter(a) self.assertNext(it, '1', is_last=False) self.assertNext(it, '2', is_last=False) self.assertNext(it, '3', is_last=True)
def test_peek_with_zero(self): a = [] it = peek_iter(a) self.assertFalseTwice(it.has_next) self.assertEqualTwice([], it.peek, 0) a = ['1'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.peek, 0) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.peek, 0) a = ['1', '2'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.peek, 0) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.peek, 0)
def test_next_with_zero(self): a = [] it = peek_iter(a) self.assertFalseTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 0) a = ['1'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.next, 0) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.next, 0) a = ['1', '2'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.next, 0) self.assertTrueTwice(it.has_next) self.assertEqualTwice([], it.next, 0)
def test_peek_with_multi(self): a = [] it = peek_iter(a) self.assertFalseTwice(it.has_next) self.assertEqualTwice([it.sentinel, it.sentinel], it.peek, 2) self.assertFalseTwice(it.has_next) a = ['1'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', it.sentinel], it.peek, 2) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', it.sentinel, it.sentinel], it.peek, 3) self.assertTrueTwice(it.has_next) a = ['1', '2'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', '2'], it.peek, 2) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', '2', it.sentinel], it.peek, 3) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', '2', it.sentinel, it.sentinel], it.peek, 4) self.assertTrueTwice(it.has_next) a = ['1', '2', '3'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', '2'], it.peek, 2) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', '2', '3'], it.peek, 3) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['1', '2', '3', it.sentinel], it.peek, 4) self.assertTrueTwice(it.has_next) self.assertEqual('1', next(it)) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['2', '3'], it.peek, 2) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['2', '3', it.sentinel], it.peek, 3) self.assertTrueTwice(it.has_next) self.assertEqualTwice(['2', '3', it.sentinel, it.sentinel], it.peek, 4) self.assertTrueTwice(it.has_next)
def test_iter(self): a = ['1', '2', '3'] it = peek_iter(a) self.assertTrue(it is it.__iter__()) a = [] b = [i for i in peek_iter(a)] self.assertEqual([], b) a = ['1'] b = [i for i in peek_iter(a)] self.assertEqual(['1'], b) a = ['1', '2'] b = [i for i in peek_iter(a)] self.assertEqual(['1', '2'], b) a = ['1', '2', '3'] b = [i for i in peek_iter(a)] self.assertEqual(['1', '2', '3'], b)
def test_init_with_sentinel(self): a = iter(['1', '2', 'DONE']) sentinel = 'DONE' self.assertRaises(TypeError, peek_iter, a, sentinel) def get_next(): return next(a) it = peek_iter(get_next, sentinel) self.assertEqual(it.sentinel, sentinel) self.assertNext(it, '1', is_last=False) self.assertNext(it, '2', is_last=True)
def test_next_with_one(self): a = [] it = peek_iter(a) self.assertFalseTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 1) a = ['1'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqual(['1'], it.next(1)) self.assertFalseTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 1) a = ['1', '2'] it = peek_iter(a) self.assertTrueTwice(it.has_next) self.assertEqual(['1'], it.next(1)) self.assertTrueTwice(it.has_next) self.assertEqual(['2'], it.next(1)) self.assertFalseTwice(it.has_next) self.assertRaisesTwice(StopIteration, it.next, 1)