示例#1
0
    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')
示例#2
0
    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(''), [])
示例#3
0
 def test_map(self):
     parser = digit.map(int)
     self.assertEqual(parser.parse('7'),
                      7)