def test_outer_iteration(self): grouper = RangeGrouper(lambda x: x == 2, lambda x: x == 3, [1, 2, 5, 3, 5, 2, 4, 4, 3, 6]) group1 = next(grouper) group2 = next(grouper) with self.assertRaises(StopIteration): next(grouper) self.assertEqual([2, 5, 3], list(group1)) self.assertEqual([2, 4, 4, 3], list(group2))
def test_truncated_last_group(self): grouper = RangeGrouper(lambda x: x == 2, lambda x: x == 3, [1, 2, 5, 3, 3, 5, 2, 4, 4]) self.assertEqual([[2, 5, 3], [2, 4, 4]], [list(x) for x in grouper])
def test_one_item_group(self): grouper = RangeGrouper(lambda x: x == 2, lambda x: x % 2 == 0, [1, 2, 5, 3, 3, 5, 4, 3]) self.assertEqual([[2]], [list(x) for x in grouper])
def test_double_end(self): grouper = RangeGrouper(lambda x: x == 2, lambda x: x == 3, [1, 2, 5, 3, 3, 5, 2, 4, 4, 3]) self.assertEqual([[2, 5, 3], [2, 4, 4, 3]], [list(x) for x in grouper])
def test_regexp(self): grouper = RangeGrouper(re.compile('a'), re.compile('b'), 'xf ga zu jd bq zu aa qa gb'.split()) self.assertEqual([['ga', 'zu', 'jd', 'bq'], ['aa', 'qa', 'gb']], [list(x) for x in grouper])
def test_no_match(self): grouper = RangeGrouper(lambda x: x == 2, lambda x: x == 3, [1, 4, 0, 1]) self.assertEqual([], [list(x) for x in grouper])
def test_one_group(self): grouper = RangeGrouper(lambda x: x == 2, lambda x: x == 3, [1, 2, 5, 3, 5]) self.assertEqual([[2, 5, 3]], [list(x) for x in grouper])