2021-07-22 09:25:55 +02:00
|
|
|
option(JSON_Valgrind "Execute test suite with Valgrind." OFF)
|
|
|
|
option(JSON_FastTests "Skip expensive/slow tests." OFF)
|
2017-08-25 20:12:21 +02:00
|
|
|
|
2020-05-01 20:59:47 +02:00
|
|
|
# download test data
|
2020-05-02 10:56:01 +02:00
|
|
|
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/download_test_data.cmake)
|
2020-05-01 14:32:37 +02:00
|
|
|
|
2020-05-01 15:14:37 +02:00
|
|
|
# test fixture to download test data
|
2020-05-01 20:59:47 +02:00
|
|
|
add_test(NAME "download_test_data" COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target download_test_data)
|
2020-05-01 15:14:37 +02:00
|
|
|
set_tests_properties(download_test_data PROPERTIES FIXTURES_SETUP TEST_DATA)
|
2020-05-01 14:32:37 +02:00
|
|
|
|
2017-08-25 20:12:21 +02:00
|
|
|
if(JSON_Valgrind)
|
|
|
|
find_program(CMAKE_MEMORYCHECK_COMMAND valgrind)
|
2017-08-25 20:19:58 +02:00
|
|
|
message(STATUS "Executing test suite with Valgrind (${CMAKE_MEMORYCHECK_COMMAND})")
|
2018-03-29 17:19:21 +02:00
|
|
|
set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS} --error-exitcode=1 --leak-check=full")
|
2017-08-25 20:12:21 +02:00
|
|
|
separate_arguments(memcheck_command)
|
|
|
|
endif()
|
|
|
|
|
2017-08-25 18:06:22 +02:00
|
|
|
#############################################################################
|
2019-01-13 17:41:21 +01:00
|
|
|
# doctest library with the main function to speed up build
|
2017-08-25 18:06:22 +02:00
|
|
|
#############################################################################
|
|
|
|
|
2020-05-02 23:24:21 +02:00
|
|
|
add_library(doctest_main OBJECT src/unit.cpp)
|
2019-01-13 17:41:21 +01:00
|
|
|
set_target_properties(doctest_main PROPERTIES
|
2017-08-25 18:06:22 +02:00
|
|
|
COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>"
|
|
|
|
COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>"
|
2017-02-12 12:46:54 +01:00
|
|
|
)
|
2019-08-27 14:22:01 +02:00
|
|
|
if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
|
2019-03-25 21:04:37 +01:00
|
|
|
target_compile_features(doctest_main PUBLIC cxx_range_for)
|
2019-01-16 14:21:49 +01:00
|
|
|
else()
|
2019-03-25 21:04:37 +01:00
|
|
|
target_compile_features(doctest_main PUBLIC cxx_std_11)
|
2019-01-16 14:21:49 +01:00
|
|
|
endif()
|
2019-01-13 17:41:21 +01:00
|
|
|
target_include_directories(doctest_main PRIVATE "thirdparty/doctest")
|
2017-02-12 12:46:54 +01:00
|
|
|
|
2017-10-16 10:02:48 +02:00
|
|
|
# https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake
|
|
|
|
if(MSVC)
|
|
|
|
# Force to always compile with W4
|
|
|
|
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
|
|
|
|
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
|
|
|
else()
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
|
|
|
endif()
|
|
|
|
|
2017-10-18 07:53:35 +02:00
|
|
|
# Disable warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252)
|
|
|
|
# Disable warning C4996: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator <<': was declared deprecated
|
2019-11-19 11:26:35 +01:00
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4566 /wd4996")
|
2019-08-27 14:22:01 +02:00
|
|
|
|
2018-06-29 06:04:41 +02:00
|
|
|
# https://github.com/nlohmann/json/issues/1114
|
|
|
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj")
|
2017-10-16 10:02:48 +02:00
|
|
|
endif()
|
|
|
|
|
2017-08-25 18:06:22 +02:00
|
|
|
#############################################################################
|
|
|
|
# one executable for each unit test file
|
|
|
|
#############################################################################
|
2016-05-11 02:25:54 +02:00
|
|
|
|
2021-12-29 09:47:05 +01:00
|
|
|
# check if compiler supports C++17
|
|
|
|
foreach(feature ${CMAKE_CXX_COMPILE_FEATURES})
|
|
|
|
if (${feature} STREQUAL cxx_std_17)
|
|
|
|
set(compiler_supports_cpp_17 TRUE)
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
# Clang only supports C++17 starting from Clang 5.0
|
|
|
|
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
|
|
|
|
unset(compiler_supports_cpp_17)
|
|
|
|
endif()
|
|
|
|
# MSVC 2015 (14.0) does not support C++17
|
|
|
|
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.1)
|
|
|
|
unset(compiler_supports_cpp_17)
|
|
|
|
endif()
|
|
|
|
|
2021-03-24 07:15:18 +01:00
|
|
|
file(GLOB files src/unit-*.cpp)
|
2019-10-07 21:13:09 +02:00
|
|
|
|
2017-08-25 18:06:22 +02:00
|
|
|
foreach(file ${files})
|
|
|
|
get_filename_component(file_basename ${file} NAME_WE)
|
|
|
|
string(REGEX REPLACE "unit-([^$]+)" "test-\\1" testcase ${file_basename})
|
2016-05-11 02:25:54 +02:00
|
|
|
|
2019-01-13 17:41:21 +01:00
|
|
|
add_executable(${testcase} $<TARGET_OBJECTS:doctest_main> ${file})
|
2020-05-02 16:07:09 +02:00
|
|
|
target_compile_definitions(${testcase} PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
|
2018-10-01 16:34:23 +02:00
|
|
|
target_compile_options(${testcase} PRIVATE
|
|
|
|
$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>
|
|
|
|
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
|
|
|
|
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
|
|
|
|
)
|
2020-07-26 12:12:17 +02:00
|
|
|
target_include_directories(${testcase} PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map)
|
2020-05-02 23:24:21 +02:00
|
|
|
target_link_libraries(${testcase} PRIVATE ${NLOHMANN_JSON_TARGET_NAME})
|
|
|
|
|
2021-12-29 09:47:05 +01:00
|
|
|
# add a copy with C++17 compilation
|
|
|
|
if (compiler_supports_cpp_17)
|
|
|
|
file(READ ${file} FILE_CONTENT)
|
|
|
|
string(FIND "${FILE_CONTENT}" "JSON_HAS_CPP_17" CPP_17_FOUND)
|
|
|
|
if(NOT ${CPP_17_FOUND} EQUAL -1)
|
|
|
|
add_executable(${testcase}_cpp17 $<TARGET_OBJECTS:doctest_main> ${file})
|
|
|
|
target_compile_definitions(${testcase}_cpp17 PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS)
|
|
|
|
target_compile_options(${testcase}_cpp17 PRIVATE
|
|
|
|
$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>
|
|
|
|
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
|
|
|
|
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
|
|
|
|
)
|
|
|
|
target_include_directories(${testcase}_cpp17 PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map)
|
|
|
|
target_link_libraries(${testcase}_cpp17 PRIVATE ${NLOHMANN_JSON_TARGET_NAME})
|
|
|
|
target_compile_features(${testcase}_cpp17 PRIVATE cxx_std_17)
|
|
|
|
|
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0 AND NOT MINGW)
|
|
|
|
# fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90050
|
|
|
|
target_link_libraries(${testcase}_cpp17 PRIVATE stdc++fs)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if (JSON_FastTests)
|
|
|
|
add_test(NAME "${testcase}_cpp17"
|
|
|
|
COMMAND ${testcase}_cpp17 ${DOCTEST_TEST_FILTER}
|
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
|
|
|
)
|
|
|
|
else()
|
|
|
|
add_test(NAME "${testcase}_cpp17"
|
|
|
|
COMMAND ${testcase}_cpp17 ${DOCTEST_TEST_FILTER} --no-skip
|
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
set_tests_properties("${testcase}_cpp17" PROPERTIES LABELS "all" FIXTURES_REQUIRED TEST_DATA)
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2021-03-27 14:44:39 +01:00
|
|
|
if (JSON_FastTests)
|
|
|
|
add_test(NAME "${testcase}"
|
|
|
|
COMMAND ${testcase} ${DOCTEST_TEST_FILTER}
|
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
|
|
|
)
|
|
|
|
else()
|
|
|
|
add_test(NAME "${testcase}"
|
|
|
|
COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip
|
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
|
|
|
)
|
|
|
|
endif()
|
2020-05-02 16:07:09 +02:00
|
|
|
set_tests_properties("${testcase}" PROPERTIES LABELS "all" FIXTURES_REQUIRED TEST_DATA)
|
2017-08-25 20:12:21 +02:00
|
|
|
|
|
|
|
if(JSON_Valgrind)
|
|
|
|
add_test(NAME "${testcase}_valgrind"
|
2020-05-03 19:33:01 +02:00
|
|
|
COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${DOCTEST_TEST_FILTER}
|
2020-05-01 15:14:37 +02:00
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
2017-08-25 20:12:21 +02:00
|
|
|
)
|
2021-03-24 07:15:18 +01:00
|
|
|
set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind" FIXTURES_REQUIRED TEST_DATA)
|
2017-08-25 20:12:21 +02:00
|
|
|
endif()
|
2017-08-25 18:06:22 +02:00
|
|
|
endforeach()
|
2018-09-26 16:46:34 +02:00
|
|
|
|
2021-08-01 13:34:55 +02:00
|
|
|
# disable exceptions for test-disabled_exceptions
|
|
|
|
target_compile_definitions(test-disabled_exceptions PUBLIC JSON_NOEXCEPTION)
|
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
|
|
|
target_compile_options(test-disabled_exceptions PUBLIC -fno-exceptions)
|
|
|
|
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
2021-08-11 08:06:25 +02:00
|
|
|
# disabled due to https://github.com/nlohmann/json/discussions/2824
|
|
|
|
#target_compile_options(test-disabled_exceptions PUBLIC /EH)
|
|
|
|
#target_compile_definitions(test-disabled_exceptions PUBLIC _HAS_EXCEPTIONS=0)
|
2021-08-01 13:34:55 +02:00
|
|
|
endif()
|
|
|
|
|
2021-08-21 13:55:06 +02:00
|
|
|
# avoid stack overflow, see https://github.com/nlohmann/json/issues/2955
|
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
|
|
|
set_property(TARGET test-cbor APPEND_STRING PROPERTY LINK_FLAGS " /STACK:4000000")
|
|
|
|
set_property(TARGET test-msgpack APPEND_STRING PROPERTY LINK_FLAGS " /STACK:4000000")
|
|
|
|
set_property(TARGET test-ubjson APPEND_STRING PROPERTY LINK_FLAGS " /STACK:4000000")
|
|
|
|
endif()
|
|
|
|
|
2018-09-26 16:46:34 +02:00
|
|
|
#############################################################################
|
|
|
|
# Test the generated build configs
|
|
|
|
#############################################################################
|
2020-05-27 13:22:14 +02:00
|
|
|
|
2021-08-18 13:24:12 +02:00
|
|
|
# these tests depend on the generated file nlohmann_jsonConfig.cmake
|
|
|
|
if (JSON_Install)
|
|
|
|
add_subdirectory(cmake_import)
|
|
|
|
add_subdirectory(cmake_import_minver)
|
|
|
|
endif()
|
|
|
|
|
2018-09-26 16:46:34 +02:00
|
|
|
add_subdirectory(cmake_add_subdirectory)
|
2020-04-29 14:29:17 +02:00
|
|
|
add_subdirectory(cmake_fetch_content)
|
2022-03-06 15:33:05 +01:00
|
|
|
add_subdirectory(cmake_fetch_content2)
|
2020-07-17 14:26:22 +02:00
|
|
|
add_subdirectory(cmake_target_include_directories)
|