diff --git a/SysML2.NET.Tests/Extend/ElementExtensionsTestFixture.cs b/SysML2.NET.Tests/Extend/ElementExtensionsTestFixture.cs index 00951b68..632cb764 100644 --- a/SysML2.NET.Tests/Extend/ElementExtensionsTestFixture.cs +++ b/SysML2.NET.Tests/Extend/ElementExtensionsTestFixture.cs @@ -70,7 +70,7 @@ public void VerifyComputeIsLibraryElement() using (Assert.EnterMultipleScope()) { Assert.That(element.ComputeIsLibraryElement, Is.False); - Assert.That(documentation.ComputeIsLibraryElement, Throws.TypeOf()); + Assert.That(documentation.ComputeIsLibraryElement, Is.False); } } @@ -289,7 +289,17 @@ public void VerifyComputePathOperation() using (Assert.EnterMultipleScope()) { Assert.That(element.ComputePathOperation, Is.EqualTo("name")); - Assert.That(secondElement.ComputePathOperation, Throws.TypeOf()); + Assert.That(secondElement.ComputePathOperation, Is.EqualTo("/2/1")); + } + + namespaceElement.DeclaredName = "namespace"; + membership.DeclaredName = "firstMember"; + secondMembership.DeclaredName = "secondMember"; + + using (Assert.EnterMultipleScope()) + { + Assert.That(element.ComputePathOperation, Is.EqualTo("name")); + Assert.That(secondElement.ComputePathOperation, Is.EqualTo("/2/1")); } } diff --git a/SysML2.NET.Tests/Extend/RelationshipExtensionsTestFixture.cs b/SysML2.NET.Tests/Extend/RelationshipExtensionsTestFixture.cs index 9ce60a32..591aaf8b 100644 --- a/SysML2.NET.Tests/Extend/RelationshipExtensionsTestFixture.cs +++ b/SysML2.NET.Tests/Extend/RelationshipExtensionsTestFixture.cs @@ -21,18 +21,61 @@ namespace SysML2.NET.Tests.Extend { using System; - + + using Moq; + using NUnit.Framework; using SysML2.NET.Core.POCO.Root.Elements; + using SysML2.NET.Core.POCO.Root.Namespaces; + using SysML2.NET.Core.POCO.Systems.DefinitionAndUsage; [TestFixture] public class RelationshipExtensionsTestFixture { [Test] - public void ComputeRelatedElement_ThrowsNotSupportedException() + public void VerfiyComputeRelatedElement() + { + Assert.That(() => ((IRelationship)null).ComputeRelatedElement(), Throws.TypeOf()); + + var relationship = new Mock(); + relationship.Setup(x => x.Source).Returns([]); + relationship.Setup(x => x.Target).Returns([]); + + Assert.That(relationship.Object.ComputeRelatedElement(), Is.Empty); + + var sourceDefinition = new Definition(); + relationship.Setup(x => x.Source).Returns([sourceDefinition]); + + Assert.That(relationship.Object.ComputeRelatedElement(), Is.EquivalentTo([sourceDefinition])); + + var targetDefinition = new Definition(); + relationship.Setup(x => x.Target).Returns([targetDefinition]); + + Assert.That(relationship.Object.ComputeRelatedElement(), Is.EquivalentTo([sourceDefinition, targetDefinition])); + var secondSource = new Definition(); + + relationship.Setup(x => x.Source).Returns([sourceDefinition, secondSource]); + Assert.That(relationship.Object.ComputeRelatedElement(), Is.EquivalentTo([sourceDefinition, secondSource, targetDefinition])); + } + + [Test] + public void VerifyComputeRedefinedLibraryNamespaceOperation() + { + Assert.That(() => ((IRelationship)null).ComputeRedefinedLibraryNamespaceOperation(), Throws.TypeOf()); + } + + [Test] + public void VerifyComputeRedefinedPathOperation() { - Assert.That(() => ((IRelationship)null).ComputeRelatedElement(), Throws.TypeOf()); + Assert.That(() => ((IRelationship)null).ComputeRedefinedPathOperation(), Throws.TypeOf()); + + var membership = new OwningMembership() + { + DeclaredName = "name" + }; + + Assert.That(((IRelationship)membership).ComputeRedefinedPathOperation(), Is.Empty); } } } diff --git a/SysML2.NET.Tests/SysML2.NET.Tests.csproj b/SysML2.NET.Tests/SysML2.NET.Tests.csproj index 06cc1658..4d3b47a1 100644 --- a/SysML2.NET.Tests/SysML2.NET.Tests.csproj +++ b/SysML2.NET.Tests/SysML2.NET.Tests.csproj @@ -20,6 +20,7 @@ + diff --git a/SysML2.NET/Extend/RelationshipExtensions.cs b/SysML2.NET/Extend/RelationshipExtensions.cs index a331b8a2..36a804f7 100644 --- a/SysML2.NET/Extend/RelationshipExtensions.cs +++ b/SysML2.NET/Extend/RelationshipExtensions.cs @@ -22,6 +22,7 @@ namespace SysML2.NET.Core.POCO.Root.Elements { using System; using System.Collections.Generic; + using System.Linq; using SysML2.NET.Core.POCO.Root.Annotations; using SysML2.NET.Core.POCO.Root.Namespaces; @@ -41,10 +42,9 @@ internal static class RelationshipExtensions /// /// the computed result /// - [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static List ComputeRelatedElement(this IRelationship relationshipSubject) { - throw new NotSupportedException("Create a GitHub issue when this method is required"); + return relationshipSubject == null ? throw new ArgumentNullException(nameof(relationshipSubject)) : [..relationshipSubject.Source, ..relationshipSubject.Target]; } /// @@ -57,10 +57,14 @@ internal static List ComputeRelatedElement(this IRelationship relation /// /// The expected /// - [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static INamespace ComputeRedefinedLibraryNamespaceOperation(this IRelationship relationshipSubject) { - throw new NotSupportedException("Create a GitHub issue when this method is required"); + if (relationshipSubject == null) + { + throw new ArgumentNullException(nameof(relationshipSubject)); + } + + return relationshipSubject.OwningRelatedElement?.LibraryNamespace() ?? relationshipSubject.OwningRelationship?.LibraryNamespace(); } /// @@ -75,10 +79,20 @@ internal static INamespace ComputeRedefinedLibraryNamespaceOperation(this IRelat /// /// The expected /// - [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static string ComputeRedefinedPathOperation(this IRelationship relationshipSubject) { - throw new NotSupportedException("Create a GitHub issue when this method is required"); + if (relationshipSubject == null) + { + throw new ArgumentNullException(nameof(relationshipSubject)); + } + + if (relationshipSubject.OwningRelationship == null && relationshipSubject.OwningRelatedElement != null) + { + var index = ((IContainedElement)relationshipSubject.OwningRelatedElement).OwnedRelationship.IndexOf(relationshipSubject) + 1; + return $"{relationshipSubject.OwningRelatedElement.Path()}/{index}"; + } + + return relationshipSubject.ComputePathOperation(); } } }