2026-07-24 12:22:49 +02:00
|
|
|
# SPDX-License-Identifier: BSL-1.0
|
|
|
|
|
|
2026-08-10 11:45:08 +02:00
|
|
|
# Unit tests for `prepare_command_fragment` helper in `extras/CatchAddTests.cmake`.
|
2026-07-24 12:22:49 +02:00
|
|
|
#
|
|
|
|
|
# Yes, we are at the stage where the script helpers need unit tests.
|
|
|
|
|
#
|
|
|
|
|
# Run as
|
|
|
|
|
# cmake -DCATCH_ADD_TESTS_SCRIPT=/path/to/extras/CatchAddTests.cmake \
|
2026-08-10 11:45:08 +02:00
|
|
|
# -P TestPrepareCommandFragment.cmake
|
2026-07-24 12:22:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
cmake_minimum_required(VERSION 3.19)
|
|
|
|
|
|
|
|
|
|
if(NOT DEFINED CATCH_ADD_TESTS_SCRIPT)
|
|
|
|
|
message(FATAL_ERROR "Missing argument `CATCH_ADD_TESTS_SCRIPT`")
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
if(NOT EXISTS "${CATCH_ADD_TESTS_SCRIPT}")
|
|
|
|
|
message(FATAL_ERROR "Cannot find CatchAddTests.cmake at '${CATCH_ADD_TESTS_SCRIPT}'")
|
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
# Pull in the helper functions. Without `TEST_EXECUTABLE` being defined,
|
|
|
|
|
# `catch_discover_tests_impl` is not called.
|
|
|
|
|
include("${CATCH_ADD_TESTS_SCRIPT}")
|
|
|
|
|
|
|
|
|
|
set(_failures 0)
|
|
|
|
|
|
|
|
|
|
function(expect_equal description actual expected)
|
|
|
|
|
if(actual STREQUAL expected)
|
|
|
|
|
message(" [PASS] ${description}")
|
|
|
|
|
else()
|
|
|
|
|
message(" [FAIL] ${description}")
|
|
|
|
|
message(" expected: [${expected}]")
|
|
|
|
|
message(" actual: [${actual}]")
|
|
|
|
|
math(EXPR _n "${_failures} + 1")
|
|
|
|
|
set(_failures "${_n}" PARENT_SCOPE)
|
|
|
|
|
endif()
|
|
|
|
|
endfunction()
|
|
|
|
|
|
2026-08-10 11:45:08 +02:00
|
|
|
prepare_command_fragment(test_fragment SimpleName /path/to/tests)
|
|
|
|
|
expect_equal("Simple arg, no quotes" "${test_fragment}" " SimpleName /path/to/tests")
|
2026-07-24 12:22:49 +02:00
|
|
|
|
2026-08-10 11:45:08 +02:00
|
|
|
prepare_command_fragment(test_fragment "Name with spaces")
|
|
|
|
|
expect_equal("Spaces in arg, needs quotes" "${test_fragment}" " [==[Name with spaces]==]")
|
2026-07-24 12:22:49 +02:00
|
|
|
|
2026-08-10 11:45:08 +02:00
|
|
|
prepare_command_fragment(test_fragment Foo PROPERTIES LABELS "tagA\;tagB\;tagC")
|
2026-07-24 12:22:49 +02:00
|
|
|
expect_equal("semicolons in argument are kept and quoted"
|
2026-08-10 11:45:08 +02:00
|
|
|
"${test_fragment}" " Foo PROPERTIES LABELS [==[tagA\;tagB\;tagC]==]")
|
2026-07-24 12:22:49 +02:00
|
|
|
|
2026-08-10 11:45:08 +02:00
|
|
|
set(test_fragment "PRE-EXISTING")
|
|
|
|
|
prepare_command_fragment(test_fragment Foo PROPERTIES BAR baz)
|
|
|
|
|
expect_equal("out var does no accumulate commands"
|
|
|
|
|
"${test_fragment}" " Foo PROPERTIES BAR baz")
|
2026-07-24 12:22:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(_failures GREATER 0)
|
2026-08-10 11:45:08 +02:00
|
|
|
message(FATAL_ERROR "${_failures} prepare_command_fragment test(s) failed")
|
2026-07-24 12:22:49 +02:00
|
|
|
else()
|
2026-08-10 11:45:08 +02:00
|
|
|
message(STATUS "All prepare_command_fragment tests passed")
|
2026-07-24 12:22:49 +02:00
|
|
|
endif()
|