GoogleTest: Add LIST_SEPARATOR for properties

This commit is contained in:
Máté Ferenc Nagy-Egri 2026-01-04 17:03:29 +01:00
parent 2e340d735f
commit a469fea3a3
6 changed files with 65 additions and 15 deletions

View File

@ -0,0 +1,5 @@
googletest-listseparator
------------------------
* The :module:`GoogleTest` module :command:`gtest_discover_tests` command
gained a `LIST_SEPARATOR` option.

View File

@ -174,6 +174,7 @@ same as the Google Test name (i.e. ``suite.testcase``); see also
[TEST_SUFFIX suffix]
[TEST_FILTER expr]
[NO_PRETTY_TYPES] [NO_PRETTY_VALUES]
[LIST_SEPARATOR sep]
[PROPERTIES name1 value1...]
[TEST_LIST var]
[DISCOVERY_TIMEOUT seconds]
@ -328,6 +329,14 @@ same as the Google Test name (i.e. ``suite.testcase``); see also
:prop_tgt:`CROSSCOMPILING_EMULATOR` target properties are preserved,
see policy :policy:`CMP0178`.
``LIST_SEPARATOR``
.. versionadded:: 4.3
By default, ``PROPERTIES`` key-value pairs cannot have lists as their
values. Providing a non-semicolon character delimiter, it is possible to
pass lists as property values. Do note that doing so the chosen character
cannot appear in any of the property values.
#]=======================================================================]
# Save project's policies
@ -562,6 +571,7 @@ function(gtest_discover_tests target)
DISCOVERY_TIMEOUT
XML_OUTPUT_DIR
DISCOVERY_MODE
LIST_SEPARATOR
)
set(multiValueArgs
EXTRA_ARGS
@ -699,6 +709,7 @@ function(gtest_discover_tests target)
-D "TEST_FILTER=${arg_TEST_FILTER}"
-D "NO_PRETTY_TYPES=${arg_NO_PRETTY_TYPES}"
-D "NO_PRETTY_VALUES=${arg_NO_PRETTY_VALUES}"
-D "LIST_SEPARATOR=${arg_LIST_SEPARATOR}"
-D "TEST_LIST=${arg_TEST_LIST}"
-D "CTEST_FILE=${ctest_tests_file}"
-D "TEST_DISCOVERY_TIMEOUT=${arg_DISCOVERY_TIMEOUT}"

View File

@ -52,7 +52,7 @@ macro(write_test_to_file)
# Handle disabled tests
set(maybe_DISABLED "")
if(pretty_test_suite MATCHES "^DISABLED_" OR pretty_test_name MATCHES "^DISABLED_")
set(maybe_DISABLED DISABLED YES)
set(maybe_DISABLED "DISABLED YES")
string(REGEX REPLACE "^DISABLED_" "" pretty_test_suite "${pretty_test_suite}")
string(REGEX REPLACE "^DISABLED_" "" pretty_test_name "${pretty_test_name}")
endif()
@ -98,6 +98,10 @@ macro(write_test_to_file)
# Add to script. Do not use add_command() here because it messes up the
# handling of empty values when forwarding arguments, and we need to
# preserve those carefully for arg_TEST_EXECUTOR and arg_EXTRA_ARGS.
# Test properties with values that are lists are also not handled
# correctly because the properties need to be expressed as a list
# of key-value pairs, but that list is flattened, so any values
# that are lists can't be differentiated from the next key.
string(APPEND script "add_test(${guarded_testname} ${launcherArgs}")
foreach(arg IN ITEMS
"${arg_TEST_EXECUTABLE}"
@ -121,18 +125,26 @@ macro(write_test_to_file)
set(maybe_LOCATION "")
if(NOT current_test_file STREQUAL "" AND NOT current_test_line STREQUAL "")
set(maybe_LOCATION DEF_SOURCE_LINE "${current_test_file}:${current_test_line}")
set(maybe_LOCATION "DEF_SOURCE_LINE [==[${current_test_file}:${current_test_line}]==]")
endif()
add_command(set_tests_properties
"${guarded_testname}"
PROPERTIES
${maybe_DISABLED}
${maybe_LOCATION}
WORKING_DIRECTORY "${arg_TEST_WORKING_DIR}"
SKIP_REGULAR_EXPRESSION "\\[ SKIPPED \\]"
${arg_TEST_PROPERTIES}
)
string(APPEND script "set_tests_properties(${guarded_testname} PROPERTIES ${maybe_DISABLED} ${maybe_LOCATION} WORKING_DIRECTORY [==[${arg_TEST_WORKING_DIR}]==] SKIP_REGULAR_EXPRESSION [==[\\[ SKIPPED \\]]==]")
if(arg_TEST_PROPERTIES)
# Making local copy of arg_TEST_PROPERTIES, as write_test_to_file is
# called in a loop and we don't want to destroy the parsed arg value
# by POP_FRONT-ing from it.
set(test_properties "${arg_TEST_PROPERTIES}")
list(LENGTH test_properties test_properties_length)
while(NOT test_properties_length EQUAL 0)
list(POP_FRONT test_properties property_name property_value)
list(LENGTH test_properties test_properties_length)
if(arg_LIST_SEPARATOR)
string(REPLACE "${arg_LIST_SEPARATOR}" ";" property_value "${property_value}")
endif()
string(APPEND script " ${property_name} [==[${property_value}]==]")
endwhile()
endif()
string(APPEND script ")\n")
# possibly unbalanced square brackets render lists invalid so skip such
# tests in ${arg_TEST_LIST}
@ -281,6 +293,7 @@ function(gtest_discover_tests_impl)
set(oneValueArgs
NO_PRETTY_TYPES # These two take a value, unlike gtest_discover_tests()
NO_PRETTY_VALUES #
LIST_SEPARATOR
TEST_TARGET
TEST_EXECUTABLE
TEST_WORKING_DIR
@ -435,5 +448,6 @@ if(CMAKE_SCRIPT_MODE_FILE)
TEST_EXTRA_ARGS "${TEST_EXTRA_ARGS}"
TEST_DISCOVERY_EXTRA_ARGS "${TEST_DISCOVERY_EXTRA_ARGS}"
TEST_PROPERTIES "${TEST_PROPERTIES}"
LIST_SEPARATOR "${LIST_SEPARATOR}"
)
endif()

View File

@ -114,3 +114,15 @@ gtest_add_tests($<TARGET_FILE:test_gtest4> "" main4.h)
if(NOT TEST GoogleTest.NoKeywords)
message(FATAL_ERROR "Test case GoogleTest.NoKeywords not defined")
endif()
# Check if LIST_SEPARATOR allows passing both ENVIRONMENT values to test
add_executable(test_gtest5 main5.cxx)
target_link_libraries(test_gtest5 GTest::Main)
set(ENVIRONMENT VALX=1 VALY=2)
set(LIST_SEPARATOR ",")
list(JOIN ENVIRONMENT ${LIST_SEPARATOR} ENVIRONMENT)
gtest_discover_tests(test_gtest5
LIST_SEPARATOR ${LIST_SEPARATOR}
PROPERTIES
ENVIRONMENT "${ENVIRONMENT}"
)

View File

@ -20,11 +20,10 @@ int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);
if (argc > 1) {
if (argv[1] != std::string("--forceFail")) {
throw "Unexpected argument";
for (int i = 0; i < argc; ++i) {
if (argv[i] == std::string("--forceFail")) {
shouldFail = true;
}
shouldFail = true;
}
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,9 @@
#include <cstdlib>
#include <gtest/gtest.h>
TEST(GoogleTest, Add)
{
EXPECT_EQ(std::atoi(std::getenv("VALX")) + std::atoi(std::getenv("VALY")),
3);
}