Skip to content

Introduced ComparerBehaviorConfig.CompareCompatibleClassifier #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 24, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres _loosely_ to [Semantic Versioning](https://semver.org/s
* Introduced `LanguageDeserializerBuilder`.
* Introduced delegating implementations for `SerializerHandler`, `DeserializerHandler`, `LanguageDeserializerHandler`.
* Added async variant `JsonUtils.WriteNodesToStreamAsync()`.
* Introduced `ComparerBehaviorConfig.CompareCompatibleClassifier` to configure whether
instances of C# `Annotation` and `Concept` should be considered different.
### Fixed
* `LenientNode` now works properly if keys of features change.
* Deserializer can now create instances of languages not registered beforehand.
Expand Down
27 changes: 23 additions & 4 deletions src/LionWeb.Core/Core/Utilities/Comparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,12 @@ protected virtual List<IDifference> CompareClassifier(IReadableNode left, IReada
case (Annotation leftAnn, Annotation rightAnn):
result.AddRange(CompareAnnotation(left, leftAnn, right, rightAnn));
break;
case (Annotation, Concept) or (Concept, Annotation):
case (Annotation, Concept) or (Concept, Annotation) when BehaviorConfig.CompareCompatibleClassifier:
result.Add(new IncompatibleClassifierDifference(left, right));
break;
case var (leftClassifier, rightClassifier):
result.AddRange(CompareClassifier(left, leftClassifier, right, rightClassifier));
break;
}

return result;
Expand Down Expand Up @@ -625,22 +628,38 @@ protected virtual List<IDifference> CompareAnnotation(IReadableNode left, Annota
return result;
}

/// Compares <paramref name="leftClassifier"/> and <paramref name="rightClassifier"/>.
protected virtual List<IDifference> CompareClassifier(IReadableNode left, Classifier leftClassifier, IReadableNode right,
Classifier rightClassifier)
{
List<IDifference> result = [];

if (!leftClassifier.EqualsIdentity(rightClassifier))
{
result.Add(new ClassifierDifference(left, right));
}

return result;
}

/// Compares <paramref name="a"/> and <paramref name="b"/> from the same side.
protected virtual bool AreSameSideNodesEqual(IReadableNode? a, IReadableNode? b) =>
// ReSharper disable once PossibleUnintendedReferenceComparison
a == b;
}

/// <summary>
/// Configures the behavior of <see cref="System.Collections.Comparer"/>.
/// Configures the behavior of <see cref="Comparer"/>.
/// </summary>
public class ComparerBehaviorConfig
{
// nothing here yet
/// Whether we should compare classifier compatibility; defaults to <c>true</c>.
/// <seealso cref="IncompatibleClassifierDifference"/>
public bool CompareCompatibleClassifier { get; init; } = true;
}

/// <summary>
/// Configures the output of <see cref="System.Collections.Comparer"/>.
/// Configures the output of <see cref="Comparer"/>.
/// </summary>
public class ComparerOutputConfig
{
Expand Down
38 changes: 38 additions & 0 deletions test/LionWeb.Core.Test/Utilities/Comparer/ComparerNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace LionWeb.Core.Test.Utilities.Comparer;

using Core.Utilities;
using Languages.Generated.V2024_1.Shapes.M2;
using M2;

[TestClass]
Expand Down Expand Up @@ -72,6 +73,43 @@ public void Annotation_Concept()
);
}

[TestMethod]
public void Annotation_Concept_Ignored_Same()
{
var left = lF.NewDocumentation("b");
var right = new LenientNode("r", ShapesLanguage.Instance.Documentation);

Comparer comparer = new Comparer([(IReadableNode?)left], [right])
{
BehaviorConfig = new()
{
CompareCompatibleClassifier = false
}
};
Assert.IsTrue(comparer.AreEqual(), comparer.ToMessage(OutputConfig));
}

[TestMethod]
public void Annotation_Concept_Ignored_Different()
{
var left = lF.NewDocumentation("b");
var right = rF.NewLine("a");

Comparer comparer = new Comparer([(IReadableNode?)left], [right])
{
BehaviorConfig = new()
{
CompareCompatibleClassifier = false
}
};

var actual = comparer.Compare().Distinct().ToList();
var parent = new NodeDifference(left, right);
List<IDifference> expected = [parent, new ClassifierDifference(left, right) { Parent = parent } ];

CollectionAssert.AreEqual(expected, actual, comparer.ToMessage(OutputConfig));
}

[TestMethod]
public void Single_Null_Same()
{
Expand Down
Loading