test junitxml: split the DomNode type to express the Document/Element difference
This commit is contained in:
parent
fd3ee18ff8
commit
31ccb8ad47
|
@ -1,4 +1,3 @@
|
||||||
# mypy: allow-untyped-defs
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
@ -40,7 +39,7 @@ class RunAndParse:
|
||||||
|
|
||||||
def __call__(
|
def __call__(
|
||||||
self, *args: str | os.PathLike[str], family: str | None = "xunit1"
|
self, *args: str | os.PathLike[str], family: str | None = "xunit1"
|
||||||
) -> tuple[RunResult, DomNode]:
|
) -> tuple[RunResult, DomDocument]:
|
||||||
if family:
|
if family:
|
||||||
args = ("-o", "junit_family=" + family, *args)
|
args = ("-o", "junit_family=" + family, *args)
|
||||||
xml_path = self.pytester.path.joinpath("junit.xml")
|
xml_path = self.pytester.path.joinpath("junit.xml")
|
||||||
|
@ -49,7 +48,7 @@ class RunAndParse:
|
||||||
with xml_path.open(encoding="utf-8") as f:
|
with xml_path.open(encoding="utf-8") as f:
|
||||||
self.schema.validate(f)
|
self.schema.validate(f)
|
||||||
xmldoc = minidom.parse(str(xml_path))
|
xmldoc = minidom.parse(str(xml_path))
|
||||||
return result, DomNode(xmldoc)
|
return result, DomDocument(xmldoc)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -75,25 +74,21 @@ def assert_attr(node: minidom.Element, **kwargs: object) -> None:
|
||||||
assert on_node == expected
|
assert on_node == expected
|
||||||
|
|
||||||
|
|
||||||
class DomNode:
|
class DomDocument:
|
||||||
def __init__(self, dom: minidom.Element | minidom.Document):
|
def __init__(self, dom: minidom.Document):
|
||||||
self.__node = dom
|
self.__node = dom
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
__node: minidom.Document
|
||||||
return self.__node.toxml()
|
|
||||||
|
|
||||||
def find_first_by_tag(self, tag: str) -> DomNode | None:
|
def find_first_by_tag(self, tag: str) -> DomNode | None:
|
||||||
return self.find_nth_by_tag(tag, 0)
|
return self.find_nth_by_tag(tag, 0)
|
||||||
|
|
||||||
@property
|
def get_first_by_tag(self, tag: str) -> DomNode:
|
||||||
def children(self) -> list[DomNode]:
|
maybe = self.find_first_by_tag(tag)
|
||||||
return [type(self)(x) for x in self.__node.childNodes]
|
if maybe is None:
|
||||||
|
raise LookupError(tag)
|
||||||
@property
|
else:
|
||||||
def get_unique_child(self) -> DomNode:
|
return maybe
|
||||||
children = self.children
|
|
||||||
assert len(children) == 1
|
|
||||||
return children[0]
|
|
||||||
|
|
||||||
def find_nth_by_tag(self, tag: str, n: int) -> DomNode | None:
|
def find_nth_by_tag(self, tag: str, n: int) -> DomNode | None:
|
||||||
items = self.__node.getElementsByTagName(tag)
|
items = self.__node.getElementsByTagName(tag)
|
||||||
|
@ -102,15 +97,35 @@ class DomNode:
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return type(self)(nth)
|
return DomNode(nth)
|
||||||
|
|
||||||
def find_by_tag(self, tag: str) -> list[DomNode]:
|
def find_by_tag(self, tag: str) -> list[DomNode]:
|
||||||
t = type(self)
|
return [DomNode(x) for x in self.__node.getElementsByTagName(tag)]
|
||||||
return [t(x) for x in self.__node.getElementsByTagName(tag)]
|
|
||||||
|
@property
|
||||||
|
def children(self) -> list[DomNode]:
|
||||||
|
return [DomNode(x) for x in self.__node.childNodes]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def get_unique_child(self) -> DomNode:
|
||||||
|
children = self.children
|
||||||
|
assert len(children) == 1
|
||||||
|
return children[0]
|
||||||
|
|
||||||
|
def toxml(self) -> str:
|
||||||
|
return self.__node.toxml()
|
||||||
|
|
||||||
|
|
||||||
|
class DomNode(DomDocument):
|
||||||
|
__node: minidom.Element
|
||||||
|
|
||||||
|
def __init__(self, dom: minidom.Element):
|
||||||
|
self.__node = dom
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return self.toxml()
|
||||||
|
|
||||||
def __getitem__(self, key: str) -> str:
|
def __getitem__(self, key: str) -> str:
|
||||||
if isinstance(self.__node, minidom.Document):
|
|
||||||
raise TypeError(type(self.__node))
|
|
||||||
node = self.__node.getAttributeNode(key)
|
node = self.__node.getAttributeNode(key)
|
||||||
if node is not None:
|
if node is not None:
|
||||||
return cast(str, node.value)
|
return cast(str, node.value)
|
||||||
|
@ -119,31 +134,19 @@ class DomNode:
|
||||||
|
|
||||||
def assert_attr(self, **kwargs: object) -> None:
|
def assert_attr(self, **kwargs: object) -> None:
|
||||||
__tracebackhide__ = True
|
__tracebackhide__ = True
|
||||||
assert isinstance(self.__node, minidom.Element)
|
|
||||||
return assert_attr(self.__node, **kwargs)
|
return assert_attr(self.__node, **kwargs)
|
||||||
|
|
||||||
def toxml(self) -> str:
|
|
||||||
return self.__node.toxml()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def text(self) -> str:
|
def text(self) -> str:
|
||||||
return cast(str, self.__node.childNodes[0].wholeText)
|
return cast(str, self.__node.childNodes[0].wholeText)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tag(self) -> str:
|
def tag(self) -> str:
|
||||||
assert isinstance(self.__node, minidom.Element)
|
|
||||||
return self.__node.tagName
|
return self.__node.tagName
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def next_sibling(self) -> DomNode:
|
def next_sibling(self) -> DomNode:
|
||||||
return type(self)(self.__node.nextSibling)
|
return DomNode(self.__node.nextSibling)
|
||||||
|
|
||||||
def get_first_by_tag(self, tag: str) -> DomNode:
|
|
||||||
maybe = self.find_first_by_tag(tag)
|
|
||||||
if maybe is None:
|
|
||||||
raise LookupError(tag)
|
|
||||||
else:
|
|
||||||
return maybe
|
|
||||||
|
|
||||||
|
|
||||||
parametrize_families = pytest.mark.parametrize("xunit_family", ["xunit1", "xunit2"])
|
parametrize_families = pytest.mark.parametrize("xunit_family", ["xunit1", "xunit2"])
|
||||||
|
|
Loading…
Reference in New Issue