test_dsl.py 2.82 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
#!/usr/bin/python3

"""test_dsl.py - tests of the dsl-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.
"""

23 24
import os
import sys
25 26

from DHParser.dsl import compile_on_disk, run_compiler
27

28 29
class TestCompilerGeneration:
    trivial_lang = """
30
        text = { word | WSPC } "."
31
        word = /\w+/
32
        WSPC = /\s+/
33
        """
34
    tmp = 'tmp/' if os.path.isdir('tmp') else ('test/tmp/')
35
    trivial_text = """Es war ein König in Thule."""
36 37 38 39
    grammar_name = tmp + "TestCompilerGeneration.ebnf"
    compiler_name = tmp + "TestCompilerGeneration_compiler.py"
    text_name = tmp + "TestCompilerGeneration_text.txt"
    result_name = tmp + "TestCompilerGeneration_text.xml"
40 41

    def setup(self):
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        with open(self.grammar_name, "w") as f:
            f.write(self.trivial_lang)
        with open(self.text_name, "w") as f:
            f.write(self.trivial_text)

    def teardown(self):
        for name in (self.grammar_name, self.compiler_name, self.text_name, self.result_name):
            if os.path.exists(name):
                os.remove(name)

    def test_compiling_functions(self):
        # test if cutting and reassembling of compiler suite works:
        errors = compile_on_disk(self.grammar_name)
        assert not errors
        with open(self.compiler_name, 'r') as f:
            compiler_suite = f.read()
        errors = compile_on_disk(self.grammar_name)
        assert not errors
        with open(self.compiler_name, 'r') as f:
            compiler_suite_2nd_run = f.read()
        assert compiler_suite == compiler_suite_2nd_run

        # test compiling with a generated compiler suite
        errors = compile_on_disk(self.text_name, self.compiler_name)
        assert not errors, str(errors)
        assert os.path.exists(self.result_name)
        with open(self.result_name, 'r') as f:
            output = f.read()
70

71 72 73
        # test compiling in memory
        result = run_compiler(self.trivial_text, self.compiler_name)
        assert output == result.as_xml(), str(result)
74

75
        sys.path.append(self.tmp)
76 77
        from TestCompilerGeneration_compiler import compile_TestCompilerGeneration
        result, errors, ast = compile_TestCompilerGeneration(self.trivial_text)
78 79 80 81 82


if __name__ == "__main__":
    from run import runner
    runner("", globals())