Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

test_syntaxtree.py 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/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.
"""

import os
import sys
sys.path.append(os.path.abspath('../../'))
25
from DHParser.toolkit import compact_sexpr
26
from DHParser.syntaxtree import traverse, mock_syntax_tree
27

28

29
class MockParser:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    def __init__(self, name=''):
        self.name = name

    def __str__(self):
        return self.name or self.__class__.__name__

    def __call__(self, text):
        return None, text


class TestSExpr:
    """
    Tests for S-expression handling.
    """
    def test_compact_sexpr(self):
        assert compact_sexpr("(a\n    (b\n        c\n    )\n)\n") == "(a (b c))"

47
    def test_mock_syntax_tree(self):
48
        sexpr = '(a (b c) (d e) (f (g h)))'
49
        tree = mock_syntax_tree(sexpr)
50 51 52 53 54 55 56 57 58 59 60 61 62 63
        assert compact_sexpr(tree.as_sexpr().replace('"', '')) == sexpr

        # test different quotation marks
        sexpr = '''(a (b """c""" 'k' "l") (d e) (f (g h)))'''
        sexpr_stripped = '(a (b c k l) (d e) (f (g h)))'
        tree = mock_syntax_tree(sexpr)
        assert compact_sexpr(tree.as_sexpr().replace('"', '')) == sexpr_stripped

        sexpr_clean = '(a (b "c" "k" "l") (d "e") (f (g "h")))'
        tree = mock_syntax_tree(sexpr_clean)
        assert compact_sexpr(tree.as_sexpr()) == sexpr_clean

        tree = mock_syntax_tree(sexpr_stripped)
        assert compact_sexpr(tree.as_sexpr()) == '(a (b "c k l") (d "e") (f (g "h")))'
64 65 66 67 68 69 70 71


class TestNode:
    """
    Tests for class Node 
    """
    def setup(self):
        self.unique_nodes_sexpr = '(a (b c) (d e) (f (g h)))'
72
        self.unique_tree = mock_syntax_tree(self.unique_nodes_sexpr)
73
        self.recurring_nodes_sexpr = '(a (b x) (c (d e) (b y)))'
74
        self.recurr_tree = mock_syntax_tree(self.recurring_nodes_sexpr)
75 76 77 78 79 80 81 82 83 84 85 86 87

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

    def test_find(self):
        found = list(self.unique_tree.find(lambda nd: not nd.children and nd.result == "e"))
        assert len(found) == 1
        assert found[0].result == 'e'
        found = list(self.recurr_tree.find(lambda nd: nd.tag_name == 'b'))
        assert len(found) == 2
        assert found[0].result == 'x' and found[1].result == 'y'

88 89 90 91 92
    def test_equality(self):
        assert self.unique_tree == self.unique_tree
        assert self.recurr_tree != self.unique_tree
        assert mock_syntax_tree('(a (b c))') != mock_syntax_tree('(a (b d))')

93

94
class TestErrorHandling:
95
    def test_error_flag_propagation(self):
96
        tree = mock_syntax_tree('(a (b c) (d (e (f (g h)))))')
97 98 99 100 101 102 103 104 105 106

        def find_h(node):
            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


107
if __name__ == "__main__":
108
    from run import runner
109
    runner("TestNode", globals())