"""testing.py - test support for DHParser based grammars and compilers Copyright 2016 by Eckhart Arnold (arnold@badw.de) Bavarian Academy of Sciences an Humanities (badw.de) 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. """ import collections import configparser import copy import fnmatch import inspect import json import os try: import regex as re except ImportError: import re from DHParser.toolkit import is_logging, clear_logs, error_messages from DHParser.syntaxtree import mock_syntax_tree, flatten_sxpr from DHParser.base import is_error __all__ = ('unit_from_configfile', 'unit_from_json', 'unit_from_file', 'get_report', 'grammar_unit', 'grammar_suite', 'runner') UNIT_STAGES = {'match', 'fail', 'ast', 'cst', '__ast__', '__cst__'} def unit_from_configfile(config_filename): """ Reads a grammar unit test from a config file. """ cfg = configparser.ConfigParser(interpolation=None) cfg.read(config_filename, encoding="utf8") OD = collections.OrderedDict unit = OD() for section in cfg.sections(): symbol, stage = section.split(':') if stage not in UNIT_STAGES: if symbol in UNIT_STAGES: symbol, stage = stage, symbol else: raise ValueError('Test stage %s not in: ' % (stage, str(UNIT_STAGES))) for testkey, testcode in cfg[section].items(): if testcode[:3] + testcode[-3:] in {"''''''", '""""""'}: testcode = testcode[3:-3] # testcode = testcode.replace('\\#', '#') testcode = re.sub(r'(?= 0: cls_name, method_name = test.split('.') obj = instantiate(cls_name) print("Running " + cls_name + "." + method_name) exec('obj.' + method_name + '()') else: obj = instantiate(test) for name in dir(obj): if name.lower().startswith("test"): print("Running " + test + "." + name) exec('obj.' + name + '()') finally: if "teardown" in dir(obj): obj.teardown()