From b13ce42c7a09156295db58f8ed66a6b239186d41 Mon Sep 17 00:00:00 2001 From: sean-parent Date: Sat, 21 Nov 2026 15:31:18 -0800 Subject: [PATCH] Make standalone tests exercise the production public API Remove private static-test support, preserve separate in-source testing, and document public-test migration and self-contained doctest headers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- README.md | 58 ++++++++++++++++--- cmake/cpp-library-testing.cmake | 27 +-------- cpp-library.cmake | 11 ---- .../fixtures/source_testing/CMakeLists.txt | 14 ++++- .../fixtures/source_testing/src/component.cpp | 1 + .../source_testing/tests/existing.cpp | 22 +++---- .../source_testing/tests/rejected_fail.cpp | 14 +++-- tests/setup/test_source_testing.cmake | 9 ++- 8 files changed, 92 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 0341b71..e87184c 100644 --- a/README.md +++ b/README.md @@ -849,13 +849,13 @@ Development tests are enabled for top-level projects with `BUILD_TESTING=ON`. Filenames containing `_fail` register compile-fail tests. Each `TESTS` file remains a separate executable with its own main. -Unit tests link a separate static implementation, even when the production -library uses `BUILD_SHARED_LIBS=ON`. They inherit the implementation's private -build requirements rather than consumer-only DLL-import definitions. Dependencies -retain the client's selected linkage, so a test can still require third-party -DLLs. Examples continue to link the production library. +`TESTS` and `EXAMPLES` link the production library and consume its public usage +requirements, including DLL-import definitions for shared libraries. They don't +receive private implementation settings, the testing configuration header, or +the private `src/` include root. Runtime DLLs are copied beside generated +executables on Windows. -The library implementation and unit tests have a private `src/` include root. +The library implementation and in-source tests have a private `src/` include root. Place private headers in `src/private/` and include them as `#include "private/header.hpp"`. This path isn't added to the production library's public interface or to the independent public-header checks. @@ -889,11 +889,34 @@ TEST_CASE("even values") { After you include the configuration header, `CPP_LIBRARY_TESTING()` is always defined as `0` or `1`. Use `#if CPP_LIBRARY_TESTING()`, not `#ifdef`, and don't -define it yourself. Production compilation and the static implementation used -by existing per-file tests use `0`; the in-source runner and the per-file test -sources use `1`. Keep this header private; don't include it from installed +define it yourself. Production compilation uses `0`; the in-source runner uses +`1`. Keep this header private; don't include it from installed public headers. Production compilation doesn't need doctest headers or libraries. +Each translation unit containing in-source tests must include the headers needed +by its assertions and diagnostic formatting. The generated runner's includes +don't supply definitions to other translation units. In particular, include +`` directly when assertion diagnostics need standard stream insertion, +including string or `std::string_view` diagnostics that otherwise encounter an +incomplete `std::basic_ostream` on MSVC. Keep test-only headers inside the selector: + +```cpp +#include +#if CPP_LIBRARY_TESTING() +#include +#include +#include + +TEST_CASE("string diagnostics") { + CHECK(std::string_view("value") == std::string_view("value")); +} +#endif +``` + +When moving standalone tests into implementation files, add these includes to +each affected file. A standalone doctest implementation might have supplied them +transitively; adding a header to one implementation file doesn't fix another. + For compiled libraries, cpp-library adds `_in_source_tests` to the normal build and CTest, even if you omit `TESTS`. This separate executable compiles the implementation with testing enabled and includes every source directly, so @@ -909,6 +932,23 @@ ordering declared after `cpp_library_setup()`, and its configured C++ standard. No in-source runner is added for header-only libraries, subprojects, or `BUILD_TESTING=OFF`. +#### Migrate private standalone tests + +**Breaking change from 6.x:** `TESTS` no longer links a separate static +implementation or exposes private headers and settings. Public tests exercise +the production target, including its shared-library boundary. + +Move tests that require private implementation access into the corresponding +implementation `.cpp` files under `#if CPP_LIBRARY_TESTING()`. Remove their +standalone entries from `TESTS`, and leave public-contract tests and their +doctest mains in `TESTS`. Don't define doctest implementation or main macros in +production source files. There is no compatibility option for the old private +standalone-test behavior. + +Build and run CTest to verify both the public executables and +`_in_source_tests`. If you rewrote generated test targets to restore +production links or remove private include paths, remove those overrides. + #### Public-header compilation In top-level projects with `BUILD_TESTING=ON`, the normal build compiles each diff --git a/cmake/cpp-library-testing.cmake b/cmake/cpp-library-testing.cmake index 9242705..94d0108 100644 --- a/cmake/cpp-library-testing.cmake +++ b/cmake/cpp-library-testing.cmake @@ -52,8 +52,6 @@ function(_cpp_library_finish_test_target source destination) if(TARGET "${header_target}") add_dependencies("${destination}" "${header_target}") endif() - get_target_property(type "${destination}" TYPE) - set(compile_usage INCLUDE_DIRECTORIES COMPILE_DEFINITIONS COMPILE_OPTIONS COMPILE_FEATURES) foreach(property INCLUDE_DIRECTORIES COMPILE_DEFINITIONS COMPILE_OPTIONS COMPILE_FEATURES CXX_STANDARD CXX_STANDARD_REQUIRED CXX_EXTENSIONS CXX_VISIBILITY_PRESET VISIBILITY_INLINES_HIDDEN POSITION_INDEPENDENT_CODE @@ -65,23 +63,11 @@ function(_cpp_library_finish_test_target source destination) if(is_set) get_target_property(value "${source}" "${property}") set_property(TARGET "${destination}" PROPERTY "${property}" "${value}") - if(type STREQUAL "STATIC_LIBRARY" AND property IN_LIST compile_usage) - # Unit tests consume implementation settings, not DLL-import definitions. - set_property(TARGET "${destination}" PROPERTY "INTERFACE_${property}" "${value}") - endif() endif() endforeach() get_target_property(links "${source}" LINK_LIBRARIES) if(links) - if(type STREQUAL "STATIC_LIBRARY") - target_link_libraries("${destination}" PUBLIC ${links}) - else() - target_link_libraries("${destination}" PRIVATE ${links}) - endif() - endif() - get_target_property(standard "${source}" CXX_STANDARD) - if(type STREQUAL "STATIC_LIBRARY" AND standard) - target_compile_features("${destination}" INTERFACE "cxx_std_${standard}") + target_link_libraries("${destination}" PRIVATE ${links}) endif() get_target_property(interface_links "${source}" INTERFACE_LINK_LIBRARIES) if(interface_links) @@ -100,19 +86,10 @@ function(_cpp_library_finish_in_source_tests source destination) target_link_libraries("${destination}" PRIVATE doctest::doctest) endfunction() -# Creates static per-file test support and a separate in-source runner. +# Creates a separate in-source runner for compiled libraries. function(_cpp_library_setup_test_support) cmake_parse_arguments(ARG "" "TARGET" "" ${ARGN}) get_target_property(type "${ARG_TARGET}" TYPE) - set(test_target "${ARG_TARGET}") - if(NOT type STREQUAL "INTERFACE_LIBRARY") - set(test_target "${ARG_TARGET}_static_tests") - add_library("${test_target}" STATIC EXCLUDE_FROM_ALL) - cmake_language(EVAL CODE - "cmake_language(DEFER CALL _cpp_library_finish_test_target - [[${ARG_TARGET}]] [[${test_target}]])") - endif() - set_property(TARGET "${ARG_TARGET}" PROPERTY _CPP_LIBRARY_UNIT_TEST_TARGET "${test_target}") if(NOT type STREQUAL "INTERFACE_LIBRARY") set(runner "${ARG_TARGET}_in_source_tests") set(main "${CMAKE_CURRENT_BINARY_DIR}/cpp-library/${runner}.cpp") diff --git a/cpp-library.cmake b/cpp-library.cmake index 40c7cb8..1196cbf 100644 --- a/cpp-library.cmake +++ b/cpp-library.cmake @@ -98,13 +98,6 @@ function(_cpp_library_setup_executables) # Extract the clean library name for linking (strip namespace prefix if present) string(REPLACE "${ARG_NAMESPACE}-" "" CLEAN_NAME "${ARG_NAME}") set(library_target "${ARG_NAMESPACE}::${CLEAN_NAME}") - if(ARG_TYPE STREQUAL "tests") - get_target_property(library_target "${ARG_NAME}" _CPP_LIBRARY_UNIT_TEST_TARGET) - if(NOT library_target) - _cpp_library_setup_test_support(TARGET "${ARG_NAME}") - get_target_property(library_target "${ARG_NAME}" _CPP_LIBRARY_UNIT_TEST_TARGET) - endif() - endif() # Note: doctest dependency is downloaded by cpp_library_setup before deferring # This function assumes doctest::doctest target already exists @@ -157,10 +150,6 @@ function(_cpp_library_setup_executables) ) endif() endif() - if(ARG_TYPE STREQUAL "tests") - _cpp_library_setup_testing_config(TARGET "${executable_base}" ENABLED 1) - target_include_directories("${executable_base}" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") - endif() if(NOT DEFINED CMAKE_CXX_EXTENSIONS) set_target_properties(${executable_base} PROPERTIES CXX_EXTENSIONS OFF) endif() diff --git a/tests/setup/fixtures/source_testing/CMakeLists.txt b/tests/setup/fixtures/source_testing/CMakeLists.txt index c5ff99c..fac53fa 100644 --- a/tests/setup/fixtures/source_testing/CMakeLists.txt +++ b/tests/setup/fixtures/source_testing/CMakeLists.txt @@ -29,10 +29,22 @@ set(test_args TESTS existing.cpp rejected_fail.cpp EXAMPLES example.cpp) if(SOURCE_ONLY) set(test_args) endif() -cpp_library_setup(DESCRIPTION "Static implementation testing" NAMESPACE fixture +cpp_library_setup(DESCRIPTION "Public and in-source testing" NAMESPACE fixture HEADERS api.hpp REQUIRES_CPP_VERSION "${CMAKE_CXX_STANDARD}" ${source_args} ${test_args}) +foreach(test_target existing rejected_fail) + if(TARGET "${test_target}") + get_target_property(links "${test_target}" LINK_LIBRARIES) + if(NOT "fixture::sample" IN_LIST links) + message(FATAL_ERROR "Public test ${test_target} must link the production library: ${links}") + endif() + endif() +endforeach() +if(TARGET existing AND BUILD_SHARED_LIBS AND NOT HEADER_ONLY) + target_compile_definitions(existing PRIVATE EXPECT_SHARED) +endif() + add_library(requirements INTERFACE) target_compile_definitions(requirements INTERFACE PUBLIC_VALUE=19) if(HEADER_ONLY) diff --git a/tests/setup/fixtures/source_testing/src/component.cpp b/tests/setup/fixtures/source_testing/src/component.cpp index 6e6d643..07ad937 100644 --- a/tests/setup/fixtures/source_testing/src/component.cpp +++ b/tests/setup/fixtures/source_testing/src/component.cpp @@ -9,6 +9,7 @@ TEST_CASE("compiled component") { CHECK(private_value() == 42); + CHECK(configured_private_value() == 84); std::ofstream marker("in-source-executions", std::ios::app); marker << "component\n"; CHECK(marker.good()); diff --git a/tests/setup/fixtures/source_testing/tests/existing.cpp b/tests/setup/fixtures/source_testing/tests/existing.cpp index fe07775..dfedae4 100644 --- a/tests/setup/fixtures/source_testing/tests/existing.cpp +++ b/tests/setup/fixtures/source_testing/tests/existing.cpp @@ -1,20 +1,20 @@ -#include #include -#include "private/component.hpp" -#if defined(FIXTURE_USING_DLL) -#error "Unit tests must not import the production DLL" +#if __has_include() || __has_include("private/component.hpp") +#error "Public tests must not receive private include directories" +#endif + +#if defined(PRIVATE_VALUE) || defined(REQUIRED_STANDARD) +#error "Public tests must not receive implementation-only definitions" +#endif + +#if defined(EXPECT_SHARED) && !defined(FIXTURE_USING_DLL) +#error "Public tests must consume production DLL usage requirements" #endif -#if CPP_LIBRARY_TESTING() #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include -#endif -TEST_CASE("public and private implementation") { +TEST_CASE("production public API") { CHECK(public_value() == 42); - CHECK(private_value() == 42); -#if !defined(FIXTURE_HEADER_ONLY) - CHECK(configured_private_value() == 84); -#endif } diff --git a/tests/setup/fixtures/source_testing/tests/rejected_fail.cpp b/tests/setup/fixtures/source_testing/tests/rejected_fail.cpp index 332f6d1..034c463 100644 --- a/tests/setup/fixtures/source_testing/tests/rejected_fail.cpp +++ b/tests/setup/fixtures/source_testing/tests/rejected_fail.cpp @@ -1,11 +1,13 @@ -#include #include -#include "private/component.hpp" + +#if __has_include() || __has_include("private/component.hpp") +#error "Public compile-fail tests must not receive private include directories" +#endif + +#if defined(PRIVATE_VALUE) || defined(REQUIRED_STANDARD) +#error "Public compile-fail tests must not receive implementation-only definitions" +#endif int main() { -#if CPP_LIBRARY_TESTING() return public_value("not an argument"); -#else - return 0; -#endif } diff --git a/tests/setup/test_source_testing.cmake b/tests/setup/test_source_testing.cmake index df4df17..beb1366 100644 --- a/tests/setup/test_source_testing.cmake +++ b/tests/setup/test_source_testing.cmake @@ -40,6 +40,13 @@ foreach(shared OFF ON) if(rejected EQUAL 0 OR NOT "${rejected_out}${rejected_err}" MATCHES "public_value") message(FATAL_ERROR "Compile-fail test did not reach its invalid call:\n${rejected_out}\n${rejected_err}") endif() + if("${rejected_out}${rejected_err}" MATCHES "Public compile-fail tests must not") + message(FATAL_ERROR "Compile-fail test received private implementation requirements") + endif() + run("${ctest}" --test-dir "${build}/build" --output-on-failure -R "^existing$") + if(EXISTS "${build}/source/in-source-executions") + message(FATAL_ERROR "Public tests executed embedded implementation tests") + endif() run("${ctest}" --test-dir "${build}/build" --output-on-failure) verify_executions("${build}/source") endforeach() @@ -91,4 +98,4 @@ foreach(mode disabled header-only nested) endif() endforeach() file(REMOVE_RECURSE "${test_root}") -message(STATUS "PASS source testing: static unit tests with static/shared production") +message(STATUS "PASS source testing: public and in-source tests with static/shared production")