mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-30 11:40:47 +02:00
Update JsonMatcher to support IgnoreArrayOrder (#1468)
* Update JsonMatcher to support IgnoreArrayOrder * more tests
This commit is contained in:
@@ -523,4 +523,151 @@ public class JsonMatcherTests
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_Array_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: true);
|
||||
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
"c",
|
||||
"a",
|
||||
"b"
|
||||
};
|
||||
var score = matcher.IsMatch(jArray).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_Array_WithIgnoreArrayOrderFalse_DifferentOrder_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: false);
|
||||
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
"c",
|
||||
"a",
|
||||
"b"
|
||||
};
|
||||
var score = matcher.IsMatch(jArray).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_Array_WithIgnoreArrayOrderTrue_SameOrder_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: true);
|
||||
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
"a",
|
||||
"b",
|
||||
"c"
|
||||
};
|
||||
var score = matcher.IsMatch(jArray).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_Array_WithIgnoreArrayOrderTrue_DifferentLength_ShouldNotMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new[] { "a", "b", "c" }, ignoreArrayOrder: true);
|
||||
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
"a",
|
||||
"b"
|
||||
};
|
||||
var score = matcher.IsMatch(jArray).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MatchScores.Mismatch, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_ObjectWithArray_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new { Items = new[] { "x", "y", "z" } }, ignoreArrayOrder: true);
|
||||
|
||||
// Act
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "Items", new JArray("z", "x", "y") }
|
||||
};
|
||||
var score = matcher.IsMatch(jObject).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_ArrayAsString_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher("[ \"a\", \"b\", \"c\" ]", ignoreArrayOrder: true);
|
||||
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
"c",
|
||||
"b",
|
||||
"a"
|
||||
};
|
||||
var score = matcher.IsMatch(jArray).Score;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1.0, score);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonMatcher_IsMatch_ArrayOfNumbers_WithIgnoreArrayOrderTrue_DifferentOrder_ShouldMatch()
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(new[] { 1, 2, 3 }, ignoreArrayOrder: true);
|
||||
|
||||
// Act
|
||||
var jArray = new JArray
|
||||
{
|
||||
3,
|
||||
1,
|
||||
2
|
||||
};
|
||||
var score = matcher.IsMatch(jArray).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 JsonMatcher_GetCSharpCodeArguments_ShouldIncludeAllConstructorArguments(MatchBehaviour matchBehaviour, bool ignoreCase, bool regex, bool ignoreArrayOrder)
|
||||
{
|
||||
// Assign
|
||||
var matcher = new JsonMatcher(matchBehaviour, "{ \"id\": 1 }", ignoreCase, regex, ignoreArrayOrder);
|
||||
|
||||
// Act
|
||||
var result = matcher.GetCSharpCodeArguments();
|
||||
|
||||
// Assert
|
||||
result.Should().StartWith($"new JsonMatcher(WireMock.Matchers.MatchBehaviour.{matchBehaviour},");
|
||||
result.Should().EndWith($", {ignoreCase.ToString().ToLowerInvariant()}, {regex.ToString().ToLowerInvariant()}, {ignoreArrayOrder.ToString().ToLowerInvariant()})");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user