diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f24014b..1287484 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,6 +35,9 @@ jobs: - name: Run independent public header compilation tests run: cmake -P tests/setup/test_header_checks.cmake + - name: Run static and in-source unit testing checks + run: cmake -P tests/setup/test_source_testing.cmake + - name: Run project version override tests run: cmake -P tests/setup/test_version_override.cmake @@ -82,6 +85,9 @@ jobs: - name: Run independent public header compilation tests run: cmake -P tests/setup/test_header_checks.cmake + - name: Run static and in-source unit testing checks + run: cmake -P tests/setup/test_source_testing.cmake + - name: Run project version override tests run: cmake -P tests/setup/test_version_override.cmake diff --git a/.scout/memory/index.md b/.scout/memory/index.md index 6c8567c..c1931e6 100644 --- a/.scout/memory/index.md +++ b/.scout/memory/index.md @@ -11,6 +11,7 @@ - [[knowledge/generated-ci-package-artifact-check.md|Generated CI package artifact check]] (decision, 0.95) `ci` `packaging` `cmake` - [[knowledge/installation-supports-separately-installed-dependencies-not-mixed-source-export-workarounds.md|Installation supports separately installed dependencies, not mixed source export workarounds]] (decision, 0.99) `cmake` `installation` `cpm` `supported-boundary` - [[knowledge/public-header-verification-must-participate-in-the-default-build.md|Public-header verification must participate in the default build]] (pattern, 0.99) `cmake` `header-verification` `testing` `file-sets` +- [[knowledge/static-and-in-source-tests-require-separate-implementation-compilations.md|Static and in-source tests require separate implementation compilations]] (decision, 0.99) `cmake` `testing` `doctest` `static-linkage` `private-components` `generated-headers` - [[knowledge/target-naming-and-alias-deduplication-convention.md|Target Naming and Alias Deduplication Convention]] (pattern, 0.75) `target-naming` `alias-deduplication` `package-name` `clean-name` `cmake-alias-convention` - [[knowledge/testing-and-example-executable-target-generation-gated-by-build-testing.md|Testing and Example Executable Target Generation Gated by BUILD_TESTING]] (pattern, 0.75) `build-testing` `doctest` `testing-setup` `negative-compilation-tests` `runtime-dll-copy` - [[knowledge/version-overrides-must-not-leak-into-independently-named-subprojects.md|Version overrides must not leak into independently named subprojects]] (decision, 0.99) `cmake` `versions` `project-scope` `package-managers` diff --git a/.scout/memory/knowledge/public-header-verification-must-participate-in-the-default-build.md b/.scout/memory/knowledge/public-header-verification-must-participate-in-the-default-build.md index 55ff7c1..6928f4c 100644 --- a/.scout/memory/knowledge/public-header-verification-must-participate-in-the-default-build.md +++ b/.scout/memory/knowledge/public-header-verification-must-participate-in-the-default-build.md @@ -3,16 +3,23 @@ title: "Public-header verification must participate in the default build" entity_type: pattern confidence: 0.99 created: 2026-11-19T00:38:12Z -last_accessed: 2026-11-19T00:38:12Z -last_reinforced: 2026-11-19T00:38:12Z -access_count: 1 +last_accessed: 2026-11-19T20:40:56Z +last_reinforced: 2026-11-19T20:40:56Z +access_count: 3 sources: - "cpp-library.cmake:314-323" - "tests/setup/test_header_checks.cmake:151-159" + - "cpp-library.cmake:326-335" + - "tests/setup/test_header_checks.cmake:152-162" tags: ["cmake", "header-verification", "testing", "file-sets"] tier: knowledge +votes_up: 1 +votes_down: 0 +last_voted: 2026-11-19T20:41:02Z --- # Public-header verification must participate in the default build -cpp_library_setup() enables native CMake VERIFY_INTERFACE_HEADER_SETS only for top-level projects with BUILD_TESTING enabled. Preserve both the target property and the ALL custom target dependency on _verify_interface_header_sets: native verification targets are excluded from the default build, so setting the property alone does not enforce the check. Keep verification based on PUBLIC/INTERFACE header file sets rather than a second header list; this includes generated headers and file sets added after setup. These build-time, per-header checks are separate from configure-time clangd indexing. C++17/20/23 fixtures check the configured standard, public usage requirements, generated-header ordering, and the testing-off/subproject scope boundaries. Platform-specific public headers must guard their contents and be no-ops when unsupported; do not introduce platform-exclusion lists. +# Public-header verification must participate in the default build + +cpp_library_setup() enables native CMake VERIFY_INTERFACE_HEADER_SETS only for top-level projects with BUILD_TESTING enabled. Preserve both the target property and the ALL custom target dependency on _verify_interface_header_sets: native verification targets are excluded from the default build, so setting the property alone does not enforce the check. Keep verification based on PUBLIC/INTERFACE header file sets rather than a second header list; this includes generated headers and file sets added after setup. These build-time, per-header checks are separate from configure-time clangd indexing. C++17/20/23 fixtures check the configured standard, public usage requirements, generated-header ordering, and the testing-off/subproject scope boundaries. Platform-specific public headers must guard their contents and be no-ops when unsupported; do not introduce platform-exclusion lists. Keep unit tests' private src/ include root and generated testing configuration out of the production target's public interface so these checks remain independent. diff --git a/.scout/memory/knowledge/static-and-in-source-tests-require-separate-implementation-compilations.md b/.scout/memory/knowledge/static-and-in-source-tests-require-separate-implementation-compilations.md new file mode 100644 index 0000000..c35ca61 --- /dev/null +++ b/.scout/memory/knowledge/static-and-in-source-tests-require-separate-implementation-compilations.md @@ -0,0 +1,29 @@ +--- +title: "Static and in-source tests require separate implementation compilations" +entity_type: decision +confidence: 0.99 +created: 2026-11-19T20:41:11Z +last_accessed: 2026-11-19T20:55:02Z +last_reinforced: 2026-11-19T20:55:02Z +access_count: 2 +sources: + - "cmake/cpp-library-testing.cmake:8-60" + - "cmake/cpp-library-testing.cmake:62-96" + - "cmake/cpp-library-testing.cmake:9" + - "cmake/cpp-library-testing.cmake:18" + - "README.md:792" +tags: ["cmake", "testing", "doctest", "static-linkage", "private-components", "generated-headers"] +tier: knowledge +--- + +# Static and in-source tests require separate implementation compilations + +# Static and in-source tests require separate implementation compilations + +Preserve existing TESTS executable names and per-file mains. Their compiled component uses a separate production-mode static target, while _in_source_tests compiles every implementation source directly with one generated doctest main. Do not combine these modes: enabling registrations in the ordinary static test library repeats embedded tests in existing binaries, while relying on archive extraction can silently omit registration-only translation units. + +The private generated cpp-library/testing.hpp defines the function-style CPP_LIBRARY_TESTING() macro as 0 for production and the ordinary static implementation, and 1 for per-file test translation units and the separate in-source runner. Guard both doctest includes and cases with #if CPP_LIBRARY_TESTING(); never expose this generated header through installed public headers. + +Finalize variants after the owner directory declares its sources, links, and generated-header dependencies. CMake file-set headers are stored separately from SOURCES: preserve HEADER_SETS and INTERFACE_HEADER_SETS generation ordering through one shared header-generation target used by production and both test variants. Requiring callers to add explicit add_dependencies() masks missing file-set support; clean direct test-target builds must also work without that workaround. + +Static unit tests consume implementation compile requirements, including private requirements, rather than the production target's consumer-only compile definitions: the documented Windows DLL pattern adds an INTERFACE import definition that is incorrect for a static implementation. Keep production BUILD_SHARED_LIBS and third-party dependency linkage unchanged. Test targets and their private include roots are not exported. diff --git a/README.md b/README.md index 5e7c217..19b2353 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ cpp_library_enable_dependency_tracking() # Now declare project project(your-library LANGUAGES CXX) -# Enable testing infrastructure (required for TESTS and EXAMPLES) +# Enable testing infrastructure (required for SOURCES, TESTS, and EXAMPLES) include(CTest) # Setup library @@ -791,7 +791,71 @@ precedence over the legacy variable. ### Testing -Uses [doctest](https://github.com/doctest/doctest) with CTest integration. Compile-fail tests are automatically detected via `_fail` suffix in filenames. +Tests use [doctest](https://github.com/doctest/doctest) with CTest integration. +Call `include(CTest)` at directory scope before `cpp_library_setup()`. +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. + +The library implementation and unit 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. + +#### In-source tests + +Put tests for unnamed-namespace components in the same `.cpp` file as the +implementation. Include the generated private configuration header and guard +both the doctest include and test cases: + +```cpp +#include + +namespace { +/// Reports whether value is even. +constexpr bool is_even(unsigned value) noexcept { + return value % 2 == 0; +} +} + +#if CPP_LIBRARY_TESTING() +#include + +TEST_CASE("even values") { + CHECK(is_even(0)); + CHECK(is_even(4)); + CHECK_FALSE(is_even(3)); +} +#endif +``` + +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 +public headers. Production compilation doesn't need doctest headers or libraries. + +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 +registration-only translation units aren't lost inside a static archive. +cpp-library supplies its doctest main: don't define +`DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN` in implementation sources. Keep the existing +mains in your `TESTS` files unchanged. Embedded tests run only in the separate +runner, not again in each per-file executable. + +Test variants require additional implementation compilation. They use the +library's finalized build settings, including dependencies and generated-header +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`. #### Public-header compilation diff --git a/cmake/cpp-library-setup.cmake b/cmake/cpp-library-setup.cmake index 6520910..2a4cb9f 100644 --- a/cmake/cpp-library-setup.cmake +++ b/cmake/cpp-library-setup.cmake @@ -97,6 +97,8 @@ function(_cpp_library_setup_core) if(ARG_SOURCES) # Respect the consumer's BUILD_SHARED_LIBS setting. add_library(${ARG_NAME} ${ARG_SOURCES}) + _cpp_library_setup_testing_config(TARGET "${ARG_NAME}" ENABLED 0) + target_include_directories(${ARG_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") if(NOT DEFINED CMAKE_CXX_EXTENSIONS) set_target_properties(${ARG_NAME} PROPERTIES CXX_EXTENSIONS OFF) endif() diff --git a/cmake/cpp-library-testing.cmake b/cmake/cpp-library-testing.cmake index fdc015f..d70c9db 100644 --- a/cmake/cpp-library-testing.cmake +++ b/cmake/cpp-library-testing.cmake @@ -1,10 +1,125 @@ # SPDX-License-Identifier: BSL-1.0 # -# cpp-library-testing.cmake - Runtime DLL deployment and testing compatibility +# cpp-library-testing.cmake - Private test configuration and executable support # # Provides cpp_library_copy_runtime_dlls for custom and toolkit-created executables. # _cpp_library_setup_testing delegates to _cpp_library_setup_executables for compatibility. +# Adds a private, always-defined function-style testing configuration. +function(_cpp_library_setup_testing_config) + cmake_parse_arguments(ARG "" "TARGET;ENABLED" "" ${ARGN}) + set(directory "${CMAKE_CURRENT_BINARY_DIR}/cpp-library/testing-${ARG_ENABLED}") + file(CONFIGURE OUTPUT "${directory}/cpp-library/testing.hpp" + CONTENT "#pragma once\n#define CPP_LIBRARY_TESTING() @ARG_ENABLED@\n" @ONLY) + target_include_directories("${ARG_TARGET}" BEFORE PRIVATE "${directory}") +endfunction() + +# Copies finalized build requirements without linking the production implementation. +function(_cpp_library_finish_test_target source destination) + get_target_property(sources "${source}" SOURCES) + target_sources("${destination}" PRIVATE ${sources}) + # File-set headers aren't SOURCES; share their generation before any compilation. + set(header_target "${source}_test_headers") + if(NOT TARGET "${header_target}") + get_property(header_sets TARGET "${source}" PROPERTY HEADER_SETS) + get_property(interface_sets TARGET "${source}" PROPERTY INTERFACE_HEADER_SETS) + list(APPEND header_sets ${interface_sets}) + list(REMOVE_DUPLICATES header_sets) + set(headers) + foreach(header_set IN LISTS header_sets) + if(header_set STREQUAL "HEADERS") + get_property(files TARGET "${source}" PROPERTY HEADER_SET) + else() + get_property(files TARGET "${source}" PROPERTY "HEADER_SET_${header_set}") + endif() + list(APPEND headers ${files}) + endforeach() + if(headers) + list(REMOVE_DUPLICATES headers) + add_custom_target("${header_target}" DEPENDS ${headers}) + add_dependencies("${source}" "${header_target}") + endif() + endif() + 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 + CXX_CLANG_TIDY CXX_COMPILER_LAUNCHER LINK_OPTIONS LINK_DIRECTORIES + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES + INTERFACE_LINK_OPTIONS INTERFACE_LINK_DIRECTORIES + INTERFACE_LINK_LIBRARIES_DIRECT INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE) + get_property(is_set TARGET "${source}" PROPERTY "${property}" SET) + 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}") + endif() + get_target_property(interface_links "${source}" INTERFACE_LINK_LIBRARIES) + if(interface_links) + target_link_libraries("${destination}" INTERFACE ${interface_links}) + endif() + get_target_property(dependencies "${source}" MANUALLY_ADDED_DEPENDENCIES) + if(dependencies) + add_dependencies("${destination}" ${dependencies}) + endif() +endfunction() + +# Configures the separate runner after all implementation requirements are known. +function(_cpp_library_finish_in_source_tests source destination) + _cpp_library_finish_test_target("${source}" "${destination}") + _cpp_library_setup_testing_config(TARGET "${destination}" ENABLED 1) + target_link_libraries("${destination}" PRIVATE doctest::doctest) +endfunction() + +# Creates static per-file test support and a separate in-source runner. +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") + file(CONFIGURE OUTPUT "${main}" CONTENT + "#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN\n#include \n") + add_executable("${runner}" "${main}") + cmake_language(EVAL CODE + "cmake_language(DEFER CALL _cpp_library_finish_in_source_tests + [[${ARG_TARGET}]] [[${runner}]])") + cpp_library_copy_runtime_dlls("${runner}") + add_test(NAME "${runner}" COMMAND "${runner}") + set_tests_properties("${runner}" PROPERTIES LABELS doctest + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + endif() +endfunction() + # Deploys runtime DLLs beside a custom target on Windows; empty lists need no copy. # Call in the directory that created the target, as required by POST_BUILD commands. function(cpp_library_copy_runtime_dlls target) diff --git a/cpp-library.cmake b/cpp-library.cmake index 80f210d..101132b 100644 --- a/cpp-library.cmake +++ b/cpp-library.cmake @@ -97,6 +97,14 @@ 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 @@ -122,7 +130,7 @@ function(_cpp_library_setup_executables) if(fail_pos GREATER -1) # Negative compile test: this executable must fail to compile add_executable(${executable_base} EXCLUDE_FROM_ALL "${source_dir}/${executable}") - target_link_libraries(${executable_base} PRIVATE ${ARG_NAMESPACE}::${CLEAN_NAME}) + target_link_libraries(${executable_base} PRIVATE ${library_target}) add_test( NAME compile_${executable_base} COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target ${executable_base} @@ -132,7 +140,7 @@ function(_cpp_library_setup_executables) else() # Regular executable - build and link normally add_executable(${executable_base} "${source_dir}/${executable}") - target_link_libraries(${executable_base} PRIVATE ${ARG_NAMESPACE}::${CLEAN_NAME} doctest::doctest) + target_link_libraries(${executable_base} PRIVATE ${library_target} doctest::doctest) cpp_library_copy_runtime_dlls(${executable_base}) @@ -147,6 +155,10 @@ 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() @@ -231,7 +243,7 @@ function(cpp_library_setup) endif() endif() - # IMPORTANT: If TESTS or EXAMPLES are specified, include(CTest) MUST be called + # IMPORTANT: For SOURCES, TESTS, or EXAMPLES, include(CTest) MUST be called # at directory scope before cpp_library_setup(). This enables the testing infrastructure # required for add_test() and defines the BUILD_TESTING option. # @@ -333,13 +345,17 @@ function(cpp_library_setup) # Download doctest if we'll need it for tests or examples # This must happen during normal configuration (not deferred) because CPMAddPackage uses add_subdirectory - if(BUILD_TESTING AND (ARG_TESTS OR ARG_EXAMPLES)) + if(BUILD_TESTING AND (ARG_TESTS OR ARG_EXAMPLES OR ARG_SOURCES)) if(NOT TARGET doctest::doctest) # [DEPENDENCY] https://github.com/doctest/doctest/releases CPMAddPackage("gh:doctest/doctest@2.5.3") endif() endif() + if(BUILD_TESTING AND ARG_SOURCES) + _cpp_library_setup_test_support(TARGET "${ARG_NAME}") + endif() + # Setup testing (if tests are specified) # enable_testing() has already been called above via include(), so we can add tests immediately if(BUILD_TESTING AND ARG_TESTS) diff --git a/setup.cmake b/setup.cmake index 2efc076..9070adf 100644 --- a/setup.cmake +++ b/setup.cmake @@ -388,7 +388,7 @@ cpp_library_enable_dependency_tracking() # Now declare project project(${ARG_NAME}) -# Enable CTest infrastructure (required for tests/examples to work) +# Enable CTest infrastructure (required for sources/tests/examples) include(CTest) # Setup library diff --git a/tests/setup/fixtures/mock_doctest.cmake b/tests/setup/fixtures/mock_doctest.cmake new file mode 100644 index 0000000..f824dc2 --- /dev/null +++ b/tests/setup/fixtures/mock_doctest.cmake @@ -0,0 +1,8 @@ +# Compiler-only fixtures do not test registration; source_testing uses real doctest. +if(NOT TARGET doctest::doctest) + add_library(doctest::doctest INTERFACE IMPORTED) + set(mock_doctest "${CMAKE_CURRENT_BINARY_DIR}/mock-doctest") + file(WRITE "${mock_doctest}/doctest/doctest.h" + "#if defined(DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)\nint main() { return 0; }\n#endif\n") + set_property(TARGET doctest::doctest PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${mock_doctest}") +endif() diff --git a/tests/setup/fixtures/source_testing/CMakeLists.txt b/tests/setup/fixtures/source_testing/CMakeLists.txt new file mode 100644 index 0000000..c5ff99c --- /dev/null +++ b/tests/setup/fixtures/source_testing/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.24) +include("${TOOLKIT_SOURCE}/cpp-library.cmake") +project(sample LANGUAGES CXX) +include(CTest) +set(CPP_LIBRARY_VERSION 1.0.0) +set(FIXTURE_INSTALL OFF CACHE BOOL "" FORCE) +set(BUILD_DOCS OFF CACHE BOOL "" FORCE) + +function(CPMAddPackage specification) + if(NOT specification MATCHES "^gh:doctest/doctest@([0-9.]+)$") + message(FATAL_ERROR "Unexpected test dependency: ${specification}") + endif() + set(version "${CMAKE_MATCH_1}") + include(FetchContent) + FetchContent_Declare(doctest + URL "https://github.com/doctest/doctest/archive/refs/tags/v${version}.tar.gz") + FetchContent_MakeAvailable(doctest) +endfunction() + +if(NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 20) +endif() +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(source_args SOURCES component.cpp) +if(HEADER_ONLY) + set(source_args) +endif() +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 + HEADERS api.hpp REQUIRES_CPP_VERSION "${CMAKE_CXX_STANDARD}" ${source_args} + ${test_args}) + +add_library(requirements INTERFACE) +target_compile_definitions(requirements INTERFACE PUBLIC_VALUE=19) +if(HEADER_ONLY) + target_compile_definitions(sample INTERFACE FIXTURE_HEADER_ONLY) + target_link_libraries(sample INTERFACE requirements) + return() +endif() +set_target_properties(sample PROPERTIES CXX_VISIBILITY_PRESET hidden) +target_sources(sample PRIVATE src/registration.cpp) +target_compile_definitions(sample PRIVATE PRIVATE_VALUE=23 + "REQUIRED_STANDARD=${CMAKE_CXX_STANDARD}" + "EXPECTED_INTERNAL=$,41,42>") +target_link_libraries(sample PUBLIC requirements) +if(BUILD_SHARED_LIBS) + target_compile_definitions(sample INTERFACE FIXTURE_USING_DLL) +endif() +set(generated "${CMAKE_CURRENT_BINARY_DIR}/generated/config.hpp") +add_custom_command(OUTPUT "${generated}" + COMMAND "${CMAKE_COMMAND}" "-DOUTPUT=${generated}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/generate.cmake") +if(FILE_SET_GENERATION) + if(DEFAULT_HEADER_SET) + target_sources(sample PUBLIC FILE_SET HEADERS + BASE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/generated" FILES "${generated}") + else() + target_sources(sample PRIVATE FILE_SET generated_headers TYPE HEADERS + BASE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/generated" FILES "${generated}") + endif() +else() + add_custom_target(generate_config DEPENDS "${generated}") + add_dependencies(sample generate_config) +endif() +target_include_directories(sample PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated") +if(NOT BUILD_TESTING OR NOT PROJECT_IS_TOP_LEVEL) + foreach(target existing rejected_fail sample_in_source_tests doctest::doctest) + if(TARGET "${target}") + message(FATAL_ERROR "Unexpected development dependency/target: ${target}") + endif() + endforeach() +endif() diff --git a/tests/setup/fixtures/source_testing/examples/example.cpp b/tests/setup/fixtures/source_testing/examples/example.cpp new file mode 100644 index 0000000..94de501 --- /dev/null +++ b/tests/setup/fixtures/source_testing/examples/example.cpp @@ -0,0 +1,5 @@ +#include + +int main() { + return public_value() == 42 ? 0 : 1; +} diff --git a/tests/setup/fixtures/source_testing/generate.cmake b/tests/setup/fixtures/source_testing/generate.cmake new file mode 100644 index 0000000..032c446 --- /dev/null +++ b/tests/setup/fixtures/source_testing/generate.cmake @@ -0,0 +1,3 @@ +get_filename_component(directory "${OUTPUT}" DIRECTORY) +file(MAKE_DIRECTORY "${directory}") +file(WRITE "${OUTPUT}" "#pragma once\n#define GENERATED_VALUE 42\n") diff --git a/tests/setup/fixtures/source_testing/include/fixture/api.hpp b/tests/setup/fixtures/source_testing/include/fixture/api.hpp new file mode 100644 index 0000000..1eaae42 --- /dev/null +++ b/tests/setup/fixtures/source_testing/include/fixture/api.hpp @@ -0,0 +1,20 @@ +#pragma once + +#if defined(_WIN32) +#if defined(sample_EXPORTS) +#define FIXTURE_API __declspec(dllexport) +#elif defined(FIXTURE_USING_DLL) +#define FIXTURE_API __declspec(dllimport) +#else +#define FIXTURE_API +#endif +#else +#define FIXTURE_API __attribute__((visibility("default"))) +#endif + +/// Returns the configured public value. +#if defined(FIXTURE_HEADER_ONLY) +inline int public_value() { return 42; } +#else +FIXTURE_API int public_value(); +#endif diff --git a/tests/setup/fixtures/source_testing/src/component.cpp b/tests/setup/fixtures/source_testing/src/component.cpp new file mode 100644 index 0000000..6e6d643 --- /dev/null +++ b/tests/setup/fixtures/source_testing/src/component.cpp @@ -0,0 +1,24 @@ +#include +#include +#include "private/component.hpp" +#include + +#if CPP_LIBRARY_TESTING() +#include +#include + +TEST_CASE("compiled component") { + CHECK(private_value() == 42); + std::ofstream marker("in-source-executions", std::ios::app); + marker << "component\n"; + CHECK(marker.good()); +} +#endif + +int public_value() { + return GENERATED_VALUE; +} + +int private_value() { + return PRIVATE_VALUE + PUBLIC_VALUE; +} diff --git a/tests/setup/fixtures/source_testing/src/private/component.hpp b/tests/setup/fixtures/source_testing/src/private/component.hpp new file mode 100644 index 0000000..61fc7cf --- /dev/null +++ b/tests/setup/fixtures/source_testing/src/private/component.hpp @@ -0,0 +1,14 @@ +#pragma once + +/// Returns the configured private value. +#if defined(FIXTURE_HEADER_ONLY) +inline int private_value() { return 42; } +#else +int private_value(); +#include + +/// Returns the value derived from private build requirements. +inline int configured_private_value() { + return GENERATED_VALUE + PRIVATE_VALUE + PUBLIC_VALUE; +} +#endif diff --git a/tests/setup/fixtures/source_testing/src/registration.cpp b/tests/setup/fixtures/source_testing/src/registration.cpp new file mode 100644 index 0000000..e8128aa --- /dev/null +++ b/tests/setup/fixtures/source_testing/src/registration.cpp @@ -0,0 +1,36 @@ +#include + +namespace { +/// Returns the configured internal value. +[[maybe_unused]] int internal_value() { + return 42; +} +} + +#if CPP_LIBRARY_TESTING() +#include +#include + +#if defined(_MSVC_LANG) +#define FIXTURE_CPLUSPLUS _MSVC_LANG +#else +#define FIXTURE_CPLUSPLUS __cplusplus +#endif + +#if REQUIRED_STANDARD == 17 +static_assert(FIXTURE_CPLUSPLUS >= 201703L); +#elif REQUIRED_STANDARD == 20 +static_assert(FIXTURE_CPLUSPLUS >= 202002L); +#elif REQUIRED_STANDARD == 23 +static_assert(FIXTURE_CPLUSPLUS > 202002L); +#endif + +namespace { +TEST_CASE("anonymous component") { + CHECK(internal_value() == EXPECTED_INTERNAL); + std::ofstream marker("in-source-executions", std::ios::app); + marker << "anonymous\n"; + CHECK(marker.good()); +} +} +#endif diff --git a/tests/setup/fixtures/source_testing/tests/existing.cpp b/tests/setup/fixtures/source_testing/tests/existing.cpp new file mode 100644 index 0000000..fe07775 --- /dev/null +++ b/tests/setup/fixtures/source_testing/tests/existing.cpp @@ -0,0 +1,20 @@ +#include +#include +#include "private/component.hpp" + +#if defined(FIXTURE_USING_DLL) +#error "Unit tests must not import the production DLL" +#endif + +#if CPP_LIBRARY_TESTING() +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include +#endif + +TEST_CASE("public and private implementation") { + 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 new file mode 100644 index 0000000..332f6d1 --- /dev/null +++ b/tests/setup/fixtures/source_testing/tests/rejected_fail.cpp @@ -0,0 +1,11 @@ +#include +#include +#include "private/component.hpp" + +int main() { +#if CPP_LIBRARY_TESTING() + return public_value("not an argument"); +#else + return 0; +#endif +} diff --git a/tests/setup/fixtures/target_type/CMakeLists.txt b/tests/setup/fixtures/target_type/CMakeLists.txt index c70e3ad..0a130e2 100644 --- a/tests/setup/fixtures/target_type/CMakeLists.txt +++ b/tests/setup/fixtures/target_type/CMakeLists.txt @@ -11,7 +11,7 @@ if(NOT CHECK_INSTALL_DIRS) endif() if(CHECK_DIALECT) include(CTest) - add_library(doctest::doctest INTERFACE IMPORTED) + include("${TOOLKIT_SOURCE}/tests/setup/fixtures/mock_doctest.cmake") add_library(unrelated INTERFACE) get_target_property(unrelated_extensions unrelated CXX_EXTENSIONS) set(unrelated_before "${unrelated_extensions}") diff --git a/tests/setup/test_header_checks.cmake b/tests/setup/test_header_checks.cmake index 9bd30ac..03a40f9 100644 --- a/tests/setup/test_header_checks.cmake +++ b/tests/setup/test_header_checks.cmake @@ -20,6 +20,7 @@ cmake_minimum_required(VERSION 3.24) include("${TOOLKIT_SOURCE}/cpp-library.cmake") project(checked LANGUAGES CXX) include(CTest) +include("${TOOLKIT_SOURCE}/tests/setup/fixtures/mock_doctest.cmake") set(CMAKE_CXX_STANDARD_REQUIRED ON) set(source_args) if(COMPILED) diff --git a/tests/setup/test_source_testing.cmake b/tests/setup/test_source_testing.cmake new file mode 100644 index 0000000..df4df17 --- /dev/null +++ b/tests/setup/test_source_testing.cmake @@ -0,0 +1,94 @@ +# SPDX-License-Identifier: BSL-1.0 +# Run as: cmake -P tests/setup/test_source_testing.cmake +cmake_minimum_required(VERSION 3.24) +get_filename_component(toolkit_source "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE) +string(RANDOM LENGTH 16 ALPHABET 0123456789abcdef test_id) +set(test_root "${toolkit_source}/build/source-testing-${test_id}") +find_program(ctest NAMES ctest REQUIRED) + +function(run) + execute_process(COMMAND ${ARGV} RESULT_VARIABLE result + OUTPUT_VARIABLE out ERROR_VARIABLE err) + if(NOT result EQUAL 0) + message(FATAL_ERROR "Command failed (${result}): ${ARGV}\n${out}\n${err}") + endif() +endfunction() + +function(verify_executions source) + if(NOT EXISTS "${source}/in-source-executions") + message(FATAL_ERROR "Embedded tests were not executed") + endif() + file(STRINGS "${source}/in-source-executions" executions) + list(SORT executions) + if(NOT executions STREQUAL "anonymous;component") + message(FATAL_ERROR "Embedded tests did not execute exactly once: ${executions}") + endif() +endfunction() + +foreach(shared OFF ON) + set(build "${test_root}/${shared}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/source_testing/" + DESTINATION "${build}/source") + run("${CMAKE_COMMAND}" -S "${build}/source" -B "${build}/build" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" "-DBUILD_SHARED_LIBS=${shared}" + "-DFILE_SET_GENERATION=${shared}" + "-DFETCHCONTENT_BASE_DIR=${test_root}/dependencies") + run("${CMAKE_COMMAND}" --build "${build}/build" --target existing sample_in_source_tests) + run("${CMAKE_COMMAND}" --build "${build}/build") + execute_process(COMMAND "${CMAKE_COMMAND}" --build "${build}/build" --target rejected_fail + RESULT_VARIABLE rejected OUTPUT_VARIABLE rejected_out ERROR_VARIABLE rejected_err) + 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() + run("${ctest}" --test-dir "${build}/build" --output-on-failure) + verify_executions("${build}/source") +endforeach() +foreach(standard 17 20 23) + set(build "${test_root}/source-only-${standard}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/source_testing/" + DESTINATION "${build}/source") + run("${CMAKE_COMMAND}" -S "${build}/source" -B "${build}/build" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" -DBUILD_SHARED_LIBS=ON -DSOURCE_ONLY=ON + -DFILE_SET_GENERATION=ON -DDEFAULT_HEADER_SET=ON + "-DCMAKE_CXX_STANDARD=${standard}" "-DFETCHCONTENT_BASE_DIR=${test_root}/dependencies") + run("${CMAKE_COMMAND}" --build "${build}/build" --target sample_in_source_tests) + run("${ctest}" --test-dir "${build}/build" --output-on-failure) + verify_executions("${build}/source") +endforeach() +set(build "${test_root}/failing") +file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/source_testing/" DESTINATION "${build}/source") +run("${CMAKE_COMMAND}" -S "${build}/source" -B "${build}/build" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" -DSOURCE_ONLY=ON -DFAIL_EMBEDDED=ON + -DFILE_SET_GENERATION=ON + "-DFETCHCONTENT_BASE_DIR=${test_root}/dependencies") +run("${CMAKE_COMMAND}" --build "${build}/build" --target sample_in_source_tests) +execute_process(COMMAND "${ctest}" --test-dir "${build}/build" --output-on-failure + RESULT_VARIABLE result OUTPUT_VARIABLE out ERROR_VARIABLE err) +if(result EQUAL 0 OR NOT out MATCHES "anonymous component") + message(FATAL_ERROR "Failing embedded assertion did not fail CTest:\n${out}\n${err}") +endif() +foreach(mode disabled header-only nested) + set(build "${test_root}/${mode}") + file(COPY "${CMAKE_CURRENT_LIST_DIR}/fixtures/source_testing/" + DESTINATION "${build}/source/library") + set(options -DBUILD_SHARED_LIBS=OFF) + set(source "${build}/source/library") + if(mode STREQUAL "disabled") + list(APPEND options -DBUILD_TESTING=OFF) + elseif(mode STREQUAL "header-only") + list(APPEND options -DHEADER_ONLY=ON) + else() + file(WRITE "${build}/source/CMakeLists.txt" + "cmake_minimum_required(VERSION 3.24)\nproject(consumer LANGUAGES CXX)\nadd_subdirectory(library)\n") + set(source "${build}/source") + endif() + run("${CMAKE_COMMAND}" -S "${source}" -B "${build}/build" -G Ninja + "-DTOOLKIT_SOURCE=${toolkit_source}" ${options} + "-DFETCHCONTENT_BASE_DIR=${test_root}/dependencies") + run("${CMAKE_COMMAND}" --build "${build}/build") + if(mode STREQUAL "header-only") + run("${ctest}" --test-dir "${build}/build" --output-on-failure) + endif() +endforeach() +file(REMOVE_RECURSE "${test_root}") +message(STATUS "PASS source testing: static unit tests with static/shared production") diff --git a/tests/setup/test_target_type.cmake b/tests/setup/test_target_type.cmake index 7eaf802..8d1fb21 100644 --- a/tests/setup/test_target_type.cmake +++ b/tests/setup/test_target_type.cmake @@ -39,21 +39,23 @@ function(run_case name shared expected_type) file(READ "${case_binary}/compile_commands.json" commands) string(JSON command_count LENGTH "${commands}") math(EXPR last_command "${command_count} - 1") - set(checked_commands 0) + set(checked_sources) foreach(index RANGE ${last_command}) string(JSON file GET "${commands}" ${index} file) - if(file MATCHES "/(sample|dialect_test|compile_fail|dialect_example)\\.cpp$") + if(file MATCHES "/(sample|sample_in_source_tests|dialect_test|compile_fail|dialect_example)\\.cpp$") string(JSON command GET "${commands}" ${index} command) if(NOT command MATCHES "-std=c\\+\\+20") set(result 1) set(err "Expected standard C++20 compile flag for ${file}: ${command}") endif() - math(EXPR checked_commands "${checked_commands} + 1") + list(APPEND checked_sources "${file}") endif() endforeach() - if(NOT checked_commands EQUAL 4) + list(REMOVE_DUPLICATES checked_sources) + list(LENGTH checked_sources checked_commands) + if(NOT checked_commands EQUAL 5) set(result 1) - set(err "Expected four toolkit compile commands, got ${checked_commands}") + set(err "Expected five toolkit source files, got ${checked_commands}") endif() endif() endif()