From 8cacca17cfb24a18e3752911841136914fadb86e Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Sun, 18 Oct 2026 12:06:39 +0000 Subject: [PATCH] test: verify every public header compiles independently Adds standalone compilation tests for all 7 public JSON headers. Also fixes an ambiguous _json operator definition in json.hpp by re-exporting nlohmann's literal. Fixes vixcpp/vix#555. --- include/vix/json/json.hpp | 5 +- tests/CMakeLists.txt | 107 ++++++++++++++++++++++++++ tests/headers/build_header_test.cpp | 6 ++ tests/headers/convert_header_test.cpp | 6 ++ tests/headers/dumps_header_test.cpp | 6 ++ tests/headers/jpath_header_test.cpp | 6 ++ tests/headers/json_header_test.cpp | 15 ++++ tests/headers/loads_header_test.cpp | 6 ++ tests/headers/simple_header_test.cpp | 6 ++ 9 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 tests/headers/build_header_test.cpp create mode 100644 tests/headers/convert_header_test.cpp create mode 100644 tests/headers/dumps_header_test.cpp create mode 100644 tests/headers/jpath_header_test.cpp create mode 100644 tests/headers/json_header_test.cpp create mode 100644 tests/headers/loads_header_test.cpp create mode 100644 tests/headers/simple_header_test.cpp diff --git a/include/vix/json/json.hpp b/include/vix/json/json.hpp index 61e41ad..18cc413 100644 --- a/include/vix/json/json.hpp +++ b/include/vix/json/json.hpp @@ -134,10 +134,7 @@ namespace vix::json namespace literals { #if defined(VIX_JSON_ENABLE_LITERALS) && (VIX_JSON_ENABLE_LITERALS == 1) - inline Json operator"" _json(const char *s, std::size_t n) - { - return Json::parse(std::string_view{s, n}); - } + using nlohmann::literals::operator""_json; #endif } // namespace literals diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b9bf660..07b30d1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,6 +12,16 @@ set(VIX_JSON_TEST_TARGETS json_simple_test ) +set(VIX_JSON_HEADER_TESTS + build + convert + dumps + jpath + json + loads + simple +) + foreach(test_name IN LISTS VIX_JSON_TEST_TARGETS) add_executable(${test_name} ${test_name}.cpp @@ -46,6 +56,103 @@ foreach(test_name IN LISTS VIX_JSON_TEST_TARGETS) ) endforeach() +foreach(header_name IN LISTS VIX_JSON_HEADER_TESTS) + set(test_name json_${header_name}_header_test) + + add_executable(${test_name} + headers/${header_name}_header_test.cpp + ) + + target_link_libraries(${test_name} + PRIVATE + vix::json + ) + + target_compile_features(${test_name} + PRIVATE + cxx_std_20 + ) + + if (TARGET vix_warnings) + target_link_libraries(${test_name} + PRIVATE + vix_warnings + ) + elseif (MSVC) + target_compile_options(${test_name} + PRIVATE + /W4 + /permissive- + ) + else() + target_compile_options(${test_name} + PRIVATE + -Wall + -Wextra + -Wpedantic + -Wconversion + -Wsign-conversion + ) + endif() + + add_test( + NAME ${test_name} + COMMAND ${test_name} + ) + + list(APPEND VIX_JSON_TEST_TARGETS ${test_name}) +endforeach() + +set(literal_test_name json_literals_header_test) + +add_executable(${literal_test_name} + headers/json_header_test.cpp +) + +target_link_libraries(${literal_test_name} + PRIVATE + vix::json +) + +target_compile_features(${literal_test_name} + PRIVATE + cxx_std_20 +) + +target_compile_definitions(${literal_test_name} + PRIVATE + VIX_JSON_ENABLE_LITERALS=1 +) + +if (TARGET vix_warnings) + target_link_libraries(${literal_test_name} + PRIVATE + vix_warnings + ) +elseif (MSVC) + target_compile_options(${literal_test_name} + PRIVATE + /W4 + /permissive- + ) +else() + target_compile_options(${literal_test_name} + PRIVATE + -Wall + -Wextra + -Wpedantic + -Wconversion + -Wsign-conversion + ) +endif() + +add_test( + NAME ${literal_test_name} + COMMAND ${literal_test_name} +) + +list(APPEND VIX_JSON_TEST_TARGETS ${literal_test_name}) + add_custom_target(vix_json_tests DEPENDS ${VIX_JSON_TEST_TARGETS} diff --git a/tests/headers/build_header_test.cpp b/tests/headers/build_header_test.cpp new file mode 100644 index 0000000..e3353c6 --- /dev/null +++ b/tests/headers/build_header_test.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/tests/headers/convert_header_test.cpp b/tests/headers/convert_header_test.cpp new file mode 100644 index 0000000..e0744a3 --- /dev/null +++ b/tests/headers/convert_header_test.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/tests/headers/dumps_header_test.cpp b/tests/headers/dumps_header_test.cpp new file mode 100644 index 0000000..98b4c4e --- /dev/null +++ b/tests/headers/dumps_header_test.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/tests/headers/jpath_header_test.cpp b/tests/headers/jpath_header_test.cpp new file mode 100644 index 0000000..b091514 --- /dev/null +++ b/tests/headers/jpath_header_test.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/tests/headers/json_header_test.cpp b/tests/headers/json_header_test.cpp new file mode 100644 index 0000000..7e8ce23 --- /dev/null +++ b/tests/headers/json_header_test.cpp @@ -0,0 +1,15 @@ +#include + +#if defined(VIX_JSON_ENABLE_LITERALS) && (VIX_JSON_ENABLE_LITERALS == 1) +using namespace vix::json::literals; +#endif + +int main() +{ +#if defined(VIX_JSON_ENABLE_LITERALS) && (VIX_JSON_ENABLE_LITERALS == 1) + const auto value = R"({"header":"json"})"_json; + return value.at("header") == "json" ? 0 : 1; +#else + return 0; +#endif +} diff --git a/tests/headers/loads_header_test.cpp b/tests/headers/loads_header_test.cpp new file mode 100644 index 0000000..2204650 --- /dev/null +++ b/tests/headers/loads_header_test.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/tests/headers/simple_header_test.cpp b/tests/headers/simple_header_test.cpp new file mode 100644 index 0000000..dd4084f --- /dev/null +++ b/tests/headers/simple_header_test.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +}