diff --git a/src/Pororoca.Domain/Features/VariableCapture/PororocaResponseValueCapturer.cs b/src/Pororoca.Domain/Features/VariableCapture/PororocaResponseValueCapturer.cs index 5580edab..88757ce1 100644 --- a/src/Pororoca.Domain/Features/VariableCapture/PororocaResponseValueCapturer.cs +++ b/src/Pororoca.Domain/Features/VariableCapture/PororocaResponseValueCapturer.cs @@ -38,6 +38,8 @@ public static partial class PororocaResponseValueCapturer foreach (string subpath in subpaths) { + if (subpath.Equals("count()", StringComparison.InvariantCultureIgnoreCase)) + return GetCount(jsonNode); if (IsArrayElementSubpath(subpath, out string? elementName, out int? index1, out int? index2)) { if (elementName?.Equals("$", StringComparison.InvariantCultureIgnoreCase) == true) @@ -104,4 +106,10 @@ private static bool IsArrayElementSubpath(string subpath, out string? elementNam return false; } } + + private static string GetCount(JsonNode node) + { + var count = node.AsArray().Count(); + return count.ToString(); + } } \ No newline at end of file diff --git a/tests/Pororoca.Domain.Tests/Features/VariableCapture/PororocaResponseValueCapturerTests.cs b/tests/Pororoca.Domain.Tests/Features/VariableCapture/PororocaResponseValueCapturerTests.cs index d6423df7..9a37cfb4 100644 --- a/tests/Pororoca.Domain.Tests/Features/VariableCapture/PororocaResponseValueCapturerTests.cs +++ b/tests/Pororoca.Domain.Tests/Features/VariableCapture/PororocaResponseValueCapturerTests.cs @@ -90,6 +90,13 @@ public static partial class PororocaResponseValueCapturerTests public static void TestJsonValueCapture(string? expectedCapture, string path, string json) => Assert.Equal(expectedCapture, CaptureJsonValue(path, json)); + [Theory] + [InlineData("2", "$.count()", testJsonArr)] + [InlineData("3", "$.myObj.myObj2.arr.count()", testJsonObj)] + [InlineData(null, "$.myObj.count()", testJsonObj)] + public static void TestJsonValueCaptureCountFunction(string? expectedCapture, string path, string json) => + Assert.Equal(expectedCapture, CaptureJsonValue(path, json)); + [Theory] [InlineData("ABC", "/a", "ABC")] [InlineData("ENG", "/SessionInfo/Language", testXmlSimpleObj)]