Skip to content

Commit dde5732

Browse files
build: Add Catch2 dependency. (#25)
Co-authored-by: davidlion <davidlion2@protonmail.com>
1 parent 2e0af59 commit dde5732

12 files changed

Lines changed: 99 additions & 48 deletions

File tree

.github/workflows/unit-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ jobs:
6363
6464
# TODO: Change the task to run all unittests once any unittest is added. Until then we check
6565
# that building the project succeeds.
66-
- run: "task build:target"
66+
- run: "task build:all"

CMakeLists.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS
1111
FORCE
1212
)
1313

14+
find_package(Catch2 3.8.0 REQUIRED)
15+
if(Catch2_FOUND)
16+
message(STATUS "Found Catch2 ${Catch2_VERSION}.")
17+
else()
18+
message(FATAL_ERROR "Could not find libraries for Catch2.")
19+
endif()
20+
1421
# Import CMake helper functions
1522
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/CMake)
1623
include(ystdlib-cpp-helpers)
1724

1825
add_subdirectory(src/ystdlib)
1926

20-
# Test dummy project
21-
add_executable(dummy)
22-
target_sources(dummy PRIVATE src/main.cpp)
23-
target_link_libraries(dummy PRIVATE ystdlib::testlib)
24-
target_compile_features(dummy PRIVATE cxx_std_20)
27+
add_executable(unitTest)
28+
target_sources(unitTest PRIVATE src/main.cpp)
29+
target_link_libraries(
30+
unitTest
31+
PRIVATE
32+
ystdlib::testlib
33+
Catch2::Catch2WithMain
34+
)
35+
target_compile_features(unitTest PRIVATE cxx_std_20)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ git submodule update --init --recursive
3030
## Building
3131
To build all targets in `ystdlib-cpp`:
3232
```shell
33-
task build:target
33+
task build:all
3434
```
3535

3636
## Linting

src/.clang-format

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
BasedOnStyle: "InheritParentConfig"
2+
3+
IncludeCategories:
4+
# NOTE: A header is grouped by first matching regex library headers. Update when adding new
5+
# libraries.
6+
# NOTE: clang-format retains leading white-space on a line in violation of the YAML spec.
7+
- Regex: "<(ystdlib)"
8+
Priority: 3
9+
- Regex: "<(catch2)"
10+
Priority: 4
11+
# C system headers
12+
- Regex: "^<.+\\.h>"
13+
Priority: 1
14+
# C++ standard libraries
15+
- Regex: "^<.+>"
16+
Priority: 2
17+
# Project headers
18+
- Regex: "^\".+\""
19+
Priority: 5

src/main.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#include <concepts>
2-
#include <iostream>
2+
33
#include <ystdlib/testlib/hello.hpp>
44

5+
#include <catch2/catch_test_macros.hpp>
6+
57
namespace {
68
template <typename T>
79
requires std::integral<T>
10+
811
[[nodiscard]] auto square(T x) -> T {
912
return x * x;
1013
}
1114
}; // namespace
1215

13-
[[nodiscard]] auto main() -> int {
14-
std::cout << square(ystdlib::testlib::hello().size()) << '\n';
15-
return 0;
16+
TEST_CASE("dummy") {
17+
REQUIRE((169 == square(ystdlib::testlib::hello().size())));
1618
}

taskfile.yaml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,21 @@ version: "3"
22

33
includes:
44
build: "./taskfiles/build.yaml"
5+
deps: "./taskfiles/deps.yaml"
56
lint: "./taskfiles/lint.yaml"
6-
utils: "tools/yscope-dev-utils/taskfiles/utils.yml"
7+
utils: "tools/yscope-dev-utils/taskfiles/utils.yaml"
78

89
vars:
910
G_BUILD_DIR: "{{.ROOT_DIR}}/build"
10-
G_CMAKE_CACHE: "{{.G_BUILD_DIR}}/CMakeCache.txt"
11-
G_COMPILE_COMMANDS_DB: "{{.G_BUILD_DIR}}/compile_commands.json"
1211
G_CPP_SRC_DIR: "{{.ROOT_DIR}}/src"
12+
G_DEPS_DIR: "{{.G_BUILD_DIR}}/deps"
1313

1414
tasks:
1515
clean:
1616
desc: "Removes the project build directory."
1717
cmds:
1818
- "rm -rf '{{.G_BUILD_DIR}}'"
1919

20-
config-cmake-project:
21-
internal: true
22-
sources:
23-
- "CMakeLists.txt"
24-
- "{{.TASKFILE}}"
25-
generates:
26-
- "{{.G_CMAKE_CACHE}}"
27-
- "{{.G_COMPILE_COMMANDS_DB}}"
28-
cmds:
29-
- "cmake -S '{{.ROOT_DIR}}' -B '{{.G_BUILD_DIR}}'"
30-
3120
init:
3221
internal: true
3322
silent: true

taskfiles/build.yaml

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
version: "3"
22

3+
vars:
4+
G_CMAKE_CACHE: "{{.G_BUILD_DIR}}/CMakeCache.txt"
5+
G_CMAKE_CONF_ARGS:
6+
- "-DCatch2_ROOT={{.G_DEPS_DIR}}/Catch2/Catch2-install"
7+
G_COMPILE_COMMANDS_DB: "{{.G_BUILD_DIR}}/compile_commands.json"
8+
39
tasks:
4-
target:
10+
all:
511
desc: "Builds ystdlib-cpp."
6-
vars:
7-
TARGETS:
8-
ref: "default (list \"all\") .TARGETS"
912
deps:
10-
- ":config-cmake-project"
13+
- "init"
1114
cmds:
12-
- >-
13-
cmake
14-
--build "{{.G_BUILD_DIR}}"
15-
--parallel {{numCPU}}
16-
--target {{range .TARGETS}}{{.}} {{end}}
15+
- task: ":utils:cmake-build"
16+
vars:
17+
BUILD_DIR: "{{.G_BUILD_DIR}}"
1718

1819
clean:
1920
desc: "Removes all built artifacts."
2021
deps:
21-
- ":config-cmake-project"
22+
- task: ":utils:cmake-clean"
23+
vars:
24+
BUILD_DIR: "{{.G_BUILD_DIR}}"
25+
26+
init:
27+
internal: true
28+
deps:
29+
- ":deps:install-all"
30+
run: "once"
2231
cmds:
23-
- >-
24-
cmake
25-
--build "{{.G_BUILD_DIR}}"
26-
--parallel {{numCPU}}
27-
--target clean
32+
- task: ":utils:cmake-generate"
33+
vars:
34+
BUILD_DIR: "{{.G_BUILD_DIR}}"
35+
EXTRA_ARGS:
36+
ref: ".G_CMAKE_CONF_ARGS"
37+
SOURCE_DIR: "{{.ROOT_DIR}}"

taskfiles/deps.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "3"
2+
3+
tasks:
4+
install-all:
5+
desc: "Install all dependencies required by ystdlib-cpp."
6+
deps:
7+
- "install-Catch2"
8+
9+
install-Catch2:
10+
internal: true
11+
vars:
12+
NAME: "Catch2"
13+
WORK_DIR: "{{.G_DEPS_DIR}}/{{.NAME}}"
14+
run: "once"
15+
cmds:
16+
- task: ":utils:cmake-install-remote-tar"
17+
vars:
18+
NAME: "{{.NAME}}"
19+
WORK_DIR: "{{.WORK_DIR}}"
20+
FILE_SHA256: "1ab2de20460d4641553addfdfe6acd4109d871d5531f8f519a52ea4926303087"
21+
URL: "https://github.com/catchorg/Catch2/archive/refs/tags/v3.8.0.tar.gz"

taskfiles/lint-cpp.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ tasks:
6565
- "{{.ROOT_DIR}}/.clang-tidy"
6666
- "{{.TASKFILE}}"
6767
deps:
68-
- ":config-cmake-project"
68+
- ":build:init"
6969
- "cpp-configs"
7070
- "venv"
7171
cmds:

taskfiles/lint-venv.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tasks:
1818
- task: ":utils:validate-checksum"
1919
vars:
2020
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
21-
DATA_DIR: "{{.OUTPUT_DIR}}"
21+
INCLUDE_PATTERNS: ["{{.OUTPUT_DIR}}"]
2222
cmds:
2323
- task: ":utils:create-venv"
2424
vars:
@@ -28,5 +28,5 @@ tasks:
2828
# This command must be last
2929
- task: ":utils:compute-checksum"
3030
vars:
31-
DATA_DIR: "{{.OUTPUT_DIR}}"
32-
OUTPUT_FILE: "{{.CHECKSUM_FILE}}"
31+
CHECKSUM_FILE: "{{.CHECKSUM_FILE}}"
32+
INCLUDE_PATTERNS: ["{{.OUTPUT_DIR}}"]

0 commit comments

Comments
 (0)