def test_it_extends_matched_string_when_same_type(self): parser = String('abc') + String('123') results = [] parser.parse(Text('abc123'), results) self.assertListEqual(results, ['abc123'])
def test_it_merges_expectations_when_no_successful_parser(self): parser = Or(String('a'), String('b')) try: parser.parse(Text('123'), []) except ParseError as e: self.assertEqual(e.expected, '"a" or "b"')
def test_it_extends_parser_list_when_same_type(self): parser_a = String('abc') parser_b = String('123') parser = Or(parser_a) | Or(parser_b) self.assertEqual(parser, Or(parser_a, parser_b))
def test_it_matches_subsequent_matches_prefixed_by_separator(self): parser = SeparatedBy(String(','), String('a')) results = [] parser.parse(Text('a,a,a,'), results) self.assertEqual(results, ['a', ',', 'a', ',', 'a'])
def test_it_stops_at_first_successful_parser(self): parser = Or(String('abc'), String('123')) results = [] parser.parse(Text('abc123'), results) self.assertListEqual(results, ['abc'])
def test_it_provides_the_same_text_to_each_parser(self): text = Text('123') result = [] parser = Or(Add(String('12'), String('4')), String('123')) parser.parse(text, result) self.assertEqual(result, ['123'])
def test_it_returns_add_parser_when_not_same_type(self): parser_a = String('abc') parser_b = ParserMock() parser = parser_a + parser_b self.assertEqual(parser, Add(parser_a, parser_b))
def test_it_returns_add_parser_when_not_same_type(self): parser_a = Or() parser_b = String('abc') parser = parser_a | parser_b self.assertEqual(parser, Or(parser_a, parser_b))
def test_it_parses_as_many_available_matches(self): parser = Many(String('a')) results = [] parser.parse(Text('aaabbb'), results) self.assertEqual(results, ['a', 'a', 'a'])
def test_it_does_not_raise_parse_error_when_fails(self): parser = Maybe(String('a')) try: parser.parse(Text('bbb'), []) except ParseError: self.fail('ParseError raised')
def test_it_appends_created_node_to_results_when_successful(self): name = 'node name' parser = CreateNode(String('abc'), name) results = [] parser.parse(Text('abc123'), results) self.assertListEqual(results, [TerminalNode(name, 'abc')])
def test_it_does_not_modify_resuls_when_successful(self): parser = Skip(String('a')) results = [] try: parser.parse(Text('aaa'), results) except ParseError: self.assertListEqual(results, [])
def test_it_forwards_text_by_one_char_when_sucess(self): string = 'abc123' text = Text(string) parser = Not(String('123')) parser.parse(text, []) expected = Text(string) expected.forward(1) self.assertEqual(text.position, expected.position)
def test_it_bactracks_when_parser_not_fails(self): text = Text('abc123') position = text.position parser = Not(String('abc')) try: parser.parse(text, []) except ParseError: pass self.assertEqual(text.position, position)
def test_it_append_first_char_when_sucess(self): results = [] Not(String('123')).parse(Text('abc123'), results) self.assertListEqual(results, ['a'])
def test_it_raises_parse_error_when_no_successful_parser(self): parser = Or(String('a'), String('b')) with self.assertRaises(ParseError): parser.parse(Text('123'), [])
def test_it_raises_parse_error_when_parser_not_fails(self): parser = Not(String('abc')) with self.assertRaises(ParseError): parser.parse(Text('abc123'), [])
def test_it_raises_parse_error_when_fails(self): with self.assertRaises(ParseError): String('123').parse(Text('abc123'), [])
def test_it_appends_result_to_results_when_success(self): results = [] String('abc').parse(Text('abc123'), results) self.assertListEqual(results, ['abc'])
def test_it_raise_parse_error_when_not_a_single_match(self): parser = Many(String('a')) with self.assertRaises(ParseError): parser.parse(Text('bbb'), [])
def test_invert_returns_many_with_inverted_wrapped_parser(self): parser = String('a') self.assertEqual(~Many(parser), Many(~parser))