11.3.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

test_syntaxtree.py 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/python3

"""test_syntaxtree.py - test of syntaxtree-module of DHParser 
                             
Author: Eckhart Arnold <arnold@badw.de>

Copyright 2017 Bavarian Academy of Sciences and Humanities

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

22
import copy
23
import sys
Eckhart Arnold's avatar
Eckhart Arnold committed
24 25
sys.path.extend(['../', './'])

26
from DHParser.error import Error
di68kap's avatar
di68kap committed
27
from DHParser.syntaxtree import Node, RootNode, parse_sxpr, flatten_sxpr, TOKEN_PTYPE
Eckhart Arnold's avatar
Eckhart Arnold committed
28
from DHParser.transform import traverse, reduce_single_child, \
29
    replace_by_single_child, flatten, remove_expendables
30
from DHParser.ebnf import get_ebnf_grammar, get_ebnf_transformer, get_ebnf_compiler
31
from DHParser.dsl import grammar_provider
32

33

34 35
class TestMockSyntaxTree:
    def test_mock_syntax_tree(self):
36 37 38 39
        tree = parse_sxpr('(a (b c))')
        assert flatten_sxpr(tree.as_sxpr()) == '(a (b "c"))', flatten_sxpr(tree.as_sxpr())
        tree = parse_sxpr('(a i\nj\nk)')
        assert flatten_sxpr(tree.as_sxpr()) == '(a "i" "j" "k")', flatten_sxpr(tree.as_sxpr())
40
        try:
41 42
            tree = parse_sxpr('a b c')
            assert False, "parse_sxpr() should raise a ValueError " \
43 44 45 46 47
                          "if argument is not a tree!"
        except ValueError:
            pass


48 49 50 51 52 53
class TestNode:
    """
    Tests for class Node 
    """
    def setup(self):
        self.unique_nodes_sexpr = '(a (b c) (d e) (f (g h)))'
54
        self.unique_tree = parse_sxpr(self.unique_nodes_sexpr)
55
        self.recurring_nodes_sexpr = '(a (b x) (c (d e) (b y)))'
56
        self.recurr_tree = parse_sxpr(self.recurring_nodes_sexpr)
57 58 59 60 61 62

    def test_str(self):
        assert str(self.unique_tree) == "ceh"
        assert str(self.recurr_tree) == "xey"

    def test_find(self):
63
        found = list(self.unique_tree.select(lambda nd: not nd.children and nd.result == "e"))
64 65
        assert len(found) == 1
        assert found[0].result == 'e'
66
        found = list(self.recurr_tree.select(lambda nd: nd.tag_name == 'b'))
67 68 69
        assert len(found) == 2
        assert found[0].result == 'x' and found[1].result == 'y'

70
    def test_equality1(self):
71 72
        assert self.unique_tree == self.unique_tree
        assert self.recurr_tree != self.unique_tree
73 74
        assert parse_sxpr('(a (b c))') != parse_sxpr('(a (b d))')
        assert parse_sxpr('(a (b c))') == parse_sxpr('(a (b c))')
75 76 77

    def test_equality2(self):
        ebnf = 'term = term ("*"|"/") factor | factor\nfactor = /[0-9]+/~'
78
        att  = {"term": [replace_by_single_child, flatten],
Eckhart Arnold's avatar
Eckhart Arnold committed
79 80
                "factor": [remove_expendables, reduce_single_child],
                (TOKEN_PTYPE): [remove_expendables, reduce_single_child],
81
                "?": [remove_expendables, replace_by_single_child]}
82
        parser = grammar_provider(ebnf)()
83 84
        tree = parser("20 / 4 * 3")
        traverse(tree, att)
85
        compare_tree = parse_sxpr("(term (term (factor 20) (:Token /) (factor 4)) (:Token *) (factor 3))")
86
        assert tree == compare_tree, tree.as_sxpr()
87

88 89 90 91 92 93 94
    def test_copy(self):
        cpy = copy.deepcopy(self.unique_tree)
        assert cpy == self.unique_tree
        assert cpy.result[0].result != "epsilon" # just to make sure...
        cpy.result[0].result = "epsilon"
        assert cpy != self.unique_tree

95 96 97 98 99 100 101
    def test_copy2(self):
        # test if Node.__deepcopy__ goes sufficiently deep for ast-
        # transformation and compiling to perform correctly after copy
        ebnf = 'term = term ("*"|"/") factor | factor\nfactor = /[0-9]+/~'
        parser = get_ebnf_grammar()
        transform = get_ebnf_transformer()
        compiler = get_ebnf_compiler()
Eckhart Arnold's avatar
Eckhart Arnold committed
102
        tree = parser(ebnf)
103 104 105 106 107 108 109 110 111 112 113 114 115 116
        tree_copy = copy.deepcopy(tree)
        transform(tree_copy)
        res1 = compiler(tree_copy)
        t2 = copy.deepcopy(tree_copy)
        res2 = compiler(t2)
        assert res1 == res2
        tree_copy = copy.deepcopy(tree)
        transform(tree_copy)
        res3 = compiler(tree_copy)
        assert res3 == res2
        transform(tree)
        res4 = compiler(tree)
        assert res4 == res3

117 118 119
    def test_len_and_pos(self):
        """Test len-property of Node."""
        nd1 = Node(None, "123")
120
        assert len(nd1) == 3, "Expected Node.len == 3, got %i" % len(nd1)
121
        nd2 = Node(None, "456")
122
        assert len(nd2) == 3, "Expected Node.len == 3, got %i" % len(nd2)
123
        nd = Node(None, (nd1, nd2))
124
        assert len(nd) == 6, "Expected Node.len == 6, got %i" % len(nd)
eckhart's avatar
eckhart committed
125
        nd.init_pos(0)
126 127 128 129
        assert nd.pos == 0, "Expected Node.pos == 0, got %i" % nd.pos
        assert nd1.pos == 0, "Expected Node.pos == 0, got %i" % nd1.pos
        assert nd2.pos == 3, "Expected Node.pos == 3, got %i" % nd2.pos

di68kap's avatar
di68kap committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

class TestRootNode:
    def test_error_handling(self):
        root = RootNode()
        root.add_error(4, "error B")
        root.add_error(2, "error A")
        assert root.error_flag
        errors = root.collect_errors(False)
        assert root.error_flag
        assert errors == root.collect_errors(True)
        assert not root.error_flag and not root.collect_errors()
        error_str = "\n".join(str(e) for e in errors)
        assert error_str.find("A") < error_str.find("B")


class TestErrorHandling:
    def test_error_flag_propagation(self):
        tree = parse_sxpr('(a (b c) (d (e (f (g h)))))')

        def find_h(context):
            node = context[-1]
            if node.result == "h":
                node.add_error("an error deep inside the syntax tree")

        assert not tree.error_flag
        traverse(tree, {"*": find_h})
        assert tree.error_flag, tree.as_sxpr()

158
    def test_collect_errors(self):
159
        tree = parse_sxpr('(A (B 1) (C (D (E 2) (F 3))))')
160
        A = tree
161 162 163
        B = next(tree.select(lambda node: str(node) == "1"))
        D = next(tree.select(lambda node: node.parser.name == "D"))
        F = next(tree.select(lambda node: str(node) == "3"))
164 165 166 167 168 169 170 171 172 173 174
        B.add_error("Error in child node")
        F.add_error("Error in child's child node")
        tree.error_flag = Error.ERROR
        errors = tree.collect_errors()
        assert len(errors) == 2, str(errors)
        assert A.error_flag
        assert D.error_flag
        errors = tree.collect_errors(clear_errors=True)
        assert len(errors) == 2
        assert not D.error_flag

175

176
class TestNodeFind():
177
    """Test the select-functions of class Node.
178 179 180 181 182 183
    """

    def test_find(self):
        def match_tag_name(node, tag_name):
            return node.tag_name == tag_name
        matchf = lambda node: match_tag_name(node, "X")
184
        tree = parse_sxpr('(a (b X) (X (c d)) (e (X F)))')
185
        matches = list(tree.select(matchf))
186 187 188
        assert len(matches) == 2, len(matches)
        assert str(matches[0]) == 'd', str(matches[0])
        assert str(matches[1]) == 'F', str(matches[1])
189 190
        assert matches[0] == parse_sxpr('(X (c d))')
        assert matches[1] == parse_sxpr('(X F)')
191 192
        # check default: root is included in search:
        matchf2 = lambda node: match_tag_name(node, 'a')
193 194
        assert list(tree.select(matchf2))
        assert not list(tree.select(matchf2, include_root=False))
195 196

    def test_getitem(self):
197 198 199 200
        tree = parse_sxpr('(a (b X) (X (c d)) (e (X F)))')
        # print(tree.as_sxpr())
        assert tree[0] == parse_sxpr('(b X)')
        assert tree[2] == parse_sxpr('(e (X F))')
201 202 203 204 205
        try:
            node = tree[3]
            assert False, "IndexError expected!"
        except IndexError:
            pass
206
        matches = list(tree.select_by_tag('X', False))
207 208
        assert matches[0] == parse_sxpr('(X (c d))')
        assert matches[1] == parse_sxpr('(X F)')
209

210
    def test_contains(self):
211
        tree = parse_sxpr('(a (b X) (X (c d)) (e (X F)))')
212
        assert 'a' not in tree
213 214
        assert any(tree.select_by_tag('a', True))
        assert not any(tree.select_by_tag('a', False))
215 216 217
        assert 'b' in tree
        assert 'X' in tree
        assert 'e' in tree
218
        assert 'c' not in tree
219
        assert any(tree.select_by_tag('c', False))
220 221


222
class TestSerialization:
di68kap's avatar
di68kap committed
223 224 225 226
    def test_sxpr_roundtrip(self):
        pass

    def test_sexpr_attributes(self):
227
        tree = parse_sxpr('(A "B")')
228
        tree.attributes['attr'] = "value"
229
        tree2 = parse_sxpr('(A `(attr "value") "B")')
230 231
        assert tree.as_sxpr() ==  tree2.as_sxpr()
        tree.attributes['attr2'] = "value2"
232
        tree3 = parse_sxpr('(A `(attr "value") `(attr2 "value2") "B")')
233 234 235
        assert tree.as_sxpr() == tree3.as_sxpr()


di68kap's avatar
di68kap committed
236

237
if __name__ == "__main__":
238
    from DHParser.testing import runner
239
    runner("", globals())