From df7267e5df9062ae37c6577affd9424c715f5817 Mon Sep 17 00:00:00 2001 From: Andrey Derzhavin Date: Mon, 17 Mar 2025 21:19:08 +0300 Subject: [PATCH] [infra] Support unit-testing via GTest Closes #10 --- .github/workflows/hsim.yml | 2 +- CMakeLists.txt | 3 +++ test/CMakeLists.txt | 1 + test/unit/CMakeLists.txt | 20 ++++++++++++++++++++ test/unit/hello/CMakeLists.txt | 1 + test/unit/hello/hello.cc | 3 +++ third_party/CMakeLists.txt | 14 ++++++++++++++ 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/CMakeLists.txt create mode 100644 test/unit/CMakeLists.txt create mode 100644 test/unit/hello/CMakeLists.txt create mode 100644 test/unit/hello/hello.cc diff --git a/.github/workflows/hsim.yml b/.github/workflows/hsim.yml index 7e7bb1d..264ec60 100644 --- a/.github/workflows/hsim.yml +++ b/.github/workflows/hsim.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - path: ['src', 'include'] + path: ['src', 'include', 'test'] steps: - name: Checkout repo and submodules uses: actions/checkout@v4 diff --git a/CMakeLists.txt b/CMakeLists.txt index 3434e5f..fb9dd61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,3 +28,6 @@ add_executable(${PROJECT_NAME} src/main.cc) target_compile_options(${PROJECT_NAME} PRIVATE -Wpedantic) target_link_libraries(${PROJECT_NAME} PRIVATE hSim-lib CLI11::CLI11 fmt::fmt) set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME hsim) + +enable_testing() +add_subdirectory(test) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..269aea0 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(unit) diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt new file mode 100644 index 0000000..8bd1cf0 --- /dev/null +++ b/test/unit/CMakeLists.txt @@ -0,0 +1,20 @@ +include(GoogleTest) + +# Helpful macro to add unittest +# Accepts src_file (with utest) as mandatory argument +# and list of linked libraries after it +macro(hsim_add_utest src_file) + set(TEST_NAME "${src_file}_test") + add_executable(${TEST_NAME} ${src_file}) + + target_link_libraries(${TEST_NAME} PRIVATE GTest::gtest_main hSIM::defaults) + foreach(arg IN LISTS ARGN) + target_link_libraries(${TEST_NAME} PRIVATE ${arg}) + endforeach() + gtest_discover_tests( + ${TEST_NAME} + EXTRA_ARGS --gtest_color=yes + PROPERTIES LABELS unit) +endmacro() + +add_subdirectory(hello) diff --git a/test/unit/hello/CMakeLists.txt b/test/unit/hello/CMakeLists.txt new file mode 100644 index 0000000..530a80f --- /dev/null +++ b/test/unit/hello/CMakeLists.txt @@ -0,0 +1 @@ +hsim_add_utest(hello.cc) diff --git a/test/unit/hello/hello.cc b/test/unit/hello/hello.cc new file mode 100644 index 0000000..36886cc --- /dev/null +++ b/test/unit/hello/hello.cc @@ -0,0 +1,3 @@ +#include + +TEST(Simple, Hello) { ASSERT_TRUE(true); } diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index e6b7ef9..9f4bfa4 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -21,3 +21,17 @@ CPMAddPackage( GIT_TAG Release_3.12 EXCLUDE_FROM_ALL True SYSTEM True) + + +# gtest +CPMAddPackage( + NAME + googletest + GITHUB_REPOSITORY + google/googletest + VERSION + 1.16.0 + OPTIONS + "INSTALL_GTEST OFF" + EXCLUDE_FROM_ALL True + SYSTEM True)