Skip to content

Commit

Permalink
feat: add support for formatting test existence assertion errors (#175)
Browse files Browse the repository at this point in the history
* feat: add support to format test existence assertion errors

* test: add `assert_configure_sample_project` helper function
  • Loading branch information
threeal authored Jul 7, 2024
1 parent dcd8ceb commit 57a60e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
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

0 comments on commit 57a60e5

Please sign in to comment.