From f3e17d3977504daf55e047a7f547349af422f6e3 Mon Sep 17 00:00:00 2001 From: "Yu-Hsiang M. Tsai" Date: Wed, 8 May 2024 15:24:27 +0200 Subject: [PATCH] fix test working dir and throw if it is not handled Co-authored-by: Tobias Ribizel --- extensions/test/config/CMakeLists.txt | 5 ++++- extensions/test/config/json_config.cpp | 12 ++++++++++++ include/ginkgo/extensions/config/json_config.hpp | 5 ++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/extensions/test/config/CMakeLists.txt b/extensions/test/config/CMakeLists.txt index fea144c176c..83103e9519f 100644 --- a/extensions/test/config/CMakeLists.txt +++ b/extensions/test/config/CMakeLists.txt @@ -1,2 +1,5 @@ ginkgo_create_test(json_config ADDITIONAL_LIBRARIES nlohmann_json::nlohmann_json) -configure_file("test.json" test.json COPYONLY) +# set the working directory to the current binary folder for file test +file(RELATIVE_PATH REL_BINARY_DIR ${PROJECT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}) +set_tests_properties("${REL_BINARY_DIR}/json_config" PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") +configure_file(test.json test.json COPYONLY) diff --git a/extensions/test/config/json_config.cpp b/extensions/test/config/json_config.cpp index b1f1e20ce84..cd73209a7fb 100644 --- a/extensions/test/config/json_config.cpp +++ b/extensions/test/config/json_config.cpp @@ -2,6 +2,9 @@ // // SPDX-License-Identifier: BSD-3-Clause +#include + + #include #include @@ -13,6 +16,15 @@ #include "core/test/utils.hpp" +TEST(JsonConfig, ThrowIfInvalid) +{ + const char json[] = R"({"test": null})"; + auto d = nlohmann::json::parse(json); + + ASSERT_THROW(gko::ext::config::parse_json(d), std::runtime_error); +} + + TEST(JsonConfig, ReadMap) { const char json[] = R"({"test": "A", "bool": true})"; diff --git a/include/ginkgo/extensions/config/json_config.hpp b/include/ginkgo/extensions/config/json_config.hpp index d4f3b34f8b0..d21a3623f46 100644 --- a/include/ginkgo/extensions/config/json_config.hpp +++ b/include/ginkgo/extensions/config/json_config.hpp @@ -7,6 +7,7 @@ #include +#include #include @@ -57,7 +58,9 @@ inline gko::config::pnode parse_json(const nlohmann::json& input) return gko::config::pnode{ std::string(data.template get())}; } - return gko::config::pnode{}; + throw std::runtime_error( + "property_tree can not handle the node with content: " + + data.dump()); }; if (dom.is_array()) {