Add ABI tests

This commit is contained in:
Florian Albrechtskirchinger 2022-08-06 14:55:11 +02:00
parent a752d1c35d
commit 2fd7d25420
No known key found for this signature in database
GPG Key ID: 19618CE9B2D4BE6D
7 changed files with 143 additions and 3 deletions

View File

@ -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)

View File

@ -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})

View File

@ -0,0 +1,27 @@
#pragma once
#include "doctest.h"
#include <iostream>
#include <regex>
#include <string>
#define STRINGIZE_EX(x) #x
#define STRINGIZE(x) STRINGIZE_EX(x)
template<typename T>
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();
}

View File

@ -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 <nlohmann/json_fwd.hpp>
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<nlohmann::json>(ns) == expected);
}
#endif
}

View File

@ -0,0 +1,33 @@
#include "doctest_compatibility.h"
#include "config.hpp"
#include <nlohmann/json_fwd.hpp>
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<nlohmann::json>(ns) == expected);
}
#endif
}

View File

@ -0,0 +1,32 @@
#include "doctest_compatibility.h"
#include "config.hpp"
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 1
#include <nlohmann/json_fwd.hpp>
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<nlohmann::json>(ns) == expected);
}
#endif
}

View File

@ -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_