From 06538d61028074a56e5612b7ccd2820dcef07eec Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Sun, 20 Mar 2022 07:51:29 +0100 Subject: [PATCH] Added tools for test verbosity --- tests/modules/test_syntax.py | 35 ++++++++--- tests/modules/tools.py | 113 +++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 tests/modules/tools.py diff --git a/tests/modules/test_syntax.py b/tests/modules/test_syntax.py index c8471c7..bbbded5 100644 --- a/tests/modules/test_syntax.py +++ b/tests/modules/test_syntax.py @@ -2,19 +2,33 @@ from itertools import zip_longest import pyparsing import pytest -import inspect - import bootstraparse.modules.syntax as sy - +from tools import __GL, __module_path, find_variables_in_file # , find_functions_in_file, find_classes_in_file ptp = pytest.param __XF = pytest.mark.xfail -# Cursed frame inspection -def __GL(): - return inspect.getframeinfo(inspect.currentframe().f_back).lineno +############################################################################################################## +# Utility functions +############################################################################################################## +def likely_definition(list_expression, file): + """ + Returns a dict for each expresion and the associated line where this expression is likely to be defined. + :param list_expression: list of expressions + :param file: file to search in + :return: dict of expression and line + """ + likely_definition_dict = {} + loop_list = [e for e in list_expression] + with open(file) as f: + for line_number, line in enumerate(f): + for expression_id, expression in enumerate(loop_list): + if expression in line: + likely_definition_dict[expression] = line_number+1 + loop_list.remove(expression) + return likely_definition_dict, file ############################################################################################################## @@ -368,6 +382,9 @@ zipped_dict_advanced_syntax_input_and_expected_output = [ ] for item in sublist ] +__syntax_file = __module_path("syntax.py") +__definition_of_syntax_elements = find_variables_in_file(__syntax_file, dict_advanced_syntax_input_and_expected_output.keys()) + # test__add_tag list_add_tag_input_and_expected_output = [ # Test cases for the add_tag function @@ -551,8 +568,12 @@ def test_advanced_expression_and_token_creation(markup_element, to_parse, expect assert isinstance(expr, pyparsing.ParserElement) result = expr.parse_string(to_parse) print() - print(f"Parsing expression: {to_parse} with {markup_element}") + print(f"Parsing string: '{to_parse}'") print("Defined at %(filename)s:%(lineno)d" % {'filename': __file__, 'lineno': line_test}) + print(f"With expression: '{markup_element}'") + # print(likely_definition_of_advanced_syntax) + print("Defined at %(filename)s:%(lineno)d" % {'filename': __syntax_file, + 'lineno': __definition_of_syntax_elements[markup_element]}) print(f"Found: {result} (len:{len(result)}).") print(f"Expected: {expected} (len:{len(expected)})") assert result is not None diff --git a/tests/modules/tools.py b/tests/modules/tools.py new file mode 100644 index 0000000..8b308ec --- /dev/null +++ b/tests/modules/tools.py @@ -0,0 +1,113 @@ +import inspect +import os +import re + + +############################################################################### +# List of tools for testing +############################################################################### +############################################################################### +# Paths +######################################## + + +def __base(): + """ + Return the path of the main module (relative to the tests) + :return: Path of the main module + :rtype: str + """ + return "../../src/bootstraparse/" + + +def __module_path(module_name): + """ + Return the path of the module module_name + :param module_name: Name of the module + :type module_name: str + :return: Path of the module + :rtype: str + """ + return os.path.normpath(os.path.join(os.path.dirname(__file__), __base(), "modules", module_name)) + + +############################################################################### +# Frame inspection +######################################## +# Cursed frame inspection +def __GL(): + return inspect.getframeinfo(inspect.currentframe().f_back).lineno + + +############################################################################### +# Code inspection +######################################## +def find_string_in_file(file_path, list_regex_to_find): + """ + Find the string in the file stream + :param file_path: Path of the file + :type file_path: str + :param list_regex_to_find: List of string to find + :type list_regex_to_find: list + :return: Dict of string found and line number + :rtype: dict + """ + dict_string_found = {} + list_regex_to_find_left = [string for string in list_regex_to_find] + with open(file_path, "r") as file: + for line_number, line in enumerate(file): + for string_number, regex_to_find in enumerate(list_regex_to_find_left): + match = regex_to_find.search(line) + if match: + dict_string_found[match.group(1)] = line_number + 1 + list_regex_to_find_left.pop(string_number) + break + if not list_regex_to_find_left: + break + if list_regex_to_find_left: + print("String not found: " + str(list_regex_to_find_left)) + return dict_string_found + + +def find_functions_in_file(file_path, list_function_name): + """ + Find the function in the file stream + :param file_path: Path of the file + :type file_path: str + :param list_function_name: List of function name to find + :type list_function_name: iterable + :return: Dict of function found and line number + :rtype: dict + """ + return find_string_in_file(file_path, [re.compile(f"$def ({function_name})") for function_name in list_function_name]) + + +def find_classes_in_file(file_path, list_class_name): + """ + Find the class in the file stream + :param file_path: Path of the file + :type file_path: str + :param list_class_name: List of class name to find + :type list_class_name: iterable + :return: Dict of class found and line number + :rtype: dict + """ + return find_string_in_file(file_path, [re.compile(f"^class ({class_name})") for class_name in list_class_name]) + + +def find_variables_in_file(file_path, list_variable_name): + """ + Find the variable in the file stream + :param file_path: Path of the file + :type file_path: str + :param list_variable_name: List of variable name to find + :type list_variable_name: iterable + :return: Dict of variable found and line number + :rtype: dict + """ + return find_string_in_file(file_path, [re.compile(f"^({variable_name}) ?=") for variable_name in list_variable_name]) + + +if __name__ == "__main__": + # print(find_string_in_file(__module_path("syntax.py"), [re.compile(r"^(html_insert) ?=")])) + pass