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): ...@@ -372,7 +372,7 @@ def log_ST(syntax_tree, log_file_name):
f.write(syntax_tree.as_sxpr()) f.write(syntax_tree.as_sxpr())
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" LOG_TAIL_THRESHOLD = 500 # maximum number of history recors for "tail log"
......
...@@ -44,8 +44,9 @@ from DHParser.toolkit import re, typing ...@@ -44,8 +44,9 @@ from DHParser.toolkit import re, typing
from typing import Tuple from typing import Tuple
__all__ = ('unit_from_configfile', __all__ = ('unit_from_config',
'unit_from_json', 'unit_from_json',
'TEST_READERS',
'unit_from_file', 'unit_from_file',
'get_report', 'get_report',
'grammar_unit', 'grammar_unit',
...@@ -93,12 +94,12 @@ RX_ENTRY = re.compile('\s*(\w+\*?)\s*:\s*(?:{value})\s*'.format(value=RE_VALUE)) ...@@ -93,12 +94,12 @@ RX_ENTRY = re.compile('\s*(\w+\*?)\s*:\s*(?:{value})\s*'.format(value=RE_VALUE))
RX_COMMENT = re.compile('\s*#.*\n') 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) """ Reads grammar unit tests contained in a file in config file (.ini)
syntax. syntax.
Args: Args:
config_filename (str): A config file containing Grammar unit-tests config_str (str): A string containing a config-file with Grammar unit-tests
Returns: Returns:
A dictionary representing the unit tests. A dictionary representing the unit tests.
...@@ -112,9 +113,7 @@ def unit_from_configfile(config_filename): ...@@ -112,9 +113,7 @@ def unit_from_configfile(config_filename):
m = RX_COMMENT.match(txt, pos) m = RX_COMMENT.match(txt, pos)
return pos return pos
with open(config_filename, 'r', encoding="utf-8") as f: cfg = config_str.replace('\t', ' ')
cfg = f.read()
cfg = cfg.replace('\t', ' ')
OD = collections.OrderedDict OD = collections.OrderedDict
unit = OD() unit = OD()
...@@ -154,31 +153,41 @@ def unit_from_configfile(config_filename): ...@@ -154,31 +153,41 @@ def unit_from_configfile(config_filename):
return unit 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.loads(json_str)
unit = json.load(f)
for symbol in unit: for symbol in unit:
for stage in unit[symbol]: for stage in unit[symbol]:
if stage not in UNIT_STAGES: if stage not in UNIT_STAGES:
raise ValueError('Test stage %s not in: %s' % (stage, str(UNIT_STAGES))) raise ValueError('Test stage %s not in: %s' % (stage, str(UNIT_STAGES)))
return unit return unit
# TODO: add support for yaml, cson, toml # 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.
TEST_READERS = {
'.ini': unit_from_config,
'.json': unit_from_json
}
def unit_from_file(filename): def unit_from_file(filename):
""" """
Reads a grammar unit test from a file. The format of the file is Reads a grammar unit test from a file. The format of the file is
determined by the ending of its name. determined by the ending of its name.
""" """
if filename.endswith(".json"): try:
test_unit = unit_from_json(filename) reader = TEST_READERS[os.path.splitext(filename)[1].lower()]
elif filename.endswith(".ini"): with open(filename, 'r', encoding='utf8') as f:
test_unit = unit_from_configfile(filename) data = f.read()
else: test_unit = reader(data)
except KeyError:
raise ValueError("Unknown unit test file type: " + filename[filename.rfind('.'):]) raise ValueError("Unknown unit test file type: " + filename[filename.rfind('.'):])
# Check for ambiguous Test names # Check for ambiguous Test names
......
...@@ -155,7 +155,14 @@ def run_grammar_tests(glob_pattern): ...@@ -155,7 +155,14 @@ def run_grammar_tests(glob_pattern):
if __name__ == '__main__': 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]
else:
# otherwise run all tests in the test directory
arg = '*_test_*.ini'
if arg.endswith('.ebnf'): if arg.endswith('.ebnf'):
recompile_grammar(arg, force=True) recompile_grammar(arg, force=True)
else: else:
......
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