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
8 changes: 7 additions & 1 deletion reqif/models/reqif_relation_group_type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Optional
from typing import List, Optional

from reqif.models.reqif_spec_object_type import SpecAttributeDefinition


class ReqIFRelationGroupType:
Expand All @@ -9,9 +11,13 @@ def __init__( # pylint: disable=too-many-arguments
last_change: Optional[str] = None,
long_name: Optional[str] = None,
is_self_closed: bool = True,
attribute_definitions: Optional[List[SpecAttributeDefinition]] = None,
):
self.identifier: str = identifier
self.description: Optional[str] = description
self.last_change: Optional[str] = last_change
self.long_name: Optional[str] = long_name
self.is_self_closed: bool = is_self_closed
self.attribute_definitions: Optional[List[SpecAttributeDefinition]] = (
attribute_definitions
)
21 changes: 18 additions & 3 deletions reqif/parsers/spec_types/relation_group_type_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from reqif.helpers.lxml import lxml_is_self_closed_tag
from reqif.models.reqif_relation_group_type import ReqIFRelationGroupType
from reqif.parsers.attribute_definition_parser import AttributeDefinitionParser


class RelationGroupTypeParser:
Expand Down Expand Up @@ -31,12 +32,17 @@ def parse(xml_spec_relation_type_xml) -> ReqIFRelationGroupType:
xml_attributes["LAST-CHANGE"] if "LAST-CHANGE" in xml_attributes else None
)

attribute_definitions = AttributeDefinitionParser.parse_attribute_definitions(
xml_spec_relation_type_xml
)

return ReqIFRelationGroupType(
is_self_closed=is_self_closed,
description=description,
identifier=identifier,
last_change=last_change,
long_name=long_name,
attribute_definitions=attribute_definitions,
)

@staticmethod
Expand All @@ -51,7 +57,16 @@ def unparse(spec_relation_type: ReqIFRelationGroupType):
output += f' LONG-NAME="{spec_relation_type.long_name}"'
if spec_relation_type.is_self_closed:
output += "/>\n"
else:
output += ">\n"
output += " </RELATION-GROUP-TYPE>\n"
return output

output += ">\n"

if spec_relation_type.attribute_definitions is not None:
output += " <SPEC-ATTRIBUTES>\n"
output += AttributeDefinitionParser.unparse_xhtml_attribute_definition(
attribute_definitions=spec_relation_type.attribute_definitions
)
output += " </SPEC-ATTRIBUTES>\n"

output += " </RELATION-GROUP-TYPE>\n"
return output
29 changes: 3 additions & 26 deletions reqif/parsers/spec_types/specification_type_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,9 @@ def unparse(spec_type: ReqIFSpecificationType) -> str:

if spec_type.spec_attributes is not None:
output += " <SPEC-ATTRIBUTES>\n"

for attribute in spec_type.spec_attributes:
output += f" <{attribute.attribute_type.get_spec_type_tag()}"
if attribute.description is not None:
output += f' DESC="{attribute.description}"'
output += f' IDENTIFIER="{attribute.identifier}"'
if attribute.editable is not None:
editable_value = "true" if attribute.editable else "false"
output += f' IS-EDITABLE="{editable_value}"'
if attribute.last_change:
output += f' LAST-CHANGE="{attribute.last_change}"'
output += f' LONG-NAME="{attribute.long_name}"'
output += ">\n"
output += " <TYPE>\n"
output += (
" "
f"<{attribute.attribute_type.get_definition_tag()}>"
f"{attribute.datatype_definition}"
f"</{attribute.attribute_type.get_definition_tag()}>"
"\n"
)
output += " </TYPE>\n"
output += " </"
output += f"{attribute.attribute_type.get_spec_type_tag()}"
output += ">\n"

output += AttributeDefinitionParser.unparse_xhtml_attribute_definition(
attribute_definitions=spec_type.spec_attributes
)
output += " </SPEC-ATTRIBUTES>\n"

output += " </SPECIFICATION-TYPE>\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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>
<DATATYPES>
<DATATYPE-DEFINITION-STRING IDENTIFIER="_dtype_String" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="String" MAX-LENGTH="1024"/>
</DATATYPES>
<SPEC-TYPES>
<RELATION-GROUP-TYPE IDENTIFIER="ID_RelationGroupType_WithAttrs" LAST-CHANGE="2012-04-07T01:51:37.112+02:00" LONG-NAME="Relation Group Type With Attributes">
<SPEC-ATTRIBUTES>
<ATTRIBUTE-DEFINITION-STRING IDENTIFIER="_attr_rgt_string" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="Comment">
<TYPE>
<DATATYPE-DEFINITION-STRING-REF>_dtype_String</DATATYPE-DEFINITION-STRING-REF>
</TYPE>
</ATTRIBUTE-DEFINITION-STRING>
</SPEC-ATTRIBUTES>
</RELATION-GROUP-TYPE>
</SPEC-TYPES>
</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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>
<DATATYPES>
<DATATYPE-DEFINITION-STRING IDENTIFIER="_dtype_String" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="String" MAX-LENGTH="1024"/>
</DATATYPES>
<SPEC-TYPES>
<SPECIFICATION-TYPE IDENTIFIER="TEST_SPECIFICATION_TYPE_ID" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="TEST_SPECIFICATION_TYPE_LONG_NAME">
<SPEC-ATTRIBUTES>
<ATTRIBUTE-DEFINITION-STRING IDENTIFIER="TEST_SPEC_ATTRIBUTE_ID" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="Title">
<DEFAULT-VALUE>
<ATTRIBUTE-VALUE-STRING THE-VALUE="Untitled"/>
</DEFAULT-VALUE>
<TYPE>
<DATATYPE-DEFINITION-STRING-REF>_dtype_String</DATATYPE-DEFINITION-STRING-REF>
</TYPE>
</ATTRIBUTE-DEFINITION-STRING>
</SPEC-ATTRIBUTES>
</SPECIFICATION-TYPE>
</SPEC-TYPES>
</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
39 changes: 39 additions & 0 deletions tests/unit/reqif/parsers/test_relation_group_type_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from lxml import etree

from reqif.models.reqif_relation_group_type import ReqIFRelationGroupType
from reqif.parsers.spec_types.relation_group_type_parser import RelationGroupTypeParser


def test_01_no_spec_attributes() -> None:
xml = etree.fromstring(
'<RELATION-GROUP-TYPE IDENTIFIER="RGT_ID" LAST-CHANGE="2012-04-07T01:51:37.112+02:00" LONG-NAME="RGT"/>'
)
result = RelationGroupTypeParser.parse(xml)
assert isinstance(result, ReqIFRelationGroupType)
assert result.identifier == "RGT_ID"
assert result.attribute_definitions is None


def test_02_spec_attributes_round_trip() -> None:
xml_string = """\
<RELATION-GROUP-TYPE IDENTIFIER="RGT_ID" LAST-CHANGE="2012-04-07T01:51:37.112+02:00" LONG-NAME="RGT With Attrs">
<SPEC-ATTRIBUTES>
<ATTRIBUTE-DEFINITION-STRING IDENTIFIER="_attr_comment" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="Comment">
<TYPE>
<DATATYPE-DEFINITION-STRING-REF>_dtype_String</DATATYPE-DEFINITION-STRING-REF>
</TYPE>
</ATTRIBUTE-DEFINITION-STRING>
</SPEC-ATTRIBUTES>
</RELATION-GROUP-TYPE>"""
xml = etree.fromstring(xml_string)
result = RelationGroupTypeParser.parse(xml)
assert isinstance(result, ReqIFRelationGroupType)
assert result.attribute_definitions is not None
assert len(result.attribute_definitions) == 1
assert result.attribute_definitions[0].identifier == "_attr_comment"
assert result.attribute_definitions[0].long_name == "Comment"

unparsed = RelationGroupTypeParser.unparse(result)
assert "SPEC-ATTRIBUTES" in unparsed
assert "_attr_comment" in unparsed
assert "Comment" in unparsed
24 changes: 24 additions & 0 deletions tests/unit/reqif/parsers/test_specification_type_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@
)


def test_02_default_value_survives_unparse() -> None:
spec_type_string = """\
<SPECIFICATION-TYPE IDENTIFIER="ST_ID" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="ST">
<SPEC-ATTRIBUTES>
<ATTRIBUTE-DEFINITION-STRING IDENTIFIER="_attr_title" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="Title">
<DEFAULT-VALUE>
<ATTRIBUTE-VALUE-STRING THE-VALUE="Untitled"/>
</DEFAULT-VALUE>
<TYPE>
<DATATYPE-DEFINITION-STRING-REF>_dtype_String</DATATYPE-DEFINITION-STRING-REF>
</TYPE>
</ATTRIBUTE-DEFINITION-STRING>
</SPEC-ATTRIBUTES>
</SPECIFICATION-TYPE>"""
xml = etree.fromstring(spec_type_string)
result = SpecificationTypeParser.parse(xml)
assert result.spec_attributes is not None
assert result.spec_attributes[0].default_value == "Untitled"

unparsed = SpecificationTypeParser.unparse(result)
assert "DEFAULT-VALUE" in unparsed
assert "Untitled" in unparsed


def test_01_nominal_case() -> None:
spec_type_string = """
<SPECIFICATION-TYPE IDENTIFIER="TEST_SPECIFICATION_TYPE_ID" LAST-CHANGE="2015-12-14T02:04:51.769+01:00" LONG-NAME="TEST_SPECIFICATION_TYPE_LONG_NAME">
Expand Down
Loading