|
1 | | -// ------------------------------------------------------------------------------------------------- |
| 1 | +// ------------------------------------------------------------------------------------------------- |
2 | 2 | // <copyright file="MetadataFeatureExtensionsTestFixture.cs" company="Starion Group S.A."> |
3 | | -// |
| 3 | +// |
4 | 4 | // Copyright 2022-2026 Starion Group S.A. |
5 | | -// |
| 5 | +// |
6 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | // you may not use this file except in compliance with the License. |
8 | 8 | // You may obtain a copy of the License at |
9 | | -// |
| 9 | +// |
10 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
11 | | -// |
| 11 | +// |
12 | 12 | // Unless required by applicable law or agreed to in writing, software |
13 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
14 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | 15 | // See the License for the specific language governing permissions and |
16 | 16 | // limitations under the License. |
17 | | -// |
| 17 | +// |
18 | 18 | // </copyright> |
19 | 19 | // ------------------------------------------------------------------------------------------------ |
20 | 20 |
|
21 | 21 | namespace SysML2.NET.Tests.Extend |
22 | 22 | { |
23 | 23 | using System; |
24 | | - |
| 24 | + |
25 | 25 | using NUnit.Framework; |
26 | | - |
| 26 | + |
| 27 | + using SysML2.NET.Core.POCO.Core.Features; |
| 28 | + using SysML2.NET.Core.POCO.Core.Types; |
| 29 | + using SysML2.NET.Core.POCO.Kernel.FeatureValues; |
| 30 | + using SysML2.NET.Core.POCO.Kernel.Functions; |
27 | 31 | using SysML2.NET.Core.POCO.Kernel.Metadata; |
| 32 | + using SysML2.NET.Extensions; |
28 | 33 |
|
29 | 34 | [TestFixture] |
30 | 35 | public class MetadataFeatureExtensionsTestFixture |
31 | 36 | { |
32 | 37 | [Test] |
33 | | - public void ComputeMetaclass_ThrowsNotSupportedException() |
| 38 | + public void VerifyComputeMetaclass() |
| 39 | + { |
| 40 | + Assert.That(() => ((IMetadataFeature)null).ComputeMetaclass(), Throws.TypeOf<ArgumentNullException>()); |
| 41 | + |
| 42 | + var metadataFeature = new MetadataFeature(); |
| 43 | + |
| 44 | + // Empty: no FeatureTyping → null. |
| 45 | + Assert.That(metadataFeature.ComputeMetaclass(), Is.Null); |
| 46 | + |
| 47 | + // Negative: FeatureTyping pointing at a non-Metaclass Type → null. |
| 48 | + var nonMetaclassType = new FeatureTyping { Type = new Feature() }; |
| 49 | + metadataFeature.AssignOwnership(nonMetaclassType); |
| 50 | + |
| 51 | + Assert.That(metadataFeature.ComputeMetaclass(), Is.Null); |
| 52 | + |
| 53 | + // Positive: FeatureTyping pointing at a Metaclass → returned. |
| 54 | + var metaclass1 = new Metaclass(); |
| 55 | + var typingToMetaclass1 = new FeatureTyping { Type = metaclass1 }; |
| 56 | + metadataFeature.AssignOwnership(typingToMetaclass1); |
| 57 | + |
| 58 | + Assert.That(metadataFeature.ComputeMetaclass(), Is.SameAs(metaclass1)); |
| 59 | + |
| 60 | + // Multiple Metaclass typings → first returned (insertion order). |
| 61 | + var metaclass2 = new Metaclass(); |
| 62 | + var typingToMetaclass2 = new FeatureTyping { Type = metaclass2 }; |
| 63 | + metadataFeature.AssignOwnership(typingToMetaclass2); |
| 64 | + |
| 65 | + Assert.That(metadataFeature.ComputeMetaclass(), Is.SameAs(metaclass1)); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void VerifyComputeEvaluateFeatureOperation() |
| 70 | + { |
| 71 | + // Null guard on subject. |
| 72 | + Assert.That(() => ((IMetadataFeature)null).ComputeEvaluateFeatureOperation(new Feature()), Throws.TypeOf<ArgumentNullException>()); |
| 73 | + |
| 74 | + var metadataFeature = new MetadataFeature(); |
| 75 | + var baseFeature = new Feature(); |
| 76 | + |
| 77 | + // Null baseFeature: throws ArgumentNullException (matching subject null-guard convention). |
| 78 | + Assert.That(() => metadataFeature.ComputeEvaluateFeatureOperation(null), Throws.TypeOf<ArgumentNullException>()); |
| 79 | + |
| 80 | + // Empty feature list: MetadataFeature with no feature members → []. |
| 81 | + Assert.That(metadataFeature.ComputeEvaluateFeatureOperation(baseFeature), Is.Empty); |
| 82 | + |
| 83 | + // Negative discrimination: feature present whose redefinition closure does NOT include baseFeature → []. |
| 84 | + var unrelatedFeature = new Feature(); |
| 85 | + var unrelatedMembership = new FeatureMembership(); |
| 86 | + metadataFeature.AssignOwnership(unrelatedMembership, unrelatedFeature); |
| 87 | + |
| 88 | + Assert.That(metadataFeature.ComputeEvaluateFeatureOperation(baseFeature), Is.Empty); |
| 89 | + |
| 90 | + // Direct match: candidate IS baseFeature (closure includes start node). |
| 91 | + // No FeatureValue owned by the closure → returns []. |
| 92 | + var metadataFeature2 = new MetadataFeature(); |
| 93 | + var directMembership = new FeatureMembership(); |
| 94 | + metadataFeature2.AssignOwnership(directMembership, baseFeature); |
| 95 | + |
| 96 | + Assert.That(metadataFeature2.ComputeEvaluateFeatureOperation(baseFeature), Is.Empty); |
| 97 | + |
| 98 | + // Transitive match: candidate redefines baseFeature via a one-step chain. |
| 99 | + // No FeatureValue → still returns []. |
| 100 | + var metadataFeature3 = new MetadataFeature(); |
| 101 | + var transitiveCandidate = new Feature(); |
| 102 | + var transitiveRedefinition = new Redefinition { RedefinedFeature = baseFeature }; |
| 103 | + transitiveCandidate.AssignOwnership(transitiveRedefinition); |
| 104 | + |
| 105 | + var transitiveMembership = new FeatureMembership(); |
| 106 | + metadataFeature3.AssignOwnership(transitiveMembership, transitiveCandidate); |
| 107 | + |
| 108 | + Assert.That(metadataFeature3.ComputeEvaluateFeatureOperation(baseFeature), Is.Empty); |
| 109 | + |
| 110 | + // Cycle test: featureA.ownedRedefinition → featureB, featureB.ownedRedefinition → featureA. |
| 111 | + // Closure helper must terminate; if the cycle does not include baseFeature → []. |
| 112 | + var metadataFeature4 = new MetadataFeature(); |
| 113 | + var cycleFeatureA = new Feature(); |
| 114 | + var cycleFeatureB = new Feature(); |
| 115 | + |
| 116 | + var redefinitionAtoB = new Redefinition { RedefinedFeature = cycleFeatureB }; |
| 117 | + cycleFeatureA.AssignOwnership(redefinitionAtoB); |
| 118 | + |
| 119 | + var redefinitionBtoA = new Redefinition { RedefinedFeature = cycleFeatureA }; |
| 120 | + cycleFeatureB.AssignOwnership(redefinitionBtoA); |
| 121 | + |
| 122 | + var cycleMembership = new FeatureMembership(); |
| 123 | + metadataFeature4.AssignOwnership(cycleMembership, cycleFeatureA); |
| 124 | + |
| 125 | + // Must not throw or infinite-loop; cycle does not include baseFeature → terminates with []. |
| 126 | + Assert.That(() => metadataFeature4.ComputeEvaluateFeatureOperation(baseFeature), Throws.Nothing); |
| 127 | + Assert.That(metadataFeature4.ComputeEvaluateFeatureOperation(baseFeature), Is.Empty); |
| 128 | + |
| 129 | + // Matched candidate with a FeatureValue whose value is a non-null Expression that has |
| 130 | + // NO ResultExpressionMembership wired. Expression.Evaluate returns an empty list, so |
| 131 | + // the operation returns the same empty list. (The stub-blocker case where Evaluate |
| 132 | + // throws NotSupportedException via ResultExpressionMembershipExtensions.ComputeOwnedResultExpression |
| 133 | + // cannot be wired here without modifying the sibling stub — scope discipline.) |
| 134 | + var metadataFeature5 = new MetadataFeature(); |
| 135 | + var matchingFeature = new Feature(); |
| 136 | + var redefinitionToBase = new Redefinition { RedefinedFeature = baseFeature }; |
| 137 | + matchingFeature.AssignOwnership(redefinitionToBase); |
| 138 | + |
| 139 | + var featureValue = new FeatureValue(); |
| 140 | + var valueExpression = new Expression(); |
| 141 | + matchingFeature.AssignOwnership(featureValue, valueExpression); |
| 142 | + |
| 143 | + var matchingMembership = new FeatureMembership(); |
| 144 | + metadataFeature5.AssignOwnership(matchingMembership, matchingFeature); |
| 145 | + |
| 146 | + Assert.That(metadataFeature5.ComputeEvaluateFeatureOperation(baseFeature), Is.Empty); |
| 147 | + } |
| 148 | + |
| 149 | + [Test] |
| 150 | + public void VerifyComputeIsSemanticOperation() |
| 151 | + { |
| 152 | + // Null guard. |
| 153 | + Assert.That(() => ((IMetadataFeature)null).ComputeIsSemanticOperation(), Throws.TypeOf<ArgumentNullException>()); |
| 154 | + |
| 155 | + var metadataFeature = new MetadataFeature(); |
| 156 | + |
| 157 | + // Empty: no library specialization context → SpecializesFromLibrary("Metaobjects::SemanticMetadata") |
| 158 | + // resolves to null → returns false. |
| 159 | + Assert.That(metadataFeature.ComputeIsSemanticOperation(), Is.False); |
| 160 | + |
| 161 | + // Discrimination: having a supertype that is a different library element still returns false. |
| 162 | + // Wire a non-SemanticMetadata OwningMembership-contained type so the specialization chain is |
| 163 | + // present but does not match the "Metaobjects::SemanticMetadata" qualified name. |
| 164 | + var metadataFeature2 = new MetadataFeature(); |
| 165 | + var unrelatedSupertype = new Feature(); |
| 166 | + var subsetting = new Subsetting { SubsettedFeature = unrelatedSupertype }; |
| 167 | + metadataFeature2.AssignOwnership(subsetting); |
| 168 | + |
| 169 | + Assert.That(metadataFeature2.ComputeIsSemanticOperation(), Is.False); |
| 170 | + |
| 171 | + // NOTE: Wiring a true positive (returns true) requires constructing a "Metaobjects::SemanticMetadata" |
| 172 | + // library namespace reachable via ResolveGlobal, which is infrastructure not available in unit tests. |
| 173 | + // Integration-level coverage is required for the true-positive path. |
| 174 | + } |
| 175 | + |
| 176 | + [Test] |
| 177 | + public void VerifyComputeIsSyntacticOperation() |
| 178 | + { |
| 179 | + // Null guard. |
| 180 | + Assert.That(() => ((IMetadataFeature)null).ComputeIsSyntacticOperation(), Throws.TypeOf<ArgumentNullException>()); |
| 181 | + |
| 182 | + var metadataFeature = new MetadataFeature(); |
| 183 | + |
| 184 | + // Empty: no library specialization context → SpecializesFromLibrary("KerML::Element") |
| 185 | + // resolves to null → returns false. |
| 186 | + Assert.That(metadataFeature.ComputeIsSyntacticOperation(), Is.False); |
| 187 | + |
| 188 | + // Discrimination: having a supertype that is a different library element still returns false. |
| 189 | + var metadataFeature2 = new MetadataFeature(); |
| 190 | + var unrelatedSupertype = new Feature(); |
| 191 | + var subsetting = new Subsetting { SubsettedFeature = unrelatedSupertype }; |
| 192 | + metadataFeature2.AssignOwnership(subsetting); |
| 193 | + |
| 194 | + Assert.That(metadataFeature2.ComputeIsSyntacticOperation(), Is.False); |
| 195 | + |
| 196 | + // NOTE: Wiring a true positive (returns true) requires constructing a "KerML::Element" library |
| 197 | + // namespace reachable via ResolveGlobal, which is infrastructure not available in unit tests. |
| 198 | + // Integration-level coverage is required for the true-positive path. |
| 199 | + } |
| 200 | + |
| 201 | + [Test] |
| 202 | + public void VerifyComputeSyntaxElementOperation_ThrowsNotSupportedException() |
34 | 203 | { |
35 | | - Assert.That(() => ((IMetadataFeature)null).ComputeMetaclass(), Throws.TypeOf<NotSupportedException>()); |
| 204 | + // Pending MOF reflective metaclass registry. Follow-up issue required. |
| 205 | + // The KerML spec defines this operation with body "No OCL"; computing syntaxElement requires |
| 206 | + // an inverse map from a runtime MetadataFeature back to the Element it reflects — infrastructure |
| 207 | + // that does not yet exist in this SDK. |
| 208 | + Assert.That(() => new MetadataFeature().ComputeSyntaxElementOperation(), Throws.TypeOf<NotSupportedException>()); |
36 | 209 | } |
37 | 210 | } |
38 | 211 | } |
0 commit comments