JsonPartialMatcher - match guid and string (#972)

* JsonPartialMatcher - match guid and string (#971)

* JsonPartialMatcher - match guid and string. Add Regex with Guid test (#971)
This commit is contained in:
Tymur Nesterenko
2023-07-19 23:34:02 +03:00
committed by GitHub
parent 9c51548d2b
commit 58bfb3ea33
2 changed files with 47 additions and 0 deletions

View File

@@ -74,6 +74,13 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher
}
}
if (input != null &&
((value.Type == JTokenType.Guid && input.Type == JTokenType.String) ||
(value.Type == JTokenType.String && input.Type == JTokenType.Guid)))
{
return IsMatch(value.ToString(), input.ToString());
}
if (input == null || value.Type != input.Type)
{
return false;

View File

@@ -199,6 +199,27 @@ public class JsonPartialMatcherTests
Assert.Equal(0.0, match);
}
[Fact]
public void JsonPartialMatcher_IsMatch_GuidAsString_UsingRegex()
{
var guid = new Guid("1111238e-b775-44a9-a263-95e570135c94");
var matcher = new JsonPartialMatcher(new {
Id = 1,
Name = "^1111[a-fA-F0-9]{4}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"
}, false, false, true);
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue(guid) }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObject()
{
@@ -281,6 +302,25 @@ public class JsonPartialMatcherTests
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialMatcher_IsMatch_GuidAsString()
{
// Assign
var guid = Guid.NewGuid();
var matcher = new JsonPartialMatcher(new { Id = 1, Name = guid });
// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue(guid.ToString()) }
};
double match = matcher.IsMatch(jObject);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void JsonPartialMatcher_IsMatch_WithIgnoreCaseTrue_JObjectAsString()
{