Skip to content

Commit 5cdb4f7

Browse files
[infra] Support unit-testing via GTest (#17)
Closes #10
1 parent 34d1f72 commit 5cdb4f7

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
lines changed

.github/workflows/hsim.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
path: ['src', 'include']
18+
path: ['src', 'include', 'test']
1919
steps:
2020
- name: Checkout repo and submodules
2121
uses: actions/checkout@v4

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ add_executable(${PROJECT_NAME} src/main.cc)
2828
target_compile_options(${PROJECT_NAME} PRIVATE -Wpedantic)
2929
target_link_libraries(${PROJECT_NAME} PRIVATE hSim-lib CLI11::CLI11 fmt::fmt)
3030
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME hsim)
31+
32+
enable_testing()
33+
add_subdirectory(test)

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(unit)

test/unit/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
include(GoogleTest)
2+
3+
# Helpful macro to add unittest
4+
# Accepts src_file (with utest) as mandatory argument
5+
# and list of linked libraries after it
6+
macro(hsim_add_utest src_file)
7+
set(TEST_NAME "${src_file}_test")
8+
add_executable(${TEST_NAME} ${src_file})
9+
10+
target_link_libraries(${TEST_NAME} PRIVATE GTest::gtest_main hSIM::defaults)
11+
foreach(arg IN LISTS ARGN)
12+
target_link_libraries(${TEST_NAME} PRIVATE ${arg})
13+
endforeach()
14+
gtest_discover_tests(
15+
${TEST_NAME}
16+
EXTRA_ARGS --gtest_color=yes
17+
PROPERTIES LABELS unit)
18+
endmacro()
19+
20+
add_subdirectory(hello)

test/unit/hello/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hsim_add_utest(hello.cc)

test/unit/hello/hello.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <gtest/gtest.h>
2+
3+
TEST(Simple, Hello) { ASSERT_TRUE(true); }

third_party/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ CPMAddPackage(
2121
GIT_TAG Release_3.12
2222
EXCLUDE_FROM_ALL True
2323
SYSTEM True)
24+
25+
26+
# gtest
27+
CPMAddPackage(
28+
NAME
29+
googletest
30+
GITHUB_REPOSITORY
31+
google/googletest
32+
VERSION
33+
1.16.0
34+
OPTIONS
35+
"INSTALL_GTEST OFF"
36+
EXCLUDE_FROM_ALL True
37+
SYSTEM True)

0 commit comments

Comments
 (0)