diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index eaaede0..b0dfa30 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -126,6 +126,9 @@ function(assert) 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() @@ -151,6 +154,9 @@ function(assert) 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") fail("expected variable" ARGV2 "not to be defined") return() diff --git a/test/assert.cmake b/test/assert.cmake index 3d77c65..13b12cf 100644 --- a/test/assert.cmake +++ b/test/assert.cmake @@ -1,3 +1,34 @@ +# Asserts whether the given code of a sample project can be configured +# successfully. +# +# assert_configure_sample_project(...) +# +# This function asserts whether the given CMake `` of a sample project can +# be configured successfully. If more than one `` string is given, they +# are concatenated into a single block of code with no separator between the +# strings. +# +# It performs the assertion by first writing the given `` to a +# `CMakeLists.txt` file under the `sample-project` directory, and then +# configuring the `sample-project` directory using the CMake command. +# +# If the configuration fails, it will output a formatted fatal error message +# with information about the context of the configuration command. +function(assert_configure_sample_project FIRST_CODE) + cmake_parse_arguments(PARSE_ARGV 1 ARG "" "" "") + + file(MAKE_DIRECTORY sample-project) + file(WRITE sample-project/CMakeLists.txt + "cmake_minimum_required(VERSION 3.5)\n" + "project(SomeProject)\n" + "include(${ASSERTION_LIST_FILE})\n" + "${FIRST_CODE}" + ${ARG_UNPARSED_ARGUMENTS}) + + assert_execute_process( + "${CMAKE_COMMAND}" sample-project -B sample-project/build) +endfunction() + section("boolean condition assertions") section("it should assert boolean conditions") assert(TRUE) @@ -56,30 +87,17 @@ section("target existence condition assertions") file(MAKE_DIRECTORY project) section("it should assert target existence conditions") - file( - WRITE project/CMakeLists.txt - "cmake_minimum_required(VERSION 3.5)\n" - "project(SomeProject)\n" - "\n" + assert_configure_sample_project( "add_custom_target(some_target)\n" "\n" - "include(${ASSERTION_LIST_FILE})\n" - "\n" "assert(TARGET some_target)\n" "assert(NOT TARGET non_existing_target)\n") - assert_execute_process("${CMAKE_COMMAND}" project -B project/build) endsection() section("it should fail to assert target existence conditions") - file( - WRITE project/CMakeLists.txt - "cmake_minimum_required(VERSION 3.5)\n" - "project(SomeProject)\n" - "\n" + assert_configure_sample_project( "add_custom_target(some_target)\n" "\n" - "include(${ASSERTION_LIST_FILE})\n" - "\n" "assert_fatal_error(\n" " CALL assert TARGET non_existing_target\n" " MESSAGE \"expected target:\\n non_existing_target\\nto exist\")\n" @@ -87,7 +105,31 @@ section("target existence condition assertions") "assert_fatal_error(\n" " CALL assert NOT TARGET some_target\n" " MESSAGE \"expected target:\\n some_target\\nnot to exist\")\n") - assert_execute_process("${CMAKE_COMMAND}" project -B project/build) + endsection() +endsection() + +section("test existence condition assertions") + file(MAKE_DIRECTORY project) + + section("it should assert test existence conditions") + assert_configure_sample_project( + "add_test(NAME some_test COMMAND some_command)\n" + "\n" + "assert(TEST some_test)\n" + "assert(NOT TEST non_existing_test)\n") + endsection() + + section("it should fail to assert test existence conditions") + assert_configure_sample_project( + "add_test(NAME some_test COMMAND some_command)\n" + "\n" + "assert_fatal_error(\n" + " CALL assert TEST non_existing_test\n" + " MESSAGE \"expected test:\\n non_existing_test\\nto exist\")\n" + "\n" + "assert_fatal_error(\n" + " CALL assert NOT TEST some_test\n" + " MESSAGE \"expected test:\\n some_test\\nnot to exist\")\n") endsection() endsection()