def test_parsers_ignore(self): """TailedRecordFinder should skip lines to ignore.""" def never(line): return False def ignore_labels(line): return (not line) or line.isspace() or line.startswith("#") lines = ["abc", "\n", "1.", "def", "#ignore", "2."] self.assertEqual( list(TailedRecordFinder(self.endswith_period)(lines)), [["abc", "1."], ["def", "#ignore", "2."]], ) self.assertEqual( list(TailedRecordFinder(self.endswith_period, ignore=never)(lines)), [["abc", "", "1."], ["def", "#ignore", "2."]], ) self.assertEqual( list(TailedRecordFinder(self.endswith_period, ignore=ignore_labels)(lines)), [["abc", "1."], ["def", "2."]], )
def test_parsers_leftover(self): """TailedRecordFinder should raise error or yield leftover""" f = self.period_tail_finder good = ["abc \n", "def\n", ".\n", "ghi \n", "j."] blank = ["", " ", "\t \t\n\n"] bad = ["abc"] result = [["abc", "def", "."], ["ghi", "j."]] self.assertEqual(list(f(good)), result) self.assertEqual(list(f(good + blank)), result) self.assertRaises(RecordError, list, f(good + bad)) f2 = TailedRecordFinder(self.endswith_period, strict=False) self.assertEqual(list(f2(good + bad)), result + [["abc"]])
def setUp(self): """Define a standard TailedRecordFinder""" self.endswith_period = lambda x: x.endswith(".") self.period_tail_finder = TailedRecordFinder(self.endswith_period)