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_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_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_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_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_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_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_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_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_restores_mark_s_position(self): text = Text('abc123') fragment = text.lookup(3) mark = text.mark() text.forward(3) text.backtrack(mark) self.assertEqual(fragment, text.lookup(3))
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_starts_at_line_1_column_1(self): text = Text('abc\n123') self.assertEqual(text.position, Position(1, 1))
def test_it_increment_line_on_each_newline(self): text = Text('abc\n123\nxyz\n789\n') text.forward(16) self.assertEqual(text.position.line, 5)
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_it_increment_column_when_not_newline(self): text = Text('abc\n123') text.forward(3) self.assertEqual(text.position.column, 4)
def test_it_reset_column_on_newline(self): text = Text('abc\n123') text.forward(4) self.assertEqual(text.position.column, 1)
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_parser_not_fails(self): parser = Not(String('abc')) with self.assertRaises(ParseError): parser.parse(Text('abc123'), [])
def test_it_forward_text_from_given_length(self): text = Text('abc123') text.forward(3) self.assertEqual(text.lookup(3), '123')
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_returns_chars_when_chars_left(self): text = Text('abc123') fragment = text.lookup(3) self.assertEqual(fragment, 'abc')