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
75 changes: 65 additions & 10 deletions SysML2.NET.Tests/Extend/ParameterMembershipExtensionsTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,93 @@
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// <copyright file="ParameterMembershipExtensionsTestFixture.cs" company="Starion Group S.A.">
//
//
// 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.
//
//
// </copyright>
// ------------------------------------------------------------------------------------------------

namespace SysML2.NET.Tests.Extend
{
using System;

using NUnit.Framework;


using SysML2.NET.Core.Core.Types;
using SysML2.NET.Core.POCO.Core.Features;
using SysML2.NET.Core.POCO.Core.Types;
using SysML2.NET.Core.POCO.Kernel.Behaviors;
using SysML2.NET.Core.POCO.Root.Elements;
using SysML2.NET.Core.POCO.Root.Namespaces;
using SysML2.NET.Exceptions;
using SysML2.NET.Extensions;

using Type = SysML2.NET.Core.POCO.Core.Types.Type;

[TestFixture]
public class ParameterMembershipExtensionsTestFixture
{
[Test]
public void ComputeOwnedMemberParameter_ThrowsNotSupportedException()
public void VerifyComputeOwnedMemberParameter()
{
Assert.That(() => ((IParameterMembership)null).ComputeOwnedMemberParameter(), Throws.TypeOf<NotSupportedException>());
Assert.That(() => ((IParameterMembership)null).ComputeOwnedMemberParameter(), Throws.TypeOf<ArgumentNullException>());

var parameterMembership = new ParameterMembership();

Assert.That(() => parameterMembership.ComputeOwnedMemberParameter(), Throws.TypeOf<IncompleteModelException>());

var owningType = new Type();
var feature = new Feature();

owningType.AssignOwnership(parameterMembership, feature);

Assert.That(parameterMembership.ComputeOwnedMemberParameter(), Is.SameAs(feature));

// Wiring two features to verify the multiple-element guard:
// First remove the existing wiring so we can create a fresh membership with two elements.
var twoElementMembership = new ParameterMembership();
var secondFeature = new Feature();

((IContainedRelationship)twoElementMembership).OwnedRelatedElement.Add(feature);
((IContainedRelationship)twoElementMembership).OwnedRelatedElement.Add(secondFeature);

Assert.That(() => twoElementMembership.ComputeOwnedMemberParameter(), Throws.TypeOf<IncompleteModelException>());

// NOTE: wiring a non-IFeature element as the sole OwnedRelatedElement is not possible via the
// public AssignOwnership API (IParameterMembership requires an IFeature target).
// To cover the as-cast-returns-null path we directly populate OwnedRelatedElement with a
// plain Namespace (which is not an IFeature).
var nonFeatureMembership = new ParameterMembership();
var nonFeatureElement = new Namespace();

((IContainedRelationship)nonFeatureMembership).OwnedRelatedElement.Add(nonFeatureElement);

Assert.That(nonFeatureMembership.ComputeOwnedMemberParameter(), Is.Null);
}

[Test]
public void VerifyComputeParameterDirectionOperation()
{
Assert.That(() => ((IParameterMembership)null).ComputeParameterDirectionOperation(), Throws.TypeOf<ArgumentNullException>());

var parameterMembership = new ParameterMembership();
var owningType = new Type();
var feature = new Feature();

owningType.AssignOwnership(parameterMembership, feature);

Assert.That(parameterMembership.ComputeParameterDirectionOperation(), Is.EqualTo(FeatureDirectionKind.In));
}
}
}
19 changes: 15 additions & 4 deletions SysML2.NET/Extend/ParameterMembershipExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using SysML2.NET.Core.POCO.Root.Annotations;
using SysML2.NET.Core.POCO.Root.Elements;
using SysML2.NET.Core.POCO.Root.Namespaces;
using SysML2.NET.Exceptions;

/// <summary>
/// The <see cref="ParameterMembershipExtensions"/> class provides extensions methods for
Expand All @@ -46,10 +47,16 @@
/// <returns>
/// the computed result
/// </returns>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static IFeature ComputeOwnedMemberParameter(this IParameterMembership parameterMembershipSubject)
{
throw new NotSupportedException("Create a GitHub issue when this method is required");
if (parameterMembershipSubject == null)
{
throw new ArgumentNullException(nameof(parameterMembershipSubject));
}

Check warning on line 55 in SysML2.NET/Extend/ParameterMembershipExtensions.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance

See more on https://sonarcloud.io/project/issues?id=STARIONGROUP_SysML2.NET&issues=AZ4hVRw0dgDN0SW4J9Y1&open=AZ4hVRw0dgDN0SW4J9Y1&pullRequest=234

return parameterMembershipSubject.OwnedRelatedElement.Count != 1
? throw new IncompleteModelException($"{nameof(parameterMembershipSubject)} must have exactly one related element")
: parameterMembershipSubject.OwnedRelatedElement[0] as IFeature;
}

/// <summary>
Expand All @@ -67,10 +74,14 @@
/// <returns>
/// The expected <see cref="FeatureDirectionKind" />
/// </returns>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static FeatureDirectionKind ComputeParameterDirectionOperation(this IParameterMembership parameterMembershipSubject)
{
throw new NotSupportedException("Create a GitHub issue when this method is required");
if (parameterMembershipSubject == null)
{
throw new ArgumentNullException(nameof(parameterMembershipSubject));
}

Check warning on line 82 in SysML2.NET/Extend/ParameterMembershipExtensions.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance

See more on https://sonarcloud.io/project/issues?id=STARIONGROUP_SysML2.NET&issues=AZ4hVRw0dgDN0SW4J9Y0&open=AZ4hVRw0dgDN0SW4J9Y0&pullRequest=234

return FeatureDirectionKind.In;
}
}
}
Loading