Commit 5220a7f7 authored by di68kap's avatar di68kap
Browse files

- inferus vervollständigt

parent 07003c56
......@@ -372,7 +372,7 @@ def log_ST(syntax_tree, log_file_name):
LOG_SIZE_THRESHOLD = 100000 # maximum number of history records to log
LOG_SIZE_THRESHOLD = 10000 # maximum number of history records to log
LOG_TAIL_THRESHOLD = 500 # maximum number of history recors for "tail log"
......@@ -44,8 +44,9 @@ from DHParser.toolkit import re, typing
from typing import Tuple
__all__ = ('unit_from_configfile',
__all__ = ('unit_from_config',
......@@ -93,12 +94,12 @@ RX_ENTRY = re.compile('\s*(\w+\*?)\s*:\s*(?:{value})\s*'.format(value=RE_VALUE))
RX_COMMENT = re.compile('\s*#.*\n')
def unit_from_configfile(config_filename):
def unit_from_config(config_str):
""" Reads grammar unit tests contained in a file in config file (.ini)
config_filename (str): A config file containing Grammar unit-tests
config_str (str): A string containing a config-file with Grammar unit-tests
A dictionary representing the unit tests.
......@@ -112,9 +113,7 @@ def unit_from_configfile(config_filename):
m = RX_COMMENT.match(txt, pos)
return pos
with open(config_filename, 'r', encoding="utf-8") as f:
cfg =
cfg = cfg.replace('\t', ' ')
cfg = config_str.replace('\t', ' ')
OD = collections.OrderedDict
unit = OD()
......@@ -154,31 +153,41 @@ def unit_from_configfile(config_filename):
return unit
def unit_from_json(json_filename):
def unit_from_json(json_str):
Reads grammar unit tests from a json file.
Reads grammar unit tests from a json string.
with open(json_filename, 'r', encoding='utf8') as f:
unit = json.load(f)
unit = json.loads(json_str)
for symbol in unit:
for stage in unit[symbol]:
if stage not in UNIT_STAGES:
raise ValueError('Test stage %s not in: %s' % (stage, str(UNIT_STAGES)))
return unit
# TODO: add support for yaml, cson, toml
# A dictionary associating file endings with reader functions that
# transfrom strings containing the file's content to a nested dictionary
# structure of test cases.
'.ini': unit_from_config,
'.json': unit_from_json
def unit_from_file(filename):
Reads a grammar unit test from a file. The format of the file is
determined by the ending of its name.
if filename.endswith(".json"):
test_unit = unit_from_json(filename)
elif filename.endswith(".ini"):
test_unit = unit_from_configfile(filename)
reader = TEST_READERS[os.path.splitext(filename)[1].lower()]
with open(filename, 'r', encoding='utf8') as f:
data =
test_unit = reader(data)
except KeyError:
raise ValueError("Unknown unit test file type: " + filename[filename.rfind('.'):])
# Check for ambiguous Test names
......@@ -155,7 +155,14 @@ def run_grammar_tests(glob_pattern):
if __name__ == '__main__':
arg = sys.argv[1] if len(sys.argv) > 1 else '*_test_*.ini'
if (len(sys.argv) == 2 and (arg.endswith('.ebnf') or (os.path.isfile(sys.argv[1]) and
os.path.splitext(sys.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 = sys.argv[1]
# otherwise run all tests in the test directory
arg = '*_test_*.ini'
if arg.endswith('.ebnf'):
recompile_grammar(arg, force=True)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment