From 8e97317b57558100b764ddc720312d29386e01ad Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Wed, 15 Jun 2022 13:01:27 +0200 Subject: [PATCH] Added a clouple debug tools --- src/bootstraparse/modules/syntax.py | 8 ++++++++ src/bootstraparse/modules/tools.py | 31 +++++++++++++++++++++++++++++ tests/modules/test_context_mngr.py | 5 ++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/bootstraparse/modules/syntax.py b/src/bootstraparse/modules/syntax.py index bae84b4..3fc1c70 100644 --- a/src/bootstraparse/modules/syntax.py +++ b/src/bootstraparse/modules/syntax.py @@ -86,6 +86,14 @@ class SemanticType: def __ne__(self, other): return not self.__eq__(other) + def __iter__(self): + """ + Iterator over the content of the token. + :yield: The iterator over the content of the token. + :ytype: iterable + """ + return iter(self.content) + def print_all(self, ident=0): """ Print all lines of the token. diff --git a/src/bootstraparse/modules/tools.py b/src/bootstraparse/modules/tools.py index 2d5619b..f96e57b 100644 --- a/src/bootstraparse/modules/tools.py +++ b/src/bootstraparse/modules/tools.py @@ -174,6 +174,37 @@ def find_variables_in_file(file_path, list_variable_name): # pragma: no cover ( return find_string_in_file(file_path, [re.compile(f"^({variable_name}) ?=") for variable_name in list_variable_name]) +def compare(me, other): + """ + Compares the container to another container. + :param me: The container to compare + :param other: The container to compare to + :type me: Container + :type other: Container + :return: True if the containers are equal, False otherwise + :rtype: bool + """ + import rich + from bootstraparse.modules import syntax, context_mngr + + for e, f in zip(me, other): + if isinstance(e, context_mngr.BaseContainer) or isinstance(e, syntax.SemanticType): + compare(e, f) + elif e != f: + rich.print(f'elt:{e} != {f}') + return False + if hasattr(me, "optionals"): + if me.optionals != other.optionals: + rich.print(f'opt:{me.optionals} != {other.optionals}') + return False + if hasattr(me, "others"): + if me.others != other.others: + rich.print(f'oth:{me.others} != {other.others}') + return False + rich.print("No differences found") + return True + + if __name__ == "__main__": # pragma: no cover # print(find_string_in_file(__module_path("syntax.py"), [re.compile(r"^(html_insert) ?=")])) pass diff --git a/tests/modules/test_context_mngr.py b/tests/modules/test_context_mngr.py index d3ca1e3..f4b24d6 100644 --- a/tests/modules/test_context_mngr.py +++ b/tests/modules/test_context_mngr.py @@ -1,7 +1,7 @@ import pytest import rich -from bootstraparse.modules import context_mngr, export, error_mngr +from bootstraparse.modules import context_mngr, export, error_mngr, tools from bootstraparse.modules import syntax as sy #sy.SemanticType, sy.TextToken, sy.Linebreak, sy.StructuralElementStartToken, sy.StructuralElementEndToken, sy.EtUlistToken, sy.EtOlistToken # noqa from bootstraparse.modules.tools import __GLk __XF = pytest.mark.xfail @@ -646,8 +646,7 @@ def test_context_call(init_list, expected, file_line): for i, e in zip(ctx(), expected): # rich.inspect(i) # rich.inspect(e) - i.print_all() - e.print_all() + tools.compare(i, e) assert i == e else: with pytest.raises(expected[0]):