import unittest
from bpython import repl


class TestHistory(unittest.TestCase):
    def setUp(self):
        self.history = repl.History('#%d' % x for x in range(1000))

    def test_is_at_start(self):
        self.history.first()

        self.assertNotEqual(self.history.index, 0)
        self.assertTrue(self.history.is_at_end)
        self.history.forward()
        self.assertFalse(self.history.is_at_end)

    def test_is_at_end(self):
        self.history.last()

        self.assertEqual(self.history.index, 0)
        self.assertTrue(self.history.is_at_start)
        self.assertFalse(self.history.is_at_end)

    def test_first(self):
        self.history.first()

        self.assertFalse(self.history.is_at_start)
        self.assertTrue(self.history.is_at_end)

    def test_last(self):
        self.history.last()

        self.assertTrue(self.history.is_at_start)
        self.assertFalse(self.history.is_at_end)

    def test_back(self):
        self.assertEqual(self.history.back(), '#999')
        self.assertNotEqual(self.history.back(), '#999')
        self.assertEqual(self.history.back(), '#997')
        for x in range(997):
            self.history.back()
        self.assertEqual(self.history.back(), '#0')

    def test_forward(self):
        self.history.first()

        self.assertEqual(self.history.forward(), '#1')
        self.assertNotEqual(self.history.forward(), '#1')
        self.assertEqual(self.history.forward(), '#3')
        #  1000 == entries   4 == len(range(1, 3) ===> '#1000' (so +1)
        for x in range(1000 - 4 - 1):
            self.history.forward()
        self.assertEqual(self.history.forward(), '#999')

    def test_MatchesIterator(self):
        matches = ['bobby', 'bobbies', 'bobberina']

        matches_iterator = repl.MatchesIterator(
                current_word='bob',
                matches=matches)

        # should be falsey before we enter (i.e. 'not active')
        self.assertEqual(bool(matches_iterator), False)

        matched = []
        for i, x in enumerate(matches_iterator):
            matched.append(x)
            if i == 8:
                break

        self.assertEqual(matched, matches * 3)

        # should be truthy once we have an active match
        self.assertEqual(bool(matches_iterator), True)

        self.assertEqual(matches_iterator.current(), (matches * 3)[-1])

if __name__ == '__main__':
    unittest.main()

@@ -9,16 +9,34 @@
 nittest%0A
+import itertools%0A%0A
 from bpy
@@ -2013,145 +2013,56 @@
     
-matched = %5B%5D%0A        for i, x in enumerate(matches_iterator):%0A            matched.append(x)%0A            if i == 8:%0A                break%0A
+slice = itertools.islice(matches_iterator, 0, 9)
 %0A   
@@ -2087,17 +2087,20 @@
 ual(
-matched, 
+list(slice),
 matc

make MatchesIterator test more succinct (thanks Andreas)