def test_sep_by(self): digit_list = digit.map(int).sep_by(string(',')) self.assertEqual(digit_list.parse('1,2,3,4'), [1, 2, 3, 4]) self.assertEqual(digit_list.parse('9,0,4,7'), [9, 0, 4, 7]) self.assertEqual(digit_list.parse('3,7'), [3, 7]) self.assertEqual(digit_list.parse('8'), [8]) self.assertEqual(digit_list.parse(''), []) self.assertRaises(ParseError, digit_list.parse, '8,') self.assertRaises(ParseError, digit_list.parse, ',9') self.assertRaises(ParseError, digit_list.parse, '82') self.assertRaises(ParseError, digit_list.parse, '7.6')
def test_sep_by_with_min_and_max(self): digit_list = digit.map(int).sep_by(string(','), min=2, max=4) self.assertEqual(digit_list.parse('1,2,3,4'), [1, 2, 3, 4]) self.assertEqual(digit_list.parse('9,0,4,7'), [9, 0, 4, 7]) self.assertEqual(digit_list.parse('3,7'), [3, 7]) self.assertRaises(ParseError, digit_list.parse, '8') self.assertRaises(ParseError, digit_list.parse, '') self.assertRaises(ParseError, digit_list.parse, '8,') self.assertRaises(ParseError, digit_list.parse, ',9') self.assertRaises(ParseError, digit_list.parse, '82') self.assertRaises(ParseError, digit_list.parse, '7.6') self.assertEqual(digit.sep_by(string(","), max=0).parse(''), [])
def test_map(self): parser = digit.map(int) self.assertEqual(parser.parse('7'), 7)