From 5620a211d8bab71cf1b3175b998e696b9d0e314a Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Mon, 26 Aug 2024 15:18:15 -0700 Subject: [PATCH] Add some usage error tests. --- CMakeLists.txt | 1 - Dependencies.cmake | 21 ++++++++-------- test/CMakeLists.txt | 32 ++++++++++++++++++++++++ test/inadequate_arity_checking_tests.cpp | 21 ++++++++++++++++ test/misconfiguration_test.cpp | 5 ++++ test/too_few_arguments_test.cpp | 6 +++++ test/too_many_arguments_test.cpp | 11 ++++++++ 7 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 test/inadequate_arity_checking_tests.cpp create mode 100644 test/misconfiguration_test.cpp create mode 100644 test/too_few_arguments_test.cpp create mode 100644 test/too_many_arguments_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 36036d2..75ca0c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,6 @@ adobe_contract_checking_setup_options() adobe_contract_checking_global_options() include(Dependencies.cmake) -adobe_contract_checking_setup_dependencies() adobe_contract_checking_local_options() diff --git a/Dependencies.cmake b/Dependencies.cmake index c7ade8d..fae3a17 100644 --- a/Dependencies.cmake +++ b/Dependencies.cmake @@ -1,20 +1,19 @@ Include(FetchContent) - -# Done as a function so that updates to variables like -# CMAKE_CXX_FLAGS don't propagate out to other -# targets -function(adobe_contract_checking_setup_dependencies) - - # For each dependency, see if it's - # already been provided to us by a parent project - +if(BUILD_TESTING) FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/ff233bdd4cac0a0bf6e5cd45bda3406814cb2796.zip ) # For Windows: Prevent overriding the parent project's compiler/linker settings set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - FetchContent_MakeAvailable(googletest) -endfunction() + FetchContent_Declare( + icm + GIT_REPOSITORY https://github.com/iboB/icm.git + GIT_TAG v1.5.1 + ) + + FetchContent_MakeAvailable(googletest icm) + list(APPEND CMAKE_MODULE_PATH "${icm_SOURCE_DIR}") +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 925030c..98044a1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,6 +23,7 @@ endif() # ---- Dependencies ---- +include(icm_build_failure_testing) include(GoogleTest) # Adds a test called verify_ with test executable target @@ -79,6 +80,8 @@ function(handle_emscripten_death_tests test_executable) ) endfunction() +adobe_contract_checking_add_test(default_configuration_tests verbose_configuration_tests.cpp) + adobe_contract_checking_add_test(verbose_configuration_tests) target_compile_definitions(verbose_configuration_tests PRIVATE "ADOBE_CONTRACT_VIOLATION=verbose") @@ -90,3 +93,32 @@ target_compile_definitions(lightweight_configuration_tests PRIVATE adobe_contract_checking_add_test(unsafe_configuration_tests) target_compile_definitions(unsafe_configuration_tests PRIVATE "ADOBE_CONTRACT_VIOLATION=unsafe") + +adobe_contract_checking_add_test(inadequate_arity_checking_tests) + +icm_add_build_failure_test( + NAME misconfiguration + TARGET misconfiguration-test + SOURCES PARSE misconfiguration_test.cpp + FOLDER test # MSVC solution folder +) +adobe_contract_checking_apply_standard_options(misconfiguration-test) +target_link_libraries(misconfiguration-test PRIVATE adobe-contract-checking) + +icm_add_build_failure_test( + NAME too-few-arguments + TARGET too-few-arguments-test + SOURCES PARSE too_few_arguments_test.cpp + FOLDER test # MSVC solution folder +) +adobe_contract_checking_apply_standard_options(too-few-arguments-test) +target_link_libraries(too-few-arguments-test PRIVATE adobe-contract-checking) + +icm_add_build_failure_test( + NAME too-many-arguments + TARGET too-many-arguments-test + SOURCES PARSE too_many_arguments_test.cpp + FOLDER test # MSVC solution folder +) +adobe_contract_checking_apply_standard_options(too-many-arguments-test) +target_link_libraries(too-many-arguments-test PRIVATE adobe-contract-checking) diff --git a/test/inadequate_arity_checking_tests.cpp b/test/inadequate_arity_checking_tests.cpp new file mode 100644 index 0000000..06ab07f --- /dev/null +++ b/test/inadequate_arity_checking_tests.cpp @@ -0,0 +1,21 @@ +#include "adobe/contract_checks.hpp" +#include + +struct F +{ + template + void operator()(const T & /*unused*/, + const U & /*unused*/, + const V & /*unused*/, + const W & /*unused*/) const + {} +}; +const F f; + +TEST(ArityChecking, TooManyArgumentsNotAlwaysDetected) +{ + // These don't fail, so our arity detection is weak. + // https://github.com/stlab/adobe-contract-checks/issues/19 + ADOBE_PRECONDITION(true, "message", f); + ADOBE_INVARIANT(true, "message", f); +} diff --git a/test/misconfiguration_test.cpp b/test/misconfiguration_test.cpp new file mode 100644 index 0000000..26ab608 --- /dev/null +++ b/test/misconfiguration_test.cpp @@ -0,0 +1,5 @@ +#define ADOBE_CONTRACT_VIOLATION misconfigured +// CI somehow escapes quotes in static assertion strings, so we can't check for those below. + +// build error: Unknown configuration ADOBE_CONTRACT_VIOLATION=misconfigured. Valid values are +#include "adobe/contract_checks.hpp" diff --git a/test/too_few_arguments_test.cpp b/test/too_few_arguments_test.cpp new file mode 100644 index 0000000..baf97b3 --- /dev/null +++ b/test/too_few_arguments_test.cpp @@ -0,0 +1,6 @@ +#include "adobe/contract_checks.hpp" +int main() +{ + // build error:too_few_arguments_test.cpp + ADOBE_PRECONDITION(); +} diff --git a/test/too_many_arguments_test.cpp b/test/too_many_arguments_test.cpp new file mode 100644 index 0000000..117d3f0 --- /dev/null +++ b/test/too_many_arguments_test.cpp @@ -0,0 +1,11 @@ +#include "adobe/contract_checks.hpp" + +int main() +{ + // It's hard to pass too many arguments without detection... + + // build error:too_many_arguments_test.cpp + ADOBE_PRECONDITION(true, "message", 1); + + // ...but see inadequate_arity_checking_tests.cpp +}