def test_constructor_is_none(self): lines = ['abc', ' def ', ' ghi', 'jkl '] # should strip exp = [['abc', 'def'], ['ghi', 'jkl']] obs = list(LineGrouper(2)(lines)) self.assertEqual(obs, exp) # should not strip exp = [['abc', ' def '], [' ghi', 'jkl ']] obs = list(LineGrouper(2, constructor=None)(lines)) self.assertEqual(obs, exp)
def test_parser_ignore(self): 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(LineGrouper(1)(lines)), [['abc'], ['1'], ['def'], ['#ignore'], ['2']]) self.assertEqual(list(LineGrouper(1, ignore=never)(lines)), [[i.strip()] for i in lines]) self.assertEqual(list(LineGrouper(2, ignore=ignore_labels)(lines)), [['abc', '1'], ['def', '2']])
def test_parser(self): good = [' \t >abc \n', '\t def\n', '\t\n', '\t >efg \n', 'ghi', ] c = LineGrouper(2) self.assertEqual(list(c(good)), [['>abc', 'def'], ['>efg', 'ghi']]) c = LineGrouper(1) self.assertEqual(list(c(good)), [['>abc'], ['def'], ['>efg'], ['ghi']]) c = LineGrouper(4) self.assertEqual(list(c(good)), [['>abc', 'def', '>efg', 'ghi']]) # shouldn't work if not evenly divisible c = LineGrouper(3) self.assertRaises(RecordError, list, c(good))