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
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// <copyright file="TextualRepresentationExtensionsTestFixture.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.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<NotSupportedException>());
Assert.That(() => ((ITextualRepresentation)null).ComputeRepresentedElement(),
Throws.TypeOf<ArgumentNullException>());

var emptyTextualRep = new TextualRepresentation();

Assert.That(() => emptyTextualRep.ComputeRepresentedElement(),
Throws.TypeOf<IncompleteModelException>());

var target = new Definition();
var textualRep = new TextualRepresentation();
var annotation = new Annotation();

target.AssignOwnership(annotation, textualRep);

Assert.That(textualRep.ComputeRepresentedElement(), Is.SameAs(target));
}
}
}
31 changes: 23 additions & 8 deletions SysML2.NET/Extend/TextualRepresentationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// <copyright file="TextualRepresentationExtensions.cs" company="Starion Group S.A.">
//
// Copyright (C) 2022-2026 Starion Group S.A.
Expand All @@ -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;

/// <summary>
/// The <see cref="TextualRepresentationExtensions"/> class provides extensions methods for
Expand All @@ -33,19 +32,35 @@
internal static class TextualRepresentationExtensions
{
/// <summary>
/// Computes the derived property.
/// Computes the derived <c>representedElement</c> property of a <see cref="ITextualRepresentation"/>.
/// </summary>
/// <param name="textualRepresentationSubject">
/// The subject <see cref="ITextualRepresentation"/>
/// </param>
/// <returns>
/// the computed result
/// The <see cref="IElement"/> that owns this <see cref="ITextualRepresentation"/>,
/// which is the <c>representedElement</c>.
/// </returns>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
/// <remarks>
/// The representedElement must be the owner of the TextualRepresentation
/// (OMG KerML spec: representedElement redefines owner, narrowing cardinality to 1..1).
/// </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="textualRepresentationSubject"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="IncompleteModelException">
/// Thrown when the <see cref="ITextualRepresentation"/> has no owner.
/// </exception>
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));
}

Check warning on line 59 in SysML2.NET/Extend/TextualRepresentationExtensions.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=AZ46MwcB8EaOukkhU92q&open=AZ46MwcB8EaOukkhU92q&pullRequest=241

return textualRepresentationSubject.owner
?? throw new IncompleteModelException(
$"{nameof(textualRepresentationSubject)} must have an owner (its representedElement)");
}
}
}
Loading