From 438dc5582f0101ecc0e8f2bc016a82287b24ff6d Mon Sep 17 00:00:00 2001 From: noremac700 <48997927+noremac700@users.noreply.github.com> Date: Sun, 24 Mar 2024 12:48:26 +0000 Subject: [PATCH] Add .count() function to JSON response captures --- .../VariableCapture/PororocaResponseValueCapturer.cs | 8 ++++++++ .../VariableCapture/PororocaResponseValueCapturerTests.cs | 7 +++++++ 2 files changed, 15 insertions(+) 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)]