|
1 | 1 | import unittest |
2 | | -import itertools |
| 2 | +from itertools import islice |
3 | 3 |
|
4 | 4 | from bpython import repl |
5 | 5 |
|
@@ -76,23 +76,58 @@ def test_reset(self): |
76 | 76 |
|
77 | 77 | class TestMatchesIterator(unittest.TestCase): |
78 | 78 |
|
79 | | - def test_all(self): |
80 | | - matches = ['bobby', 'bobbies', 'bobberina'] |
| 79 | + def setUp(self): |
| 80 | + self.matches = ['bobby', 'bobbies', 'bobberina'] |
| 81 | + self.matches_iterator = repl.MatchesIterator(current_word='bob', |
| 82 | + matches=self.matches) |
| 83 | + |
| 84 | + def test_next(self): |
| 85 | + self.assertEqual(self.matches_iterator.next(), self.matches[0]) |
| 86 | + |
| 87 | + for x in range(len(self.matches) - 1): |
| 88 | + self.matches_iterator.next() |
| 89 | + |
| 90 | + self.assertEqual(self.matches_iterator.next(), self.matches[0]) |
| 91 | + self.assertEqual(self.matches_iterator.next(), self. matches[1]) |
| 92 | + self.assertNotEqual(self.matches_iterator.next(), self.matches[1]) |
| 93 | + |
| 94 | + def test_previous(self): |
| 95 | + self.assertEqual(self.matches_iterator.previous(), self.matches[2]) |
| 96 | + |
| 97 | + for x in range(len(self.matches) - 1): |
| 98 | + self.matches_iterator.previous() |
| 99 | + |
| 100 | + self.assertNotEqual(self.matches_iterator.previous(), self.matches[0]) |
| 101 | + self.assertEqual(self.matches_iterator.previous(), self.matches[1]) |
| 102 | + self.assertEqual(self.matches_iterator.previous(), self.matches[0]) |
| 103 | + |
| 104 | + def test_nonzero(self): |
| 105 | + """self.matches_iterator should be False at start, |
| 106 | + then True once we active a match. |
| 107 | + """ |
| 108 | + self.assertFalse(self.matches_iterator) |
| 109 | + self.matches_iterator.next() |
| 110 | + self.assertTrue(self.matches_iterator) |
81 | 111 |
|
82 | | - matches_iterator = repl.MatchesIterator( |
83 | | - current_word='bob', |
84 | | - matches=matches) |
| 112 | + def test_iter(self): |
| 113 | + slice = islice(self.matches_iterator, 0, 9) |
| 114 | + self.assertEqual(list(slice), self.matches * 3) |
85 | 115 |
|
86 | | - # should be falsey before we enter (i.e. 'not active') |
87 | | - self.assertFalse(matches_iterator) |
| 116 | + def test_current(self): |
| 117 | + self.assertRaises(ValueError, self.matches_iterator.current) |
| 118 | + self.matches_iterator.next() |
| 119 | + self.assertEqual(self.matches_iterator.current(), self.matches[0]) |
88 | 120 |
|
89 | | - slice = itertools.islice(matches_iterator, 0, 9) |
90 | | - self.assertEqual(list(slice), matches * 3) |
| 121 | + def test_update(self): |
| 122 | + slice = islice(self.matches_iterator, 0, 3) |
| 123 | + self.assertEqual(list(slice), self.matches) |
91 | 124 |
|
92 | | - # should be truthy once we have an active match |
93 | | - self.assertTrue(matches_iterator) |
| 125 | + newmatches = ['string', 'str', 'set'] |
| 126 | + self.matches_iterator.update('s', newmatches) |
94 | 127 |
|
95 | | - self.assertEqual(matches_iterator.current(), (matches * 3)[-1]) |
| 128 | + newslice = islice(newmatches, 0, 3) |
| 129 | + self.assertNotEqual(list(slice), self.matches) |
| 130 | + self.assertEqual(list(newslice), newmatches) |
96 | 131 |
|
97 | 132 |
|
98 | 133 | if __name__ == '__main__': |
|
0 commit comments