diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/assertions.py b/tests/assertions.py new file mode 100644 index 0000000..14d21f6 --- /dev/null +++ b/tests/assertions.py @@ -0,0 +1,71 @@ +from xml import etree +from xml.dom import minidom +import textwrap + + +def assert_xml_equal(xml_string, expected_xml_string): + """ + Assert equality of two xml strings, particularly that the contents of + each string have the same elements, with the same attributes (e.g. class, + text) and the same non-xml string contents + """ + # this prints a human-formatted string of what the test passed in -- useful + # if you need to modify test expectations after you've modified + # a rendering and tested it visually + print(to_readable_error_output(xml_string)) + + assert_elements_equal( + etree.ElementTree.fromstring(tostring(xml_string)), + etree.ElementTree.fromstring(tostring(expected_xml_string)), + ) + + +def assert_elements_equal(element, reference_element): + """ + Assert, recursively, the equality of two etree objects. + """ + assert ( + element.text == reference_element.text + ), f"Text doesn't match: {element.text} =/= {reference_element.text}." + assert ( + element.attrib == reference_element.attrib + ), f"Attrib doesn't match: {element.attrib} =/= {reference_element.attrib}" + assert len(element) == len( + reference_element + ), f"Expected {len(reference_element)} children but got {len(element)}" + for sub_element, reference_sub_element in zip(element, reference_element): + assert_elements_equal(sub_element, reference_sub_element) + + +def tostring(xml_string): + """ + Wraps `xml_string` in a div so it can be rendered, even if it has multiple roots. + """ + return remove_indents(f"
Some Text
.Some Text
+ .mocklib.example_function
(a, b=None, *args, **kwargs)This is a function with a docstring.
+ mocklib.
+ example_function
+
+ (
+ a
+ ,
+ b=None
+ ,
+ *args
+ ,
+ **kwargs
+ )
+ + This is a function with a docstring. +
+mocklib.example_async_function
()
+ mocklib.
+ example_async_function
+
+ (
+ )
+ mocklib.ExampleClass
()This is a class with a docstring.
example_method
(self, a, b=None)This is a method with a docstring.
example_property
This is a property with a docstring.
+ mocklib.
+ ExampleClass
+
+ (
+ )
+ + This is a class with a docstring. +
+
+ example_method
+
+ (
+ self
+ ,
+ a
+ ,
+ b=None
+ )
+ + This is a method with a docstring. +
+
+ example_property
+
+ + This is a property with a docstring. +
+