From 295ec9eb815d5bdfdfffba08e322876c8a1c4973 Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Thu, 17 Apr 2025 13:24:30 +0200 Subject: [PATCH] Add more JSON tests --- tests/test-json.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/test-json.cpp b/tests/test-json.cpp index 183f7e26..0f7e11ca 100644 --- a/tests/test-json.cpp +++ b/tests/test-json.cpp @@ -52,7 +52,7 @@ TEST_CASE("MatchJson", "[json-helpers]") REQUIRE(result == false); } -TEST_CASE("GetJsonField", "[utility]") +TEST_CASE("GetJsonField", "[json-helpers]") { auto result = advss::GetJsonField("{}", ""); REQUIRE_FALSE(result); @@ -65,4 +65,41 @@ TEST_CASE("GetJsonField", "[utility]") result = advss::GetJsonField("{ \"test\": 1 }", "test"); REQUIRE(*result == "1"); -} \ No newline at end of file +} + +TEST_CASE("QueryJson", "[json-helpers]") +{ + auto result = advss::QueryJson("{}", ""); + REQUIRE_FALSE(result); + + result = advss::QueryJson("invalid json", "$.test"); + REQUIRE_FALSE(result); + + result = advss::QueryJson("{}", "invalid query"); + REQUIRE_FALSE(result); + + result = advss::QueryJson("{}", "$.test"); + REQUIRE(*result == "[]"); + + result = advss::QueryJson("{ \"test\": { \"something\" : 1 } }", + "$.test.something"); + REQUIRE(*result == "[1]"); +} + +TEST_CASE("AccessJsonArrayIndex", "[json-helpers]") +{ + auto result = advss::AccessJsonArrayIndex("{}", 0); + REQUIRE_FALSE(result); + + result = advss::AccessJsonArrayIndex("invalid json", 0); + REQUIRE_FALSE(result); + + result = advss::AccessJsonArrayIndex("[]", 0); + REQUIRE_FALSE(result); + + result = advss::AccessJsonArrayIndex("[\"1\", \"2\"]", -1); + REQUIRE_FALSE(result); + + result = advss::AccessJsonArrayIndex("[\"1\", \"2\"]", 1); + REQUIRE(*result == "2"); +}