Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Formatting Test Existence Assertion Errors #175

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
74 changes: 58 additions & 16 deletions test/assert.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# Asserts whether the given code of a sample project can be configured
# successfully.
#
# assert_configure_sample_project(<code>...)
#
# This function asserts whether the given CMake `<code>` of a sample project can
# be configured successfully. If more than one `<code>` 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 `<code>` 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)
Expand Down Expand Up @@ -56,38 +87,49 @@ 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"
"\n"
"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()

Expand Down