Update JsonMatcher to support IgnoreArrayOrder (#1468)

* Update JsonMatcher to support IgnoreArrayOrder

* more tests
This commit is contained in:
Stef Heyenrath
2026-05-30 08:45:52 +02:00
committed by GitHub
parent 6912d52faf
commit e92041783c
4 changed files with 307 additions and 12 deletions
@@ -367,4 +367,118 @@ public class SystemTextJsonMatcherTests
// Assert
Assert.Equal(1.0, match);
}
}
[Fact]
public void SystemTextJsonMatcher_IsMatch_Array_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
{
// Assign
var matcher = new SystemTextJsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: true);
// Act
var jsonElement = JsonDocument.Parse("[ \"c\", \"a\", \"b\" ]").RootElement;
var score = matcher.IsMatch(jsonElement).Score;
// Assert
Assert.Equal(1.0, score);
}
[Fact]
public void SystemTextJsonMatcher_IsMatch_Array_WithIgnoreArrayOrderFalse_DifferentOrder_ShouldNotMatch()
{
// Assign
var matcher = new SystemTextJsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: false);
// Act
var jsonElement = JsonDocument.Parse("[ \"c\", \"a\", \"b\" ]").RootElement;
var score = matcher.IsMatch(jsonElement).Score;
// Assert
Assert.Equal(MatchScores.Mismatch, score);
}
[Fact]
public void SystemTextJsonMatcher_IsMatch_Array_WithIgnoreArrayOrderTrue_SameOrder_ShouldMatch()
{
// Assign
var matcher = new SystemTextJsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: true);
// Act
var jsonElement = JsonDocument.Parse("[ \"a\", \"b\", \"c\" ]").RootElement;
var score = matcher.IsMatch(jsonElement).Score;
// Assert
Assert.Equal(1.0, score);
}
[Fact]
public void SystemTextJsonMatcher_IsMatch_Array_WithIgnoreArrayOrderTrue_DifferentLength_ShouldNotMatch()
{
// Assign
var matcher = new SystemTextJsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: true);
// Act
var jsonElement = JsonDocument.Parse("[ \"a\", \"b\" ]").RootElement;
var score = matcher.IsMatch(jsonElement).Score;
// Assert
Assert.Equal(MatchScores.Mismatch, score);
}
[Fact]
public void SystemTextJsonMatcher_IsMatch_ObjectWithArray_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
{
// Assign
var matcher = new SystemTextJsonMatcher(new { Items = new[] { "x", "y", "z" } }, ignoreArrayOrder: true);
// Act
var match = matcher.IsMatch("{ \"Items\" : [ \"z\", \"x\", \"y\" ] }").Score;
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void SystemTextJsonMatcher_IsMatch_ArrayAsString_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
{
// Assign
var matcher = new SystemTextJsonMatcher("[ \"a\", \"b\", \"c\" ]", ignoreArrayOrder: true);
// Act
var jsonElement = JsonDocument.Parse("[ \"c\", \"b\", \"a\" ]").RootElement;
var score = matcher.IsMatch(jsonElement).Score;
// Assert
Assert.Equal(1.0, score);
}
[Fact]
public void SystemTextJsonMatcher_IsMatch_ArrayOfNumbers_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
{
// Assign
var matcher = new SystemTextJsonMatcher(new[] { 1, 2, 3 }, ignoreArrayOrder: true);
// Act
var jsonElement = JsonDocument.Parse("[ 3, 1, 2 ]").RootElement;
var score = matcher.IsMatch(jsonElement).Score;
// Assert
Assert.Equal(1.0, score);
}
[Theory]
[InlineData(MatchBehaviour.AcceptOnMatch, false, false, false)]
[InlineData(MatchBehaviour.AcceptOnMatch, true, false, true)]
[InlineData(MatchBehaviour.RejectOnMatch, true, true, false)]
public void SystemTextJsonMatcher_GetCSharpCodeArguments_ShouldIncludeAllConstructorArguments(MatchBehaviour matchBehaviour, bool ignoreCase, bool regex, bool ignoreArrayOrder)
{
// Assign
var matcher = new SystemTextJsonMatcher(matchBehaviour, "{ \"id\": 1 }", ignoreCase, regex, ignoreArrayOrder);
// Act
var result = matcher.GetCSharpCodeArguments();
// Assert
result.Should().StartWith($"new SystemTextJsonMatcher(WireMock.Matchers.MatchBehaviour.{matchBehaviour},");
result.Should().EndWith($", {ignoreCase.ToString().ToLowerInvariant()}, {regex.ToString().ToLowerInvariant()}, {ignoreArrayOrder.ToString().ToLowerInvariant()})");
}
}