diff --git a/SysML2.NET.Tests/Extend/AttributeUsageExtensionsTestFixture.cs b/SysML2.NET.Tests/Extend/AttributeUsageExtensionsTestFixture.cs index c226fe18..83c58ebe 100644 --- a/SysML2.NET.Tests/Extend/AttributeUsageExtensionsTestFixture.cs +++ b/SysML2.NET.Tests/Extend/AttributeUsageExtensionsTestFixture.cs @@ -1,44 +1,72 @@ // ------------------------------------------------------------------------------------------------- // -// +// // 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.Systems.Attributes ; + using SysML2.NET.Core.POCO.Core.Classifiers; + using SysML2.NET.Core.POCO.Core.Features; + using SysML2.NET.Core.POCO.Systems.Attributes; + using SysML2.NET.Extensions; [TestFixture] public class AttributeUsageExtensionsTestFixture { [Test] - public void ComputeAttributeDefinition_ThrowsNotSupportedException() + public void VerifyComputeAttributeDefinition() { - Assert.That(() => ((IAttributeUsage)null).ComputeAttributeDefinition(), Throws.TypeOf()); + Assert.That(() => ((IAttributeUsage)null).ComputeAttributeDefinition(), Throws.TypeOf()); + + var emptySubject = new AttributeUsage(); + + Assert.That(emptySubject.ComputeAttributeDefinition(), Has.Count.EqualTo(0)); + + var subject = new AttributeUsage(); + var attributeDefinition = new AttributeDefinition(); + var bareClassifier = new Classifier(); + + subject.AssignOwnership(new FeatureTyping { Type = attributeDefinition }); + subject.AssignOwnership(new FeatureTyping { Type = bareClassifier }); + + // Only the IDataType (AttributeDefinition) is returned; bare Classifier is excluded. + Assert.That(subject.ComputeAttributeDefinition(), Is.EquivalentTo(new[] { attributeDefinition })); } - + [Test] - public void ComputeIsReference_ThrowsNotSupportedException() + public void VerifyComputeIsReference() { - Assert.That(() => ((IAttributeUsage)null).ComputeIsReference(), Throws.TypeOf()); + Assert.That(() => ((IAttributeUsage)null).ComputeIsReference(), Throws.TypeOf()); + + var emptySubject = new AttributeUsage(); + + Assert.That(emptySubject.ComputeIsReference(), Is.True); + + var populatedSubject = new AttributeUsage(); + var attributeDefinition = new AttributeDefinition(); + populatedSubject.AssignOwnership(new FeatureTyping { Type = attributeDefinition }); + + // Always true regardless of subject state — AttributeUsages are always referential. + Assert.That(populatedSubject.ComputeIsReference(), Is.True); } } } diff --git a/SysML2.NET/Extend/AttributeUsageExtensions.cs b/SysML2.NET/Extend/AttributeUsageExtensions.cs index 78dc8528..9d52443e 100644 --- a/SysML2.NET/Extend/AttributeUsageExtensions.cs +++ b/SysML2.NET/Extend/AttributeUsageExtensions.cs @@ -22,6 +22,7 @@ namespace SysML2.NET.Core.POCO.Systems.Attributes { using System; using System.Collections.Generic; + using System.Linq; using SysML2.NET.Core.Core.Types; using SysML2.NET.Core.Root.Namespaces; @@ -63,16 +64,25 @@ internal static class AttributeUsageExtensions /// /// Computes the derived property. /// + /// + /// Walks OwnedRelationshipIFeatureTypingType directly, + /// filtering to IDataType. The AttributeUsage POCO's explicit-interface + /// IUsage.definition impl delegates to this.attributeDefinition, + /// which would route back into this method → stack overflow. Bypassing the + /// instance property mirrors the technique in + /// . + /// /// /// The subject /// /// /// the computed result /// - [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static List ComputeAttributeDefinition(this IAttributeUsage attributeUsageSubject) { - throw new NotSupportedException("Create a GitHub issue when this method is required"); + return attributeUsageSubject == null + ? throw new ArgumentNullException(nameof(attributeUsageSubject)) + : [..attributeUsageSubject.OwnedRelationship.OfType().Select(featureTyping => featureTyping.Type).OfType()]; } /// @@ -84,10 +94,11 @@ internal static List ComputeAttributeDefinition(this IAttributeUsage /// /// the computed result /// - [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static bool ComputeIsReference(this IAttributeUsage attributeUsageSubject) { - throw new NotSupportedException("Create a GitHub issue when this method is required"); + return attributeUsageSubject == null + ? throw new ArgumentNullException(nameof(attributeUsageSubject)) + : true; } }