Update JSONPathMatcher.cs to cover the string path selection to a child (#993)

* Update JSONPathMatcher.cs to cover the string path selection to a child 

The .SelectToken method accept string path selection and JSONPath queries. The current code works only for the queries because the result is JObject. When the string path is selected the result is JValue and event with a valid result the code the code doesn't return valued result.
https://www.newtonsoft.com/json/help/html/SelectToken.htm

* Added unit tests

* Addressed the comments

* Addressed the comments

* Update JSONPathMatcher.cs
This commit is contained in:
DayLightDancer authored and GitHub committed 2023-08-17 17:18:32 +02:00
1 parent fd816f0952
commit 8adf34fb56
2 files changed
+132 -3

No files matched your search

@@ -200,4 +200,128 @@ public class JsonPathMatcherTests
// Assert
Check.That(match).IsEqualTo(0.0);
}
}
[Fact]
public void JsonPathMatcher_IsMatch_ArrayOneLevel()
{
// Arrange
var matcher = new JsonPathMatcher("$.arr[0].line1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": [{
""line1"": ""line1"",
}]
}"));
// Assert
Check.That(match).IsEqualTo(1.0);
}
[Fact]
public void JsonPathMatcher_IsMatch_ObjectMatch()
{
// Arrange
var matcher = new JsonPathMatcher("$.test");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": [
{
""line1"": ""line1"",
}
]
}"));
// Assert
Check.That(match).IsEqualTo(1.0);
}
[Fact]
public void JsonPathMatcher_IsMatch_DoesntMatch()
{
// Arrange
var matcher = new JsonPathMatcher("$.test3");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": [
{
""line1"": ""line1"",
}
]
}"));
// Assert
Check.That(match).IsEqualTo(0.0);
}
[Fact]
public void JsonPathMatcher_IsMatch_DoesntMatchInArray()
{
// Arrange
var matcher = new JsonPathMatcher("$arr[0].line1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": []
}"));
// Assert
Check.That(match).IsEqualTo(0.0);
}
[Fact]
public void JsonPathMatcher_IsMatch_DoesntMatchNoObjectsInArray()
{
// Arrange
var matcher = new JsonPathMatcher("$arr[2].line1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": []
}"));
// Assert
Check.That(match).IsEqualTo(0.0);
}
[Fact]
public void JsonPathMatcher_IsMatch_NestedArrays()
{
// Arrange
var matcher = new JsonPathMatcher("$.arr[0].sub[0].subline1");
// Act
double match = matcher.IsMatch(JObject.Parse(@"{
""name"": ""PathSelectorTest"",
""test"": ""test"",
""test2"": ""test2"",
""arr"": [{
""line1"": ""line1"",
""sub"":[
{
""subline1"":""subline1""
}]
}]
}"));
// Assert
Check.That(match).IsEqualTo(1.0);
}
}