Skip to content

Commit

Permalink
Add some usage error tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrahams committed Aug 26, 2024
1 parent 6a70e37 commit 5620a21
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 12 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
21 changes: 10 additions & 11 deletions Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ endif()

# ---- Dependencies ----

include(icm_build_failure_testing)
include(GoogleTest)

# Adds a test called verify_<base_name> with test executable target
Expand Down Expand Up @@ -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")
Expand All @@ -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)
21 changes: 21 additions & 0 deletions test/inadequate_arity_checking_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "adobe/contract_checks.hpp"
#include <gtest/gtest.h>

struct F
{
template<class T, class U, class V, class W>
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);
}
5 changes: 5 additions & 0 deletions test/misconfiguration_test.cpp
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions test/too_few_arguments_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "adobe/contract_checks.hpp"
int main()
{
// build error:too_few_arguments_test.cpp
ADOBE_PRECONDITION();
}
11 changes: 11 additions & 0 deletions test/too_many_arguments_test.cpp
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 5620a21

Please sign in to comment.