diff --git a/CMakeLists.txt b/CMakeLists.txt index 501679f..e46ee91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.21) +cmake_minimum_required(VERSION 3.24) project( Assertion diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index 6fcd3c5..0bcc4f0 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -118,227 +118,217 @@ endmacro() function(assert) cmake_parse_arguments(PARSE_ARGV 0 ARG "" "" "") - # This block will automatically set several policies to use the new behavior - # if it is available in the current CMake version. - block(SCOPE_FOR POLICIES) - foreach(POLICY IN ITEMS CMP0012 CMP0054 CMP0057 CMP0064 CMP0139) - if(POLICY "${POLICY}") - cmake_policy(SET "${POLICY}" NEW) - endif() - endforeach() + if(${ARG_UNPARSED_ARGUMENTS}) + # Assertion passed, exit immediately. + return() + endif() - if(${ARG_UNPARSED_ARGUMENTS}) - # Assertion passed, exit immediately. - return() + # Assertion failed, determine the fatal error message based on the given + # condition. + if(ARGC EQUAL 2) + if(NOT ARGV0 STREQUAL NOT) + if(ARGV0 STREQUAL "COMMAND") + fail("expected command" ARGV1 "to be defined") + return() + elseif(ARGV0 STREQUAL "POLICY") + fail("expected policy" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "TARGET") + fail("expected target" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "TEST") + fail("expected test" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "DEFINED") + fail("expected variable" ARGV1 "to be defined") + return() + elseif(ARGV0 STREQUAL "EXISTS") + fail("expected path" ARGV1 "to exist") + return() + elseif(ARGV0 STREQUAL "IS_READABLE") + fail("expected path" ARGV1 "to be readable") + return() + elseif(ARGV0 STREQUAL "IS_WRITABLE") + fail("expected path" ARGV1 "to be writable") + return() + elseif(ARGV0 STREQUAL "IS_EXECUTABLE") + fail("expected path" ARGV1 "to be an executable") + return() + elseif(ARGV0 STREQUAL "IS_DIRECTORY") + fail("expected path" ARGV1 "to be a directory") + return() + elseif(ARGV0 STREQUAL "IS_SYMLINK") + fail("expected path" ARGV1 "to be a symbolic link") + return() + elseif(ARGV0 STREQUAL "IS_ABSOLUTE") + fail("expected path" ARGV1 "to be absolute") + return() + endif() endif() - - # Assertion failed, determine the fatal error message based on the given - # condition. - if(ARGC EQUAL 2) - if(NOT ARGV0 STREQUAL NOT) - if(ARGV0 STREQUAL "COMMAND") - fail("expected command" ARGV1 "to be defined") - return() - elseif(ARGV0 STREQUAL "POLICY") - fail("expected policy" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "TARGET") - fail("expected target" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "TEST") - fail("expected test" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "DEFINED") - fail("expected variable" ARGV1 "to be defined") - return() - elseif(ARGV0 STREQUAL "EXISTS") - fail("expected path" ARGV1 "to exist") - return() - elseif(ARGV0 STREQUAL "IS_READABLE") - fail("expected path" ARGV1 "to be readable") - return() - elseif(ARGV0 STREQUAL "IS_WRITABLE") - fail("expected path" ARGV1 "to be writable") - return() - elseif(ARGV0 STREQUAL "IS_EXECUTABLE") - fail("expected path" ARGV1 "to be an executable") - return() - elseif(ARGV0 STREQUAL "IS_DIRECTORY") - fail("expected path" ARGV1 "to be a directory") - return() - elseif(ARGV0 STREQUAL "IS_SYMLINK") - fail("expected path" ARGV1 "to be a symbolic link") - return() - elseif(ARGV0 STREQUAL "IS_ABSOLUTE") - fail("expected path" ARGV1 "to be absolute") - return() - endif() + elseif(ARGC EQUAL 3) + if(ARGV0 STREQUAL NOT) + if(ARGV1 STREQUAL "COMMAND") + fail("expected command" ARGV2 "not to be defined") + return() + elseif(ARGV1 STREQUAL "POLICY") + fail("expected policy" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "TARGET") + fail("expected target" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "TEST") + fail("expected test" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "DEFINED") + # Unset this to prevent the value from being formatted. + unset("${ARGV2}") + + fail("expected variable" ARGV2 "not to be defined") + return() + elseif(ARGV1 STREQUAL "EXISTS") + fail("expected path" ARGV2 "not to exist") + return() + elseif(ARGV1 STREQUAL "IS_READABLE") + fail("expected path" ARGV2 "not to be readable") + return() + elseif(ARGV1 STREQUAL "IS_WRITABLE") + fail("expected path" ARGV2 "not to be writable") + return() + elseif(ARGV1 STREQUAL "IS_EXECUTABLE") + fail("expected path" ARGV2 "not to be an executable") + return() + elseif(ARGV1 STREQUAL "IS_DIRECTORY") + fail("expected path" ARGV2 "not to be a directory") + return() + elseif(ARGV1 STREQUAL "IS_SYMLINK") + fail("expected path" ARGV2 "not to be a symbolic link") + return() + elseif(ARGV1 STREQUAL "IS_ABSOLUTE") + fail("expected path" ARGV2 "not to be absolute") + return() endif() - elseif(ARGC EQUAL 3) - if(ARGV0 STREQUAL NOT) - if(ARGV1 STREQUAL "COMMAND") - fail("expected command" ARGV2 "not to be defined") - return() - elseif(ARGV1 STREQUAL "POLICY") - fail("expected policy" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "TARGET") - fail("expected target" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "TEST") - fail("expected test" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "DEFINED") - # Unset this to prevent the value from being formatted. - unset("${ARGV2}") - - fail("expected variable" ARGV2 "not to be defined") - return() - elseif(ARGV1 STREQUAL "EXISTS") - fail("expected path" ARGV2 "not to exist") - return() - elseif(ARGV1 STREQUAL "IS_READABLE") - fail("expected path" ARGV2 "not to be readable") - return() - elseif(ARGV1 STREQUAL "IS_WRITABLE") - fail("expected path" ARGV2 "not to be writable") - return() - elseif(ARGV1 STREQUAL "IS_EXECUTABLE") - fail("expected path" ARGV2 "not to be an executable") - return() - elseif(ARGV1 STREQUAL "IS_DIRECTORY") - fail("expected path" ARGV2 "not to be a directory") - return() - elseif(ARGV1 STREQUAL "IS_SYMLINK") - fail("expected path" ARGV2 "not to be a symbolic link") - return() - elseif(ARGV1 STREQUAL "IS_ABSOLUTE") - fail("expected path" ARGV2 "not to be absolute") - return() - endif() - else() - if(ARGV1 STREQUAL "IN_LIST") - fail("expected string" ARGV0 "to exist in" ARGV2) - return() - elseif(ARGV1 STREQUAL "IS_NEWER_THAN") - fail("expected file" ARGV0 "to be newer than" ARGV2) - return() - elseif(ARGV1 STREQUAL "MATCHES") - fail("expected string" ARGV0 "to match" ARGV2) - return() - elseif(ARGV1 STREQUAL "LESS") - fail("expected number" ARGV0 "to be less than" ARGV2) - return() - elseif(ARGV1 STREQUAL "GREATER") - fail("expected number" ARGV0 "to be greater than" ARGV2) - return() - elseif(ARGV1 STREQUAL "EQUAL") - fail("expected number" ARGV0 "to be equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "LESS_EQUAL") - fail("expected number" ARGV0 "to be less than or equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "GREATER_EQUAL") - fail("expected number" ARGV0 "to be greater than or equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "STRLESS") - fail("expected string" ARGV0 "to be less than" ARGV2) - return() - elseif(ARGV1 STREQUAL "STRGREATER") - fail("expected string" ARGV0 "to be greater than" ARGV2) - return() - elseif(ARGV1 STREQUAL "STREQUAL") - fail("expected string" ARGV0 "to be equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "STRLESS_EQUAL") - fail("expected string" ARGV0 "to be less than or equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "STRGREATER_EQUAL") - fail("expected string" ARGV0 "to be greater than or equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "VERSION_LESS") - fail("expected version" ARGV0 "to be less than" ARGV2) - return() - elseif(ARGV1 STREQUAL "VERSION_GREATER") - fail("expected version" ARGV0 "to be greater than" ARGV2) - return() - elseif(ARGV1 STREQUAL "VERSION_EQUAL") - fail("expected version" ARGV0 "to be equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "VERSION_LESS_EQUAL") - fail("expected version" ARGV0 "to be less than or equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "VERSION_GREATER_EQUAL") - fail("expected version" ARGV0 "to be greater than or equal to" ARGV2) - return() - elseif(ARGV1 STREQUAL "PATH_EQUAL") - fail("expected path" ARGV0 "to be equal to" ARGV2) - return() - endif() + else() + if(ARGV1 STREQUAL "IN_LIST") + fail("expected string" ARGV0 "to exist in" ARGV2) + return() + elseif(ARGV1 STREQUAL "IS_NEWER_THAN") + fail("expected file" ARGV0 "to be newer than" ARGV2) + return() + elseif(ARGV1 STREQUAL "MATCHES") + fail("expected string" ARGV0 "to match" ARGV2) + return() + elseif(ARGV1 STREQUAL "LESS") + fail("expected number" ARGV0 "to be less than" ARGV2) + return() + elseif(ARGV1 STREQUAL "GREATER") + fail("expected number" ARGV0 "to be greater than" ARGV2) + return() + elseif(ARGV1 STREQUAL "EQUAL") + fail("expected number" ARGV0 "to be equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "LESS_EQUAL") + fail("expected number" ARGV0 "to be less than or equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "GREATER_EQUAL") + fail("expected number" ARGV0 "to be greater than or equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "STRLESS") + fail("expected string" ARGV0 "to be less than" ARGV2) + return() + elseif(ARGV1 STREQUAL "STRGREATER") + fail("expected string" ARGV0 "to be greater than" ARGV2) + return() + elseif(ARGV1 STREQUAL "STREQUAL") + fail("expected string" ARGV0 "to be equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "STRLESS_EQUAL") + fail("expected string" ARGV0 "to be less than or equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "STRGREATER_EQUAL") + fail("expected string" ARGV0 "to be greater than or equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "VERSION_LESS") + fail("expected version" ARGV0 "to be less than" ARGV2) + return() + elseif(ARGV1 STREQUAL "VERSION_GREATER") + fail("expected version" ARGV0 "to be greater than" ARGV2) + return() + elseif(ARGV1 STREQUAL "VERSION_EQUAL") + fail("expected version" ARGV0 "to be equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "VERSION_LESS_EQUAL") + fail("expected version" ARGV0 "to be less than or equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "VERSION_GREATER_EQUAL") + fail("expected version" ARGV0 "to be greater than or equal to" ARGV2) + return() + elseif(ARGV1 STREQUAL "PATH_EQUAL") + fail("expected path" ARGV0 "to be equal to" ARGV2) + return() endif() - elseif(ARGC EQUAL 4) - if(ARGV0 STREQUAL "NOT") - if(ARGV2 STREQUAL "IN_LIST") - fail("expected string" ARGV1 "not to exist in" ARGV3) - return() - elseif(ARGV2 STREQUAL "IS_NEWER_THAN") - fail("expected file" ARGV1 "not to be newer than" ARGV3) - return() - elseif(ARGV2 STREQUAL "MATCHES") - fail("expected string" ARGV1 "not to match" ARGV3) - return() - elseif(ARGV2 STREQUAL "LESS") - fail("expected number" ARGV1 "not to be less than" ARGV3) - return() - elseif(ARGV2 STREQUAL "GREATER") - fail("expected number" ARGV1 "not to be greater than" ARGV3) - return() - elseif(ARGV2 STREQUAL "EQUAL") - fail("expected number" ARGV1 "not to be equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "LESS_EQUAL") - fail("expected number" ARGV1 "not to be less than or equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "GREATER_EQUAL") - fail("expected number" ARGV1 "not to be greater than or equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "STRLESS") - fail("expected string" ARGV1 "not to be less than" ARGV3) - return() - elseif(ARGV2 STREQUAL "STRGREATER") - fail("expected string" ARGV1 "not to be greater than" ARGV3) - return() - elseif(ARGV2 STREQUAL "STREQUAL") - fail("expected string" ARGV1 "not to be equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "STRLESS_EQUAL") - fail("expected string" ARGV1 "not to be less than or equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "STRGREATER_EQUAL") - fail("expected string" ARGV1 "not to be greater than or equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "VERSION_LESS") - fail("expected version" ARGV1 "not to be less than" ARGV3) - return() - elseif(ARGV2 STREQUAL "VERSION_GREATER") - fail("expected version" ARGV1 "not to be greater than" ARGV3) - return() - elseif(ARGV2 STREQUAL "VERSION_EQUAL") - fail("expected version" ARGV1 "not to be equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "VERSION_LESS_EQUAL") - fail("expected version" ARGV1 "not to be less than or equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "VERSION_GREATER_EQUAL") - fail("expected version" ARGV1 "not to be greater than or equal to" ARGV3) - return() - elseif(ARGV2 STREQUAL "PATH_EQUAL") - fail("expected path" ARGV1 "not to be equal to" ARGV3) - return() - endif() + endif() + elseif(ARGC EQUAL 4) + if(ARGV0 STREQUAL "NOT") + if(ARGV2 STREQUAL "IN_LIST") + fail("expected string" ARGV1 "not to exist in" ARGV3) + return() + elseif(ARGV2 STREQUAL "IS_NEWER_THAN") + fail("expected file" ARGV1 "not to be newer than" ARGV3) + return() + elseif(ARGV2 STREQUAL "MATCHES") + fail("expected string" ARGV1 "not to match" ARGV3) + return() + elseif(ARGV2 STREQUAL "LESS") + fail("expected number" ARGV1 "not to be less than" ARGV3) + return() + elseif(ARGV2 STREQUAL "GREATER") + fail("expected number" ARGV1 "not to be greater than" ARGV3) + return() + elseif(ARGV2 STREQUAL "EQUAL") + fail("expected number" ARGV1 "not to be equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "LESS_EQUAL") + fail("expected number" ARGV1 "not to be less than or equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "GREATER_EQUAL") + fail("expected number" ARGV1 "not to be greater than or equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "STRLESS") + fail("expected string" ARGV1 "not to be less than" ARGV3) + return() + elseif(ARGV2 STREQUAL "STRGREATER") + fail("expected string" ARGV1 "not to be greater than" ARGV3) + return() + elseif(ARGV2 STREQUAL "STREQUAL") + fail("expected string" ARGV1 "not to be equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "STRLESS_EQUAL") + fail("expected string" ARGV1 "not to be less than or equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "STRGREATER_EQUAL") + fail("expected string" ARGV1 "not to be greater than or equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "VERSION_LESS") + fail("expected version" ARGV1 "not to be less than" ARGV3) + return() + elseif(ARGV2 STREQUAL "VERSION_GREATER") + fail("expected version" ARGV1 "not to be greater than" ARGV3) + return() + elseif(ARGV2 STREQUAL "VERSION_EQUAL") + fail("expected version" ARGV1 "not to be equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "VERSION_LESS_EQUAL") + fail("expected version" ARGV1 "not to be less than or equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "VERSION_GREATER_EQUAL") + fail("expected version" ARGV1 "not to be greater than or equal to" ARGV3) + return() + elseif(ARGV2 STREQUAL "PATH_EQUAL") + fail("expected path" ARGV1 "not to be equal to" ARGV3) + return() endif() endif() - endblock() + endif() # Fallback to this message if it could not determine the fatal error message # from the given condition. diff --git a/test/add_cmake_script_test.cmake b/test/add_cmake_script_test.cmake index 228baa8..ff1819f 100644 --- a/test/add_cmake_script_test.cmake +++ b/test/add_cmake_script_test.cmake @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.24) + include(${CMAKE_CURRENT_LIST_DIR}/../cmake/Assertion.cmake RESULT_VARIABLE ASSERTION_LIST_FILE) @@ -6,7 +8,7 @@ file(WRITE project/test.cmake "message(\"all ok\")\n") section("it should create a new test") file(WRITE project/CMakeLists.txt - "cmake_minimum_required(VERSION 3.21)\n" + "cmake_minimum_required(VERSION 3.24)\n" "project(Sample LANGUAGES NONE)\n" "\n" "include(${ASSERTION_LIST_FILE})\n" @@ -22,7 +24,7 @@ endsection() section("it should create a new test " "with the file specified using an absolute path") file(WRITE project/CMakeLists.txt - "cmake_minimum_required(VERSION 3.21)\n" + "cmake_minimum_required(VERSION 3.24)\n" "project(Sample LANGUAGES NONE)\n" "\n" "include(${ASSERTION_LIST_FILE})\n" @@ -37,7 +39,7 @@ endsection() section("it should create a new test with the specified name") file(WRITE project/CMakeLists.txt - "cmake_minimum_required(VERSION 3.21)\n" + "cmake_minimum_required(VERSION 3.24)\n" "project(Sample LANGUAGES NONE)\n" "\n" "include(${ASSERTION_LIST_FILE})\n" @@ -52,7 +54,7 @@ endsection() section("it should fail to create a new test due to a non-existing file") file(WRITE project/CMakeLists.txt - "cmake_minimum_required(VERSION 3.21)\n" + "cmake_minimum_required(VERSION 3.24)\n" "project(Sample LANGUAGES NONE)\n" "\n" "include(${ASSERTION_LIST_FILE})\n" diff --git a/test/assert.cmake b/test/assert.cmake index 1867e05..c5a0117 100644 --- a/test/assert.cmake +++ b/test/assert.cmake @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.24) + include(${CMAKE_CURRENT_LIST_DIR}/../cmake/Assertion.cmake RESULT_VARIABLE ASSERTION_LIST_FILE) diff --git a/test/assert_execute_process.cmake b/test/assert_execute_process.cmake index 074e255..834ea80 100644 --- a/test/assert_execute_process.cmake +++ b/test/assert_execute_process.cmake @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.24) + include(${CMAKE_CURRENT_LIST_DIR}/../cmake/Assertion.cmake) section("process execution assertions") diff --git a/test/assert_fatal_error.cmake b/test/assert_fatal_error.cmake index 4d7b2f4..c2272c7 100644 --- a/test/assert_fatal_error.cmake +++ b/test/assert_fatal_error.cmake @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.24) + include(${CMAKE_CURRENT_LIST_DIR}/../cmake/Assertion.cmake) section("it should assert a fatal error message") diff --git a/test/fail.cmake b/test/fail.cmake index 820f8fb..6bd83c2 100644 --- a/test/fail.cmake +++ b/test/fail.cmake @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.24) + include(${CMAKE_CURRENT_LIST_DIR}/../cmake/Assertion.cmake) section("given strings")