示例#1
0
    def test_peek_with_one(self):
        a = []
        it = iterpeek(a)
        self.assertFalseTwice(it.has_next)
        self.assertEqualTwice([it.sentinel], it.peek, 1)
        self.assertFalseTwice(it.has_next)

        a = ['1']
        it = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        self.assertEqualTwice(['1'], it.peek, 1)
        assert '1' == next(it)
        self.assertFalseTwice(it.has_next)
        self.assertEqualTwice([it.sentinel], it.peek, 1)
        self.assertFalseTwice(it.has_next)

        a = ['1', '2']
        it = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        self.assertEqualTwice(['1'], it.peek, 1)
        assert '1' == next(it)
        self.assertTrueTwice(it.has_next)
        self.assertEqualTwice(['2'], it.peek, 1)
        assert '2' == next(it)
        self.assertFalseTwice(it.has_next)
        self.assertEqualTwice([it.sentinel], it.peek, 1)
        self.assertFalseTwice(it.has_next)
示例#2
0
    def test_next_with_multi(self):
        a = []
        it = iterpeek(a)
        self.assertFalseTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next, 2)

        a = ['1']
        it = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next, 2)
        self.assertTrueTwice(it.has_next)

        a = ['1', '2']
        it = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        assert ['1', '2'] == it.next(2)
        self.assertFalseTwice(it.has_next)

        a = ['1', '2', '3']
        it = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        assert ['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 = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        assert ['1', '2'] == it.next(2)
        self.assertTrueTwice(it.has_next)
        assert ['3', '4'] == it.next(2)
        self.assertFalseTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next, 2)
        self.assertFalseTwice(it.has_next)
示例#3
0
    def test_next_with_none(self):
        a = []
        it = iterpeek(a)
        self.assertFalseTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next)
        self.assertFalseTwice(it.has_next)

        a = ['1']
        it = iterpeek(a)
        assert '1' == it.__next__()

        a = ['1']
        it = iterpeek(a)
        self.assertNext(it, '1', is_last=True)

        a = ['1', '2']
        it = iterpeek(a)
        self.assertNext(it, '1', is_last=False)
        self.assertNext(it, '2', is_last=True)

        a = ['1', '2', '3']
        it = iterpeek(a)
        self.assertNext(it, '1', is_last=False)
        self.assertNext(it, '2', is_last=False)
        self.assertNext(it, '3', is_last=True)
示例#4
0
    def test_peek_with_zero(self):
        a = []
        it = iterpeek(a)
        self.assertFalseTwice(it.has_next)
        self.assertEqualTwice([], it.peek, 0)

        a = ['1']
        it = iterpeek(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 = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        self.assertEqualTwice([], it.peek, 0)
        self.assertTrueTwice(it.has_next)
        self.assertEqualTwice([], it.peek, 0)
示例#5
0
    def test_next_with_zero(self):
        a = []
        it = iterpeek(a)
        self.assertFalseTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next, 0)

        a = ['1']
        it = iterpeek(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 = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        self.assertEqualTwice([], it.next, 0)
        self.assertTrueTwice(it.has_next)
        self.assertEqualTwice([], it.next, 0)
示例#6
0
    def test_peek_with_multi(self):
        a = []
        it = iterpeek(a)
        self.assertFalseTwice(it.has_next)
        self.assertEqualTwice([it.sentinel, it.sentinel], it.peek, 2)
        self.assertFalseTwice(it.has_next)

        a = ['1']
        it = iterpeek(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 = iterpeek(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 = iterpeek(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)
        assert '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)
示例#7
0
    def test_iter(self):
        a = ['1', '2', '3']
        it = iterpeek(a)
        assert it is it.__iter__()

        a = []
        b = [i for i in iterpeek(a)]
        assert [] == b

        a = ['1']
        b = [i for i in iterpeek(a)]
        assert ['1'] == b

        a = ['1', '2']
        b = [i for i in iterpeek(a)]
        assert ['1', '2'] == b

        a = ['1', '2', '3']
        b = [i for i in iterpeek(a)]
        assert ['1', '2', '3'] == b
示例#8
0
    def test_init_with_sentinel(self):
        a = iter(['1', '2', 'DONE'])
        sentinel = 'DONE'
        pytest.raises(TypeError, iterpeek, a, sentinel)

        def get_next():
            return next(a)

        it = iterpeek(get_next, sentinel)
        assert it.sentinel == sentinel
        self.assertNext(it, '1', is_last=False)
        self.assertNext(it, '2', is_last=True)
示例#9
0
    def test_next_with_one(self):
        a = []
        it = iterpeek(a)
        self.assertFalseTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next, 1)

        a = ['1']
        it = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        assert ['1'] == it.next(1)
        self.assertFalseTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next, 1)

        a = ['1', '2']
        it = iterpeek(a)
        self.assertTrueTwice(it.has_next)
        assert ['1'] == it.next(1)
        self.assertTrueTwice(it.has_next)
        assert ['2'] == it.next(1)
        self.assertFalseTwice(it.has_next)
        self.assertRaisesTwice(StopIteration, it.next, 1)