diff --git a/SysML2.NET.Tests/Extend/TextualRepresentationExtensionsTestFixture.cs b/SysML2.NET.Tests/Extend/TextualRepresentationExtensionsTestFixture.cs index 3ad1b7d9..984a8618 100644 --- a/SysML2.NET.Tests/Extend/TextualRepresentationExtensionsTestFixture.cs +++ b/SysML2.NET.Tests/Extend/TextualRepresentationExtensionsTestFixture.cs @@ -1,38 +1,55 @@ -// ------------------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // -// +// // Copyright 2022-2026 Starion Group S.A. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -// +// // // ------------------------------------------------------------------------------------------------ namespace SysML2.NET.Tests.Extend { using System; - + using NUnit.Framework; - + using SysML2.NET.Core.POCO.Root.Annotations; + using SysML2.NET.Core.POCO.Systems.DefinitionAndUsage; + using SysML2.NET.Exceptions; + using SysML2.NET.Extensions; [TestFixture] public class TextualRepresentationExtensionsTestFixture { [Test] - public void ComputeRepresentedElement_ThrowsNotSupportedException() + public void VerifyComputeRepresentedElement() { - Assert.That(() => ((ITextualRepresentation)null).ComputeRepresentedElement(), Throws.TypeOf()); + Assert.That(() => ((ITextualRepresentation)null).ComputeRepresentedElement(), + Throws.TypeOf()); + + var emptyTextualRep = new TextualRepresentation(); + + Assert.That(() => emptyTextualRep.ComputeRepresentedElement(), + Throws.TypeOf()); + + var target = new Definition(); + var textualRep = new TextualRepresentation(); + var annotation = new Annotation(); + + target.AssignOwnership(annotation, textualRep); + + Assert.That(textualRep.ComputeRepresentedElement(), Is.SameAs(target)); } } } diff --git a/SysML2.NET/Extend/TextualRepresentationExtensions.cs b/SysML2.NET/Extend/TextualRepresentationExtensions.cs index fce6d2c3..786f4121 100644 --- a/SysML2.NET/Extend/TextualRepresentationExtensions.cs +++ b/SysML2.NET/Extend/TextualRepresentationExtensions.cs @@ -1,4 +1,4 @@ -// ------------------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------- // // // Copyright (C) 2022-2026 Starion Group S.A. @@ -21,10 +21,9 @@ namespace SysML2.NET.Core.POCO.Root.Annotations { using System; - using System.Collections.Generic; using SysML2.NET.Core.POCO.Root.Elements; - using SysML2.NET.Core.POCO.Root.Namespaces; + using SysML2.NET.Exceptions; /// /// The class provides extensions methods for @@ -33,19 +32,35 @@ namespace SysML2.NET.Core.POCO.Root.Annotations internal static class TextualRepresentationExtensions { /// - /// Computes the derived property. + /// Computes the derived representedElement property of a . /// /// /// The subject /// /// - /// the computed result + /// The that owns this , + /// which is the representedElement. /// - [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + /// + /// The representedElement must be the owner of the TextualRepresentation + /// (OMG KerML spec: representedElement redefines owner, narrowing cardinality to 1..1). + /// + /// + /// Thrown when is . + /// + /// + /// Thrown when the has no owner. + /// internal static IElement ComputeRepresentedElement(this ITextualRepresentation textualRepresentationSubject) { - throw new NotSupportedException("Create a GitHub issue when this method is required"); - } + if (textualRepresentationSubject == null) + { + throw new ArgumentNullException(nameof(textualRepresentationSubject)); + } + return textualRepresentationSubject.owner + ?? throw new IncompleteModelException( + $"{nameof(textualRepresentationSubject)} must have an owner (its representedElement)"); + } } }