Skip to content

Commit

Permalink
add test for nodelist function
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Nov 30, 2024
1 parent fe7a068 commit 47c45eb
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/JsonPath.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,52 @@ public void OrderOfOperations(string pathString, string expectedString)
var path = JsonPath.Parse(pathString, new PathParsingOptions { AllowMathOperations = true });
var expected = JsonNode.Parse(expectedString);

var actual = path.Evaluate(target).Matches!.Select(x => x.Value).ToJsonArray();
var actual = path.Evaluate(target).Matches.Select(x => x.Value).ToJsonArray();

JsonAssert.AreEquivalent(expected, actual);
}

public class NoOpFunction : NodelistFunctionDefinition
{
public override string Name => "noop";

public NodeList? Evaluate(NodeList? nodeList)
{
return nodeList;
}

}

[Test]
public void ExpressionWithNoOpFunctionWorks()
{
FunctionRepository.RegisterNodelistFunction<NoOpFunction>();

// should do the same thing as $[?@]
var path = JsonPath.Parse("$[?noop(@)]");
var data = JsonNode.Parse(
"""
[
{"foo": 9},
{"foo": 18, "bar": false},
{},
42,
["yes", "no", 0],
true,
null
]
""");

var expected = JsonNode.Parse(
"""
[
{"foo": 9},
{"foo": 18, "bar": false},
["yes", "no", 0]
]
""");

var actual = path.Evaluate(data).Matches.Select(x => x.Value).ToJsonArray();

JsonAssert.AreEquivalent(expected, actual);
}
Expand Down

0 comments on commit 47c45eb

Please sign in to comment.