From 2fd7d25420c1231ccea0cf178189381baf51571a Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 6 Aug 2022 14:55:11 +0200 Subject: [PATCH] Add ABI tests --- tests/abi/CMakeLists.txt | 1 + tests/abi/config/CMakeLists.txt | 22 ++++++++++++++ tests/abi/config/config.hpp | 27 +++++++++++++++++ tests/abi/config/custom.cpp | 25 ++++++++++++++++ tests/abi/config/default.cpp | 33 +++++++++++++++++++++ tests/abi/config/noversion.cpp | 32 ++++++++++++++++++++ tests/abi/include/nlohmann/json_v3_10_5.hpp | 6 ++-- 7 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 tests/abi/config/CMakeLists.txt create mode 100644 tests/abi/config/config.hpp create mode 100644 tests/abi/config/custom.cpp create mode 100644 tests/abi/config/default.cpp create mode 100644 tests/abi/config/noversion.cpp diff --git a/tests/abi/CMakeLists.txt b/tests/abi/CMakeLists.txt index c1ae5430a..ba90837cb 100644 --- a/tests/abi/CMakeLists.txt +++ b/tests/abi/CMakeLists.txt @@ -25,5 +25,6 @@ add_library(abi_compat_main STATIC main.cpp) target_link_libraries(abi_compat_main PUBLIC abi_compat_common) # add individual tests +add_subdirectory(config) add_subdirectory(diag) add_subdirectory(inline_ns) diff --git a/tests/abi/config/CMakeLists.txt b/tests/abi/config/CMakeLists.txt new file mode 100644 index 000000000..3a8367690 --- /dev/null +++ b/tests/abi/config/CMakeLists.txt @@ -0,0 +1,22 @@ +# test the different options to change the namespace + +# test default namespace +add_executable(abi_config_default default.cpp) +target_link_libraries(abi_config_default PRIVATE abi_compat_main) +add_test( + NAME test-abi_config_default + COMMAND abi_config_default ${DOCTEST_TEST_FILTER}) + +# test no version namespace +add_executable(abi_config_noversion noversion.cpp) +target_link_libraries(abi_config_noversion PRIVATE abi_compat_main) +add_test( + NAME test-abi_config_noversion + COMMAND abi_config_noversion ${DOCTEST_TEST_FILTER}) + +# test custom namespace +add_executable(abi_config_custom custom.cpp) +target_link_libraries(abi_config_custom PRIVATE abi_compat_main) +add_test( + NAME test-abi_config_custom + COMMAND abi_config_custom ${DOCTEST_TEST_FILTER}) diff --git a/tests/abi/config/config.hpp b/tests/abi/config/config.hpp new file mode 100644 index 000000000..d52228bb1 --- /dev/null +++ b/tests/abi/config/config.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "doctest.h" + +#include +#include +#include + +#define STRINGIZE_EX(x) #x +#define STRINGIZE(x) STRINGIZE_EX(x) + +template +std::string namespace_name(std::string ns, T* /*unused*/ = nullptr) // NOLINT(performance-unnecessary-value-param) +{ +#if DOCTEST_MSVC && !DOCTEST_CLANG + ns = __FUNCSIG__; +#elif !DOCTEST_CLANG + ns = __PRETTY_FUNCTION__; +#endif + std::smatch m; + + // extract the true namespace name from the function signature + CAPTURE(ns); + CHECK(std::regex_search(ns, m, std::regex("nlohmann(::[a-zA-Z0-9_]+)*::basic_json"))); + + return m.str(); +} diff --git a/tests/abi/config/custom.cpp b/tests/abi/config/custom.cpp new file mode 100644 index 000000000..98c3a9996 --- /dev/null +++ b/tests/abi/config/custom.cpp @@ -0,0 +1,25 @@ +#include "doctest_compatibility.h" + +#include "config.hpp" + +// define custom namespace +#define NLOHMANN_JSON_NAMESPACE nlohmann // this line may be omitted +#define NLOHMANN_JSON_NAMESPACE_BEGIN namespace nlohmann { +#define NLOHMANN_JSON_NAMESPACE_END } +#include + +TEST_CASE("custom namespace") +{ + // GCC 4.8 fails with regex_error +#if !DOCTEST_GCC || DOCTEST_GCC >= DOCTEST_COMPILER(4, 9, 0) + SECTION("namespace matches expectation") + { + std::string expected = "nlohmann::basic_json"; + + // fallback for Clang + std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; + + CHECK(namespace_name(ns) == expected); + } +#endif +} diff --git a/tests/abi/config/default.cpp b/tests/abi/config/default.cpp new file mode 100644 index 000000000..e50be7dcb --- /dev/null +++ b/tests/abi/config/default.cpp @@ -0,0 +1,33 @@ +#include "doctest_compatibility.h" + +#include "config.hpp" + +#include + +TEST_CASE("default namespace") +{ + // GCC 4.8 fails with regex_error +#if !DOCTEST_GCC || DOCTEST_GCC >= DOCTEST_COMPILER(4, 9, 0) + SECTION("namespace matches expectation") + { + std::string expected = "nlohmann::json_abi"; + +#if JSON_DIAGNOSTICS + expected += "_diag"; +#endif + +#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + expected += "_ldvcmp"; +#endif + + expected += "_v" STRINGIZE(NLOHMANN_JSON_VERSION_MAJOR); + expected += "_" STRINGIZE(NLOHMANN_JSON_VERSION_MINOR); + expected += "_" STRINGIZE(NLOHMANN_JSON_VERSION_PATCH) "::basic_json"; + + // fallback for Clang + std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; + + CHECK(namespace_name(ns) == expected); + } +#endif +} diff --git a/tests/abi/config/noversion.cpp b/tests/abi/config/noversion.cpp new file mode 100644 index 000000000..f8d8972e3 --- /dev/null +++ b/tests/abi/config/noversion.cpp @@ -0,0 +1,32 @@ +#include "doctest_compatibility.h" + +#include "config.hpp" + +#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 1 +#include + +TEST_CASE("default namespace without version component") +{ + // GCC 4.8 fails with regex_error +#if !DOCTEST_GCC || DOCTEST_GCC >= DOCTEST_COMPILER(4, 9, 0) + SECTION("namespace matches expectation") + { + std::string expected = "nlohmann::json_abi"; + +#if JSON_DIAGNOSTICS + expected += "_diag"; +#endif + +#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + expected += "_ldvcmp"; +#endif + + expected += "::basic_json"; + + // fallback for Clang + std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; + + CHECK(namespace_name(ns) == expected); + } +#endif +} diff --git a/tests/abi/include/nlohmann/json_v3_10_5.hpp b/tests/abi/include/nlohmann/json_v3_10_5.hpp index cb27e0581..87995556e 100644 --- a/tests/abi/include/nlohmann/json_v3_10_5.hpp +++ b/tests/abi/include/nlohmann/json_v3_10_5.hpp @@ -36,8 +36,8 @@ SOFTWARE. * file doc/README.md. * \****************************************************************************/ -#ifndef INCLUDE_NLOHMANN_JSON_HPP_ -#define INCLUDE_NLOHMANN_JSON_HPP_ +#ifndef INCLUDE_NLOHMANN_JSON_V3_10_5_HPP_ +#define INCLUDE_NLOHMANN_JSON_V3_10_5_HPP_ #define NLOHMANN_JSON_VERSION_MAJOR 3 #define NLOHMANN_JSON_VERSION_MINOR 10 @@ -22088,4 +22088,4 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std -#endif // INCLUDE_NLOHMANN_JSON_HPP_ +#endif // INCLUDE_NLOHMANN_JSON_V3_10_5_HPP_