diff --git a/reqif/parsers/spec_object_parser.py b/reqif/parsers/spec_object_parser.py index 0438873..4b3b412 100644 --- a/reqif/parsers/spec_object_parser.py +++ b/reqif/parsers/spec_object_parser.py @@ -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"] diff --git a/tests/integration/reqif/SPEC-OBJECTS/02_spec_object_without_values/sample.reqif b/tests/integration/reqif/SPEC-OBJECTS/02_spec_object_without_values/sample.reqif new file mode 100644 index 0000000..f1450c9 --- /dev/null +++ b/tests/integration/reqif/SPEC-OBJECTS/02_spec_object_without_values/sample.reqif @@ -0,0 +1,20 @@ + + + + + + + + TEST_SPEC_OBJECT_TYPE_IDENTIFIER_FUNCTIONAL + + + + + + TEST_SPEC_OBJECT_TYPE_IDENTIFIER_FUNCTIONAL + + + + + + diff --git a/tests/integration/reqif/SPEC-OBJECTS/02_spec_object_without_values/test.itest b/tests/integration/reqif/SPEC-OBJECTS/02_spec_object_without_values/test.itest new file mode 100644 index 0000000..aa3061a --- /dev/null +++ b/tests/integration/reqif/SPEC-OBJECTS/02_spec_object_without_values/test.itest @@ -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 diff --git a/tests/unit/reqif/parsers/test_spec_object_parser.py b/tests/unit/reqif/parsers/test_spec_object_parser.py index b73d78a..08cfbe2 100644 --- a/tests/unit/reqif/parsers/test_spec_object_parser.py +++ b/tests/unit/reqif/parsers/test_spec_object_parser.py @@ -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, ) @@ -115,3 +117,45 @@ def test_02_attributes_xhtml(): ].value == expected_xhtml ) + + +def test_unparse_spec_object_without_values_element(): + spec_object_string = """ + + + TEST_SPEC_OBJECT_TYPE + + + """ + + spec_object = SpecObjectParser.parse(etree.fromstring(spec_object_string)) + output = SpecObjectParser.unparse(spec_object) + + assert "TEST_SPEC_OBJECT_TYPE" in output + ) + + +def test_unparse_spec_object_without_values_element_after_adding_attribute(): + spec_object_string = """ + + + TEST_SPEC_OBJECT_TYPE + + + """ + 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("") < output.index("")