Skip to content

Commit 95d89d1

Browse files
authored
build: Add unit test build control through BUILD_TESTING and YSTDLIB_CPP_BUILD_TESTING flags; Support CTest through Catch2 functions. (#30)
1 parent e7cd616 commit 95d89d1

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Build-related directories and files
22
.task
33
build
4+
5+
# CTest directories and files
6+
Testing

CMake/ystdlib-cpp-helpers.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Adds a c++20 interface library in the subdirectory NAME with the target NAME and alias
22
# NAMESPACE::NAME. Libraries with multiple levels of namespace nesting are currently not supported.
33
#
4-
# If `BUILD_TESTING` is ON, build the unit tests specific to the current library, and link this
5-
# library against the unified unit test target for the entire `ystdlib-cpp`.
4+
# If `YSTDLIB_CPP_ENABLE_TESTS` is ON, builds the unit tests specific to the current library, and
5+
# links this library against the unified unit test target for the entire `ystdlib-cpp` project.
66
#
77
# @param NAME
88
# @param NAMESPACE
@@ -41,7 +41,8 @@ function(cpp_library)
4141
target_compile_features(${arg_cpp_lib_NAME} INTERFACE cxx_std_20)
4242
add_library(${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME})
4343

44-
if(BUILD_TESTING)
44+
if(YSTDLIB_CPP_ENABLE_TESTS)
45+
# Build library-specific unit test target
4546
set(_UNIT_TEST_TARGET "unit-test-${arg_cpp_lib_NAME}")
4647
add_executable(${_UNIT_TEST_TARGET})
4748
target_sources(${_UNIT_TEST_TARGET} PRIVATE ${arg_cpp_lib_TESTS_SOURCES})

CMakeLists.txt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.16.3)
1+
cmake_minimum_required(VERSION 3.22.1)
22

33
project(YSTDLIB_CPP LANGUAGES CXX)
44

@@ -33,25 +33,32 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS
3333
FORCE
3434
)
3535

36-
option(BUILD_TESTING "If ON, unit tests will be built." ON)
37-
38-
# Import CMake helper functions
39-
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)
40-
41-
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
36+
if(YSTDLIB_CPP_IS_TOP_LEVEL)
4237
# Include dependency settings if the project isn't being included as a subproject.
4338
# NOTE: We mark the file optional because if the user happens to have the dependencies
4439
# installed, this file is not necessary.
4540
include(build/deps/settings.cmake OPTIONAL)
41+
42+
# If previously undefined, `BUILD_TESTING` will be set to ON.
43+
include(CTest)
4644
endif()
4745

48-
if(BUILD_TESTING)
46+
option(YSTDLIB_CPP_BUILD_TESTING "Build the testing tree for ystdlib-cpp." ON)
47+
if(BUILD_TESTING AND YSTDLIB_CPP_BUILD_TESTING)
48+
set(YSTDLIB_CPP_ENABLE_TESTS ON)
49+
endif()
50+
51+
# Import CMake helper functions
52+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)
53+
54+
if(YSTDLIB_CPP_ENABLE_TESTS)
4955
find_package(Catch2 3.8.0 REQUIRED)
5056
if(Catch2_FOUND)
5157
message(STATUS "Found Catch2 ${Catch2_VERSION}.")
5258
else()
5359
message(FATAL_ERROR "Could not find libraries for Catch2.")
5460
endif()
61+
include(Catch)
5562

5663
# Set up the unified unit test target
5764
set(UNIFIED_UNIT_TEST_TARGET "unit-test-all")
@@ -65,6 +72,7 @@ if(BUILD_TESTING)
6572
RUNTIME_OUTPUT_DIRECTORY
6673
${CMAKE_BINARY_DIR}/testbin
6774
)
75+
catch_discover_tests(${UNIFIED_UNIT_TEST_TARGET} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/testbin)
6876
endif()
6977

7078
include(ystdlib-cpp-helpers)

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
ystdlib-cpp
2-
===================================
1+
# ystdlib-cpp
32
An open-source C++ library developed and used at YScope.
43

54
# Usage
5+
6+
## Via CMake's add_subdirectory()
67
Clone `ystdlib-cpp` into your project. Then, in your project's `CMakeLists.txt`, add the following:
78
```cmake
9+
# Set `YSTDLIB_CPP_BUILD_TESTING` to an accepted `FALSE` class value to skip building unit tests.
10+
# option(YSTDLIB_CPP_BUILD_TESTING "" OFF)
811
add_subdirectory(/path/to/ystdlib-cpp EXCLUDE_FROM_ALL)
912
target_link_libraries(<target_name> <link_options>
1013
ystdlib::<lib_1> ystdlib::<lib_2> ... ystdlib::<lib_N>
@@ -60,6 +63,10 @@ To build and run unit tests for a specific library:
6063
task test-<lib_name>
6164
```
6265

66+
When generating a testing target, the CMake variable `BUILD_TESTING` is followed (unless overruled
67+
by setting `YSTDLIB_CPP_BUILD_TESTING` to false). By default, if built as a top-level project,
68+
`BUILD_TESTING` is set to true and unit tests are built.
69+
6370
## Linting
6471
Before submitting a pull request, ensure you’ve run the linting commands below and have fixed all
6572
violations and suppressed all warnings.

0 commit comments

Comments
 (0)