From 5d6668bd28f774970d15f1c0c62e22d0cdcc3f66 Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Mon, 23 Oct 2023 17:09:27 -0400 Subject: [PATCH] jsonpath jsonpath_expression --- test/CMakeLists.txt | 1 + .../src/jsonpath_make_expression_tests.cpp | 111 ++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/jsonpath/src/jsonpath_make_expression_tests.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 82ca4534c0..a47599bae5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -89,6 +89,7 @@ add_executable(unit_tests jsonpath/src/jsonpath_flatten_tests.cpp jsonpath/src/jsonpath_custom_function_tests.cpp jsonpath/src/jsonpath_json_query_tests.cpp + jsonpath/src/jsonpath_make_expression_tests.cpp jsonpath/src/jsonpath_json_replace_tests.cpp jsonpath/src/jsonpath_test_suite.cpp jsonpath/src/jsonpath_stateful_allocator_tests.cpp diff --git a/test/jsonpath/src/jsonpath_make_expression_tests.cpp b/test/jsonpath/src/jsonpath_make_expression_tests.cpp new file mode 100644 index 0000000000..2fe020b749 --- /dev/null +++ b/test/jsonpath/src/jsonpath_make_expression_tests.cpp @@ -0,0 +1,111 @@ +// Copyright 2013-2023 Daniel Parker +// Distributed under Boost license + +#if defined(_MSC_VER) +#include "windows.h" // test no inadvertant macro expansions +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // std::unordered_set +#include + +using namespace jsoncons; + +TEST_CASE("jsonpath make_expression test") +{ + std::string input = R"( + { + "books": + [ + { + "category": "fiction", + "title" : "A Wild Sheep Chase", + "author" : "Haruki Murakami", + "price" : 22.72 + }, + { + "category": "fiction", + "title" : "The Night Watch", + "author" : "Sergei Lukyanenko", + "price" : 23.58 + }, + { + "category": "fiction", + "title" : "The Comedians", + "author" : "Graham Greene", + "price" : 21.99 + }, + { + "category": "memoir", + "title" : "The Night Watch", + "author" : "Phillips, David Atlee" + } + ] + } + )"; + + SECTION("test 1") + { + int count = 0; + + const json doc = json::parse(input); + + auto expr = jsoncons::jsonpath::make_expression_for_update("$.books[*]"); + + auto callback = [&](const std::string& /*path*/, const json& book) + { + if (book.at("category") == "memoir" && !book.contains("price")) + { + ++count; + } + }; + + expr.evaluate(doc, callback); + + CHECK(count == 1); + CHECK_FALSE(doc["books"][3].contains("price")); + } + + SECTION("test 2") + { + int count = 0; + + json doc = json::parse(input); + + auto expr = jsoncons::jsonpath::make_expression_for_update("$.books[*]"); + + auto callback1 = [&](const std::string& /*path*/, const json& book) + { + if (book.at("category") == "memoir" && !book.contains("price")) + { + ++count; + } + }; + + auto callback2 = [](const std::string& /*path*/, json& book) + { + if (book.at("category") == "memoir" && !book.contains("price")) + { + book.try_emplace("price", 140.0); + } + }; + + expr.evaluate(doc, callback1); + + CHECK(count == 1); + + CHECK_FALSE(doc["books"][3].contains("price")); + expr.evaluate_and_update(doc, callback2); + CHECK(doc["books"][3].contains("price")); + CHECK(doc["books"][3].at("price") == 140); + } +} +