Skip to content

Commit 272477f

Browse files
authored
Merge pull request #111 from strictdoc-project/stanislaw/develop
helpers/lxml: simplify is_self_closed_tag()
2 parents 1d9a363 + da075e2 commit 272477f

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

reqif/helpers/lxml.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,4 @@ def is_self_closed_tag(xml):
9696
return False
9797
if xml.text is not None:
9898
return False
99-
data_type_string = etree.tostring(xml, pretty_print=True).decode("utf-8")
100-
return data_type_string.find(f"</{xml.tag}>") == -1
99+
return True

tests/unit/reqif/helpers/test_lxml.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from lxml import etree
22

3-
from reqif.helpers.lxml import dump_xml_node, stringify_namespaced_children
3+
from reqif.helpers.lxml import (
4+
dump_xml_node,
5+
is_self_closed_tag,
6+
stringify_namespaced_children,
7+
)
48

59

610
def test_01_dump_xml() -> None:
@@ -45,3 +49,25 @@ def test__02_stringify_namespaced_children__02_nested_tags_and_attrs() -> None:
4549
string = stringify_namespaced_children(spec_type_xml)
4650

4751
assert string == expected_string
52+
53+
54+
def test__is_self_closed_tag() -> None:
55+
spec_type_string = """\
56+
<THE-VALUE>\
57+
Text
58+
</THE-VALUE>
59+
"""
60+
spec_type_xml = etree.fromstring(spec_type_string)
61+
assert is_self_closed_tag(spec_type_xml) is False
62+
63+
spec_type_string = """\
64+
<THE-VALUE></THE-VALUE>
65+
"""
66+
spec_type_xml = etree.fromstring(spec_type_string)
67+
assert is_self_closed_tag(spec_type_xml) is True
68+
69+
spec_type_string = """\
70+
<THE-VALUE/>
71+
"""
72+
spec_type_xml = etree.fromstring(spec_type_string)
73+
assert is_self_closed_tag(spec_type_xml) is True

0 commit comments

Comments
 (0)