Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
`<ostream>` 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 <cpp-library/testing.hpp>
#if CPP_LIBRARY_TESTING()
#include <doctest/doctest.h>
#include <ostream>
#include <string_view>

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 `<project>_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
Expand All @@ -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
`<project>_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
Expand Down
27 changes: 2 additions & 25 deletions cmake/cpp-library-testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand Down
11 changes: 0 additions & 11 deletions cpp-library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 13 additions & 1 deletion tests/setup/fixtures/source_testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/setup/fixtures/source_testing/src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
22 changes: 11 additions & 11 deletions tests/setup/fixtures/source_testing/tests/existing.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <cpp-library/testing.hpp>
#include <fixture/api.hpp>
#include "private/component.hpp"

#if defined(FIXTURE_USING_DLL)
#error "Unit tests must not import the production DLL"
#if __has_include(<cpp-library/testing.hpp>) || __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 <doctest/doctest.h>
#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
}
14 changes: 8 additions & 6 deletions tests/setup/fixtures/source_testing/tests/rejected_fail.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <cpp-library/testing.hpp>
#include <fixture/api.hpp>
#include "private/component.hpp"

#if __has_include(<cpp-library/testing.hpp>) || __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
}
9 changes: 8 additions & 1 deletion tests/setup/test_source_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Loading