tst_XML_grammar.py 1.69 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/python3

"""tst_XML_grammar.py - runs the unit tests for the XML-grammar
"""

import os
import sys

9
scriptpath = os.path.dirname(__file__) or '.'
10
11
12
13
for path in (os.path.join('..', '..'), '.'):
    fullpath = os.path.abspath(os.path.join(scriptpath, path))
    if fullpath not in sys.path:
        sys.path.append(fullpath)
14
15
16
17
18
19
20
21
22
23
24
25
26


try:
    from DHParser import dsl
    import DHParser.log
    from DHParser import testing
except ModuleNotFoundError:
    print('Could not import DHParser. Please adjust sys.path in file '
          '"%s" manually' % __file__)
    sys.exit(1)


def recompile_grammar(grammar_src, force):
27
28
29
30
31
32
33
    # recompiles Grammar only if it has changed
    if not dsl.recompile_grammar(grammar_src, force=force):
        print('\nErrors while recompiling "%s":' % grammar_src +
              '\n--------------------------------------\n\n')
        with open('XML_ebnf_ERRORS.txt') as f:
            print(f.read())
        sys.exit(1)
34
35
36


def run_grammar_tests(glob_pattern):
37
38
39
40
    error_report = testing.grammar_suite(
        os.path.join(scriptpath, 'grammar_tests'),
        get_grammar, get_transformer,
        fn_patterns=[glob_pattern], verbose=True)
41
42
43
44
45
46
47
48
    return error_report


if __name__ == '__main__':
    arg = sys.argv[1] if len(sys.argv) > 1 else '*_test_*.ini'
    if arg.endswith('.ebnf'):
        recompile_grammar(arg, force=True)
    else:
49
        recompile_grammar(os.path.join(scriptpath, 'XML.ebnf'), force=False)
50
51
52
53
54
55
56
57
        sys.path.append('.')
        from XMLCompiler import get_grammar, get_transformer
        error_report = run_grammar_tests(glob_pattern=arg)
        if error_report:
            print('\n')
            print(error_report)
            sys.exit(1)
        print('ready.\n')