Beispiel #1
0
    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)
Beispiel #2
0
    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'])
Beispiel #3
0
    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'])
Beispiel #5
0
    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'])
Beispiel #6
0
    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'])
Beispiel #7
0
    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')])
Beispiel #9
0
    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'])
Beispiel #10
0
    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, [])
Beispiel #11
0
    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))
Beispiel #12
0
    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)
Beispiel #13
0
    def test_it_starts_at_line_1_column_1(self):
        text = Text('abc\n123')

        self.assertEqual(text.position, Position(1, 1))
Beispiel #14
0
    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)
Beispiel #15
0
    def test_it_raises_parse_error_when_fails(self):

        with self.assertRaises(ParseError):
            String('123').parse(Text('abc123'), [])
Beispiel #16
0
    def test_it_appends_result_to_results_when_success(self):
        results = []

        String('abc').parse(Text('abc123'), results)

        self.assertListEqual(results, ['abc'])
Beispiel #17
0
    def test_it_raise_parse_error_when_not_a_single_match(self):
        parser = Many(String('a'))

        with self.assertRaises(ParseError):
            parser.parse(Text('bbb'), [])
Beispiel #18
0
    def test_it_increment_column_when_not_newline(self):
        text = Text('abc\n123')
        text.forward(3)

        self.assertEqual(text.position.column, 4)
Beispiel #19
0
    def test_it_reset_column_on_newline(self):
        text = Text('abc\n123')
        text.forward(4)

        self.assertEqual(text.position.column, 1)
Beispiel #20
0
    def test_it_append_first_char_when_sucess(self):
        results = []

        Not(String('123')).parse(Text('abc123'), results)

        self.assertListEqual(results, ['a'])
Beispiel #21
0
    def test_it_raises_parse_error_when_parser_not_fails(self):
        parser = Not(String('abc'))

        with self.assertRaises(ParseError):
            parser.parse(Text('abc123'), [])
Beispiel #22
0
    def test_it_forward_text_from_given_length(self):
        text = Text('abc123')

        text.forward(3)

        self.assertEqual(text.lookup(3), '123')
Beispiel #23
0
    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'), [])
Beispiel #24
0
    def test_it_returns_chars_when_chars_left(self):
        text = Text('abc123')

        fragment = text.lookup(3)

        self.assertEqual(fragment, 'abc')