From b44bb09891afdd0471dd05c745bcdcf907fe55f7 Mon Sep 17 00:00:00 2001 From: Torsten Wierschin Date: Fri, 17 Jul 2026 17:24:50 +0200 Subject: [PATCH] tests: add optional GoogleTest-based smoke-tests and CMake support (BUILD_GTEST_SMOKE) --- .../DdsUnitTestsPlaceholder/CMakeLists.txt | 56 ++++++++++++++++++- .../ddsDataTopicTests_gtest.cpp | 7 +++ .../ddsFunctionTopicTests_gtest.cpp | 9 +++ .../ddsGenericDataTests_gtest.cpp | 13 +++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 Lts/DDS/DdsUnitTestsPlaceholder/ddsDataTopicTests_gtest.cpp create mode 100644 Lts/DDS/DdsUnitTestsPlaceholder/ddsFunctionTopicTests_gtest.cpp create mode 100644 Lts/DDS/DdsUnitTestsPlaceholder/ddsGenericDataTests_gtest.cpp diff --git a/Lts/DDS/DdsUnitTestsPlaceholder/CMakeLists.txt b/Lts/DDS/DdsUnitTestsPlaceholder/CMakeLists.txt index 50b6c2b78..c2606bd89 100644 --- a/Lts/DDS/DdsUnitTestsPlaceholder/CMakeLists.txt +++ b/Lts/DDS/DdsUnitTestsPlaceholder/CMakeLists.txt @@ -6,29 +6,79 @@ # Option to force-building smoke-tests even when real test targets exist. option(BUILD_SMOKE_TESTS "Build smoke-tests even if real test executables are present" OFF) +option(BUILD_GTEST_SMOKE "Build GoogleTest-based smoke-tests for core logic" OFF) + +# Try to find GoogleTest if requested +if(BUILD_GTEST_SMOKE) + find_package(GTest QUIET) + if(NOT GTest_FOUND) + message(STATUS "GoogleTest not found via find_package; will attempt to use FetchContent fallback if enabled later.") + endif() +endif() if(BUILD_TESTING) include(CTest) - # Generic data tests smoke + # Generic data tests smoke (QtTest) if(BUILD_SMOKE_TESTS OR NOT TARGET ddsGenericDataTests) add_executable(ddsGenericDataTests_smoke ddsGenericDataTests_smoke.cpp) target_link_libraries(ddsGenericDataTests_smoke PRIVATE Qt5::Core Qt5::Test) add_test(NAME ddsGenericDataTests COMMAND $) endif() - # Data topic tests smoke + # Data topic tests smoke (QtTest) if(BUILD_SMOKE_TESTS OR NOT TARGET ddsDataTopicTests) add_executable(ddsDataTopicTests_smoke ddsDataTopicTests_smoke.cpp) target_link_libraries(ddsDataTopicTests_smoke PRIVATE Qt5::Core Qt5::Test) add_test(NAME ddsDataTopicTests COMMAND $) endif() - # Function topic tests smoke + # Function topic tests smoke (QtTest) if(BUILD_SMOKE_TESTS OR NOT TARGET ddsFunctionTopicTests) add_executable(ddsFunctionTopicTests_smoke ddsFunctionTopicTests_smoke.cpp) target_link_libraries(ddsFunctionTopicTests_smoke PRIVATE Qt5::Core Qt5::Test) add_test(NAME ddsFunctionTopicTests COMMAND $) endif() + # GoogleTest-based smoke-tests for core logic (optional) + if(BUILD_GTEST_SMOKE) + if(NOT GTest_FOUND) + # FetchContent fallback if GTest not found on system + include(FetchContent) + FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.tar.gz + ) + FetchContent_GetProperties(googletest) + if(NOT googletest_POPULATED) + FetchContent_Populate(googletest) + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}) + endif() + endif() + + if(TARGET GTest::gtest_main OR TARGET gtest_main) + # Generic data tests gtest + if(NOT TARGET ddsGenericDataTests) + add_executable(ddsGenericDataTests_gtest ddsGenericDataTests_gtest.cpp) + target_link_libraries(ddsGenericDataTests_gtest PRIVATE GTest::gtest_main) + add_test(NAME ddsGenericDataTests_gtest COMMAND $) + endif() + + if(NOT TARGET ddsDataTopicTests) + add_executable(ddsDataTopicTests_gtest ddsDataTopicTests_gtest.cpp) + target_link_libraries(ddsDataTopicTests_gtest PRIVATE GTest::gtest_main) + add_test(NAME ddsDataTopicTests_gtest COMMAND $) + endif() + + if(NOT TARGET ddsFunctionTopicTests) + add_executable(ddsFunctionTopicTests_gtest ddsFunctionTopicTests_gtest.cpp) + target_link_libraries(ddsFunctionTopicTests_gtest PRIVATE GTest::gtest_main) + add_test(NAME ddsFunctionTopicTests_gtest COMMAND $) + endif() + + else() + message(WARNING "GTest targets not available even after fallback; skipping GTest smoke-tests.") + endif() + endif() + endif() diff --git a/Lts/DDS/DdsUnitTestsPlaceholder/ddsDataTopicTests_gtest.cpp b/Lts/DDS/DdsUnitTestsPlaceholder/ddsDataTopicTests_gtest.cpp new file mode 100644 index 000000000..bfc62e5f1 --- /dev/null +++ b/Lts/DDS/DdsUnitTestsPlaceholder/ddsDataTopicTests_gtest.cpp @@ -0,0 +1,7 @@ +#include + +TEST(DataTopicSmokeGTest, Format) { + char buf[64]; + snprintf(buf, sizeof(buf), "%d-%s", 42, "abc"); + EXPECT_STREQ(buf, "42-abc"); +} diff --git a/Lts/DDS/DdsUnitTestsPlaceholder/ddsFunctionTopicTests_gtest.cpp b/Lts/DDS/DdsUnitTestsPlaceholder/ddsFunctionTopicTests_gtest.cpp new file mode 100644 index 000000000..5770ad2cb --- /dev/null +++ b/Lts/DDS/DdsUnitTestsPlaceholder/ddsFunctionTopicTests_gtest.cpp @@ -0,0 +1,9 @@ +#include +#include + +TEST(FunctionTopicSmokeGTest, Containers) { + std::map m; + m["one"] = 1; + m["two"] = 2; + EXPECT_EQ(m["two"], 2); +} diff --git a/Lts/DDS/DdsUnitTestsPlaceholder/ddsGenericDataTests_gtest.cpp b/Lts/DDS/DdsUnitTestsPlaceholder/ddsGenericDataTests_gtest.cpp new file mode 100644 index 000000000..edba5cfff --- /dev/null +++ b/Lts/DDS/DdsUnitTestsPlaceholder/ddsGenericDataTests_gtest.cpp @@ -0,0 +1,13 @@ +#include + +TEST(GenericSmokeGTest, BasicStringOps) { + std::string s = "MiXeD"; + for (auto &c : s) c = (char)std::tolower(c); + EXPECT_EQ(s, "mixed"); +} + +TEST(GenericSmokeGTest, VariantLike) { + // simulate basic core logic check + int v = 42; + EXPECT_EQ(v + 1, 43); +}