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

"""tst_EBNF_grammar.py - runs the unit tests for the EBNF-grammar
"""

import os
import sys

di68kap's avatar
di68kap committed
9
LOGGING = ''
10

11
scriptpath = os.path.dirname(__file__) or '.'
12
13
14
15
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)
16
17
18
19
20


try:
    from DHParser import dsl
    import DHParser.log
21
    from DHParser import testing, create_test_templates, access_presets, finalize_presets
22
23
24
25
26
27
28
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):
eckhart's avatar
eckhart committed
29
30
31
32
33
34
    grammar_tests_dir = os.path.join(scriptpath, 'grammar_tests')
    if not os.path.exists(grammar_tests_dir) \
            or not any(os.path.isfile(os.path.join(grammar_tests_dir, entry))
                       for entry in os.listdir(grammar_tests_dir)):
        print('No grammar-tests found, generating test templates.')
        create_test_templates(grammar_src, grammar_tests_dir)
35
36
37
38
39
40
41
42
    DHParser.log.start_logging(LOGGING)
    # 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('EBNF_ebnf_ERRORS.txt') as f:
            print(f.read())
        sys.exit(1)
43
44
45


def run_grammar_tests(glob_pattern):
eckhart's avatar
eckhart committed
46
    grammar_tests_dir = os.path.join(scriptpath, 'grammar_tests')
47
48
49
50
51
    DHParser.log.start_logging(LOGGING)
    error_report = testing.grammar_suite(
        grammar_tests_dir,
        get_grammar, get_transformer,
        fn_patterns=[glob_pattern], verbose=True)
52
53
54
55
    return error_report


if __name__ == '__main__':
56
57
58
    access_presets()['ast_serialization'] = "S-expression"
    finalize_presets()

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    argv = sys.argv[:]
    if len(argv) > 1 and sys.argv[1] == "--debug":
        LOGGING = True
        del argv[1]
    if (len(argv) >= 2 and (argv[1].endswith('.ebnf') or
        os.path.splitext(argv[1])[1].lower() in testing.TEST_READERS.keys())):
        # if called with a single filename that is either an EBNF file or a known
        # test file type then use the given argument
        arg = argv[1]
    else:
        # otherwise run all tests in the test directory
        arg = '*_test_*.ini'
    if arg.endswith('.ebnf'):
        recompile_grammar(arg, force=True)
    else:
        recompile_grammar(os.path.join(scriptpath, 'EBNF.ebnf'),
                          force=False)
        sys.path.append('.')
        from EBNFCompiler 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')