Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion reqif/parsers/spec_object_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ def unparse(spec_object: ReqIFSpecObject) -> str:

if spec_object.xml_node is not None:
children_tags = list(map(lambda el: el.tag, list(spec_object.xml_node)))
assert "VALUES" in children_tags
assert "TYPE" in children_tags
# VALUES is optional. Write it back only if the object has
# attributes, e.g. ones added after parsing.
if "VALUES" not in children_tags and len(spec_object.attributes) > 0:
children_tags.insert(children_tags.index("TYPE"), "VALUES")
else:
children_tags = ["ALTERNATIVE-ID", "VALUES", "TYPE"]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:configuration="http://eclipse.org/rmf/pror/toolextensions/1.0">
<CORE-CONTENT>
<REQ-IF-CONTENT>
<SPEC-OBJECTS>
<SPEC-OBJECT IDENTIFIER="TEST_SPEC_OBJECT_WITHOUT_VALUES" LAST-CHANGE="2021-10-15T11:34:36.007+02:00">
<TYPE>
<SPEC-OBJECT-TYPE-REF>TEST_SPEC_OBJECT_TYPE_IDENTIFIER_FUNCTIONAL</SPEC-OBJECT-TYPE-REF>
</TYPE>
</SPEC-OBJECT>
<SPEC-OBJECT IDENTIFIER="TEST_SPEC_OBJECT_WITH_EMPTY_VALUES" LAST-CHANGE="2021-10-15T11:34:36.007+02:00">
<VALUES/>
<TYPE>
<SPEC-OBJECT-TYPE-REF>TEST_SPEC_OBJECT_TYPE_IDENTIFIER_FUNCTIONAL</SPEC-OBJECT-TYPE-REF>
</TYPE>
</SPEC-OBJECT>
</SPEC-OBJECTS>
</REQ-IF-CONTENT>
</CORE-CONTENT>
</REQ-IF>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RUN: mkdir -p %S/output
RUN: %reqif passthrough %S/sample.reqif %S/output/sample.reqif
RUN: %diff %S/sample.reqif %S/output/sample.reqif
44 changes: 44 additions & 0 deletions tests/unit/reqif/parsers/test_spec_object_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from lxml import etree

from reqif.models.reqif_spec_object import SpecObjectAttribute
from reqif.models.reqif_types import SpecObjectAttributeType
from reqif.parsers.spec_object_parser import (
SpecObjectParser,
)
Expand Down Expand Up @@ -115,3 +117,45 @@ def test_02_attributes_xhtml():
].value
== expected_xhtml
)


def test_unparse_spec_object_without_values_element():
spec_object_string = """
<SPEC-OBJECT IDENTIFIER="TEST_NO_VALUES_ID">
<TYPE>
<SPEC-OBJECT-TYPE-REF>TEST_SPEC_OBJECT_TYPE</SPEC-OBJECT-TYPE-REF>
</TYPE>
</SPEC-OBJECT>
"""

spec_object = SpecObjectParser.parse(etree.fromstring(spec_object_string))
output = SpecObjectParser.unparse(spec_object)

assert "<VALUES" not in output
assert (
"<SPEC-OBJECT-TYPE-REF>TEST_SPEC_OBJECT_TYPE</SPEC-OBJECT-TYPE-REF>" in output
)


def test_unparse_spec_object_without_values_element_after_adding_attribute():
spec_object_string = """
<SPEC-OBJECT IDENTIFIER="TEST_NO_VALUES_ID">
<TYPE>
<SPEC-OBJECT-TYPE-REF>TEST_SPEC_OBJECT_TYPE</SPEC-OBJECT-TYPE-REF>
</TYPE>
</SPEC-OBJECT>
"""
spec_object = SpecObjectParser.parse(etree.fromstring(spec_object_string))
spec_object.attributes.append(
SpecObjectAttribute(
attribute_type=SpecObjectAttributeType.STRING,
definition_ref="TEST_FIELD_UID",
value="SR001",
)
)

output = SpecObjectParser.unparse(spec_object)

reparsed = SpecObjectParser.parse(etree.fromstring(output))
assert reparsed.attribute_map["TEST_FIELD_UID"].value == "SR001"
assert output.index("<VALUES>") < output.index("<TYPE>")
Loading