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
131 changes: 131 additions & 0 deletions SysML2.NET.CodeGenerator.Tests/Extensions/ErratumRecordsTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="ErratumRecordsTestFixture.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.CodeGenerator.Tests.Extensions
{
using System;

using NUnit.Framework;

using SysML2.NET.CodeGenerator.Extensions;

/// <summary>
/// Covers the records that carry a correction or invariant, all of which refuse to be constructed
/// without a justification — the property that keeps the tables auditable.
/// </summary>
[TestFixture]
public class ErratumRecordsTestFixture
{
[Test]
public void VerifyOclErratum()
{
using (Assert.EnterMultipleScope())
{
Assert.That(() => new OclErratum(null, "replacement", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new OclErratum(" ", "replacement", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new OclErratum("original", null, "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new OclErratum("original", " ", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new OclErratum("original", "replacement", null), Throws.TypeOf<ArgumentException>());
Assert.That(() => new OclErratum("original", "replacement", " "), Throws.TypeOf<ArgumentException>());
}

var erratum = new OclErratum("original", "replacement", "justification");

using (Assert.EnterMultipleScope())
{
Assert.That(erratum.Original, Is.EqualTo("original"));
Assert.That(erratum.Replacement, Is.EqualTo("replacement"));
Assert.That(erratum.Justification, Is.EqualTo("justification"));
}
}

[Test]
public void VerifyGrammarErratum()
{
using (Assert.EnterMultipleScope())
{
Assert.That(() => new GrammarErratum(null, "target", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarErratum(" ", "target", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarErratum("rule", null, "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarErratum("rule", " ", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarErratum("rule", "target", null), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarErratum("rule", "target", " "), Throws.TypeOf<ArgumentException>());
}

var erratum = new GrammarErratum("rule", "target", "justification");

using (Assert.EnterMultipleScope())
{
Assert.That(erratum.RuleName, Is.EqualTo("rule"));
Assert.That(erratum.TargetElementName, Is.EqualTo("target"));
Assert.That(erratum.Justification, Is.EqualTo("justification"));
}
}

[Test]
public void VerifyGrammarProductionErratum()
{
using (Assert.EnterMultipleScope())
{
Assert.That(() => new GrammarProductionErratum(null, "original", "replacement", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarProductionErratum(" ", "original", "replacement", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarProductionErratum("rule", null, "replacement", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarProductionErratum("rule", " ", "replacement", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarProductionErratum("rule", "original", null, "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarProductionErratum("rule", "original", " ", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarProductionErratum("rule", "original", "replacement", null), Throws.TypeOf<ArgumentException>());
Assert.That(() => new GrammarProductionErratum("rule", "original", "replacement", " "), Throws.TypeOf<ArgumentException>());
}

var erratum = new GrammarProductionErratum("rule", "original", "replacement", "justification");

using (Assert.EnterMultipleScope())
{
Assert.That(erratum.RuleName, Is.EqualTo("rule"));
Assert.That(erratum.Original, Is.EqualTo("original"));
Assert.That(erratum.Replacement, Is.EqualTo("replacement"));
Assert.That(erratum.Justification, Is.EqualTo("justification"));
}
}

[Test]
public void VerifyNotationInvariant()
{
using (Assert.EnterMultipleScope())
{
Assert.That(() => new NotationInvariant(null, "metamodelName", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new NotationInvariant(" ", "metamodelName", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new NotationInvariant("name", null, "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new NotationInvariant("name", " ", "justification"), Throws.TypeOf<ArgumentException>());
Assert.That(() => new NotationInvariant("name", "metamodelName", null), Throws.TypeOf<ArgumentException>());
Assert.That(() => new NotationInvariant("name", "metamodelName", " "), Throws.TypeOf<ArgumentException>());
}

var invariant = new NotationInvariant("name", "metamodelName", "justification");

using (Assert.EnterMultipleScope())
{
Assert.That(invariant.Name, Is.EqualTo("name"));
Assert.That(invariant.MetamodelName, Is.EqualTo("metamodelName"));
Assert.That(invariant.Justification, Is.EqualTo("justification"));
}
}
}
}
108 changes: 108 additions & 0 deletions SysML2.NET.CodeGenerator.Tests/Extensions/GrammarErrataTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="GrammarErrataTestFixture.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.CodeGenerator.Tests.Extensions
{
using System.Linq;

using NUnit.Framework;

using SysML2.NET.CodeGenerator.Extensions;

[TestFixture]
public class GrammarErrataTestFixture
{
/// <summary>
/// The production the <c>CaseBodyItem</c> erratum corrects, quoted exactly as the grammar carries it.
/// </summary>
private const string CaseBodyItemOriginal = "CaseBodyItem : Type =\r\n ActionBodyItem";

[Test]
public void VerifyApplyProductions()
{
using (Assert.EnterMultipleScope())
{
Assert.That(GrammarErrata.ApplyProductions(null), Is.Null);
Assert.That(GrammarErrata.ApplyProductions(string.Empty), Is.Empty);
Assert.That(GrammarErrata.ApplyProductions(" "), Is.EqualTo(" "));
}

// A grammar carrying none of the corrected productions is returned untouched.
const string unrelated = "Foo : Bar =\r\n Baz";

Assert.That(GrammarErrata.ApplyProductions(unrelated), Is.EqualTo(unrelated));

var corrected = GrammarErrata.ApplyProductions(CaseBodyItemOriginal);

using (Assert.EnterMultipleScope())
{
Assert.That(corrected, Is.Not.EqualTo(CaseBodyItemOriginal));
Assert.That(corrected, Does.Contain("CalculationBodyItem"));
Assert.That(corrected, Does.Not.Contain(" ActionBodyItem"));

// Re-applying a correction to already-corrected text is a no-op: the Original stops matching.
Assert.That(GrammarErrata.ApplyProductions(corrected), Is.EqualTo(corrected));
}

// The correction is applied verbatim wherever it appears, leaving surrounding text intact.
var embedded = GrammarErrata.ApplyProductions($"// leading\r\n{CaseBodyItemOriginal}\r\n// trailing");

using (Assert.EnterMultipleScope())
{
Assert.That(embedded, Does.StartWith("// leading"));
Assert.That(embedded, Does.EndWith("// trailing"));
Assert.That(embedded, Does.Contain("CalculationBodyItem"));
}
}

[Test]
public void VerifyQueryUnappliedErrata()
{
// ApplyProductions above marks the CaseBodyItem entry applied, so whatever remains must be
// reportable: every stale entry has to carry the rule name and the reason it was recorded.
var unapplied = GrammarErrata.QueryUnappliedErrata();

using (Assert.EnterMultipleScope())
{
Assert.That(unapplied, Is.Not.Null);
Assert.That(unapplied.All(erratum => !string.IsNullOrWhiteSpace(erratum.RuleName)), Is.True);
Assert.That(unapplied.All(erratum => !string.IsNullOrWhiteSpace(erratum.Justification)), Is.True);
}
}

[Test]
public void VerifyApplyTarget()
{
using (Assert.EnterMultipleScope())
{
// A target the grammar states itself always wins — an erratum only fills a gap.
Assert.That(GrammarErrata.ApplyTarget("LiteralReal", "AlreadyStated"), Is.EqualTo("AlreadyStated"));

// A rule with no erratum keeps its (absent) target.
Assert.That(GrammarErrata.ApplyTarget("NoSuchRule", null), Is.Null);
Assert.That(GrammarErrata.ApplyTarget(null, null), Is.Null);
Assert.That(GrammarErrata.ApplyTarget(" ", null), Is.Null);

// The KEBNF names this rule LiteralReal, but the metaclass is LiteralRational.
Assert.That(GrammarErrata.ApplyTarget("LiteralReal", null), Is.EqualTo("LiteralRational"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="NotationInvariantsTestFixture.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.CodeGenerator.Tests.Extensions
{
using System.Linq;

using NUnit.Framework;

using SysML2.NET.CodeGenerator.Extensions;

[TestFixture]
public class NotationInvariantsTestFixture
{
[Test]
public void VerifyQueryMetamodelName()
{
using (Assert.EnterMultipleScope())
{
Assert.That(NotationInvariants.QueryMetamodelName(NotationInvariants.ResultMemberMetaclass), Is.EqualTo("ReturnParameterMembership"));
Assert.That(NotationInvariants.QueryMetamodelName(NotationInvariants.ImpliedDirectionProperty), Is.EqualTo("direction"));
Assert.That(NotationInvariants.QueryMetamodelName("NoSuchInvariant"), Is.Null);
Assert.That(NotationInvariants.QueryMetamodelName(null), Is.Null);
}
}

[Test]
public void VerifyQueryMetaclass()
{
// Without a cache source there is nothing to resolve against, and an unknown key has no name to
// resolve — neither may throw, because both simply disable the rule the invariant backs.
using (Assert.EnterMultipleScope())
{
Assert.That(NotationInvariants.QueryMetaclass(NotationInvariants.ResultMemberMetaclass, null), Is.Null);
Assert.That(NotationInvariants.QueryMetaclass("NoSuchInvariant", null), Is.Null);
Assert.That(NotationInvariants.QueryMetaclass(null, null), Is.Null);
}
}

[Test]
public void VerifyQueryUnresolvedInvariants()
{
NotationInvariants.MarkResolved(NotationInvariants.ImpliedDirectionProperty);

// Marking must be tolerant: a blank key records nothing rather than corrupting the set.
NotationInvariants.MarkResolved(null);
NotationInvariants.MarkResolved(" ");

var unresolved = NotationInvariants.QueryUnresolvedInvariants();

using (Assert.EnterMultipleScope())
{
Assert.That(unresolved, Is.Not.Null);
Assert.That(unresolved.Any(invariant => invariant.Name == NotationInvariants.ImpliedDirectionProperty), Is.False);

// An unresolved entry has to be actionable: it names the anchor that went missing and why it
// mattered, because the rule it backs is silently off until it is re-anchored.
Assert.That(unresolved.All(invariant => !string.IsNullOrWhiteSpace(invariant.MetamodelName)), Is.True);
Assert.That(unresolved.All(invariant => !string.IsNullOrWhiteSpace(invariant.Justification)), Is.True);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Remove="Extensions\**" />
<EmbeddedResource Remove="Extensions\**" />
<None Remove="Extensions\**" />
<Compile Include="Extensions\*TestFixture.cs" />
<Compile Remove="Expected\UML\Core\AutoGenExtensions\ElementExtensions.cs" />
<Compile Remove="Expected\UML\Core\AutoGenReaders\AnnotatingElementReader.cs" />
<None Include="Expected\UML\Core\AutoGenExtensions\ElementExtensions.cs">
Expand Down
Loading
Loading