Skip to content

Commit

Permalink
Add GTest with FetchContent (#120)
Browse files Browse the repository at this point in the history
* Add missing GTest linking
* Add unit testing switch
* Rename UNIT_TESTING to be fff-specific
* Add fff.h generation toggle
* Use options instead of variables for disabling build elements
* Add interface library for when the header is not regenerated
* Update build script and README
  • Loading branch information
jakub-dudarewicz authored May 22, 2023
1 parent a9cb716 commit 5111c61
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 28,717 deletions.
48 changes: 30 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,41 @@ project(fff)

set(CMAKE_CXX_STANDARD 11)

# Add the gtest library which will be used below
add_subdirectory(gtest)

# Enable ctest
enable_testing()

add_library(fff INTERFACE)

option(FFF_GENERATE "If enabled, fff.h will be regenerated using ruby" OFF)

# Generate fff.h if fakegen.rb changed
add_custom_command(
OUTPUT
${CMAKE_CURRENT_LIST_DIR}/fff.h
COMMAND
ruby ${CMAKE_CURRENT_LIST_DIR}/fakegen.rb >> ${CMAKE_CURRENT_LIST_DIR}/fff.h
DEPENDS
${CMAKE_CURRENT_LIST_DIR}/fakegen.rb
${CMAKE_CURRENT_LIST_DIR}/LICENSE
)
add_custom_target(fff_h DEPENDS ${CMAKE_CURRENT_LIST_DIR}/fff.h)
if(FFF_GENERATE)
add_custom_command(
OUTPUT
${CMAKE_CURRENT_LIST_DIR}/fff.h
COMMAND
ruby ${CMAKE_CURRENT_LIST_DIR}/fakegen.rb >> ${CMAKE_CURRENT_LIST_DIR}/fff.h
DEPENDS
${CMAKE_CURRENT_LIST_DIR}/fakegen.rb
${CMAKE_CURRENT_LIST_DIR}/LICENSE
)
add_custom_target(fff_h DEPENDS ${CMAKE_CURRENT_LIST_DIR}/fff.h)
else()
add_library(fff_h INTERFACE)
set_target_properties(fff_h
PROPERTIES PUBLIC_HEADER "fff.h"
)
endif()

# Add an interface library for fff.h
add_library(fff INTERFACE)
add_dependencies(fff fff_h)

# Add an interface library for fff.h
target_include_directories(fff INTERFACE ${CMAKE_CURRENT_LIST_DIR})

# Add tests and samples
add_subdirectory(test)
add_subdirectory(examples)
option(FFF_UNIT_TESTING "If enabled, fff tests will be compiled and run" OFF)

if(FFF_UNIT_TESTING)
# Add tests and samples
add_subdirectory(test)
add_subdirectory(examples)
endif()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To run all the tests and sample apps, simply call `$ buildandtest`. This script
will call down into CMake with the following:

```shell
cmake -GNinja -B build
cmake -B build -DFFF_GENERATE=ON -DFFF_UNIT_TESTING=ON
cmake --build build
ctest --test-dir build --output-on-failure
```
Expand Down
2 changes: 1 addition & 1 deletion buildandtest
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rm -fr build fff.h
mkdir build

# Configure the build
cmake -GNinja -B build || exit -1
cmake -B build -DFFF_GENERATE=ON -DFFF_UNIT_TESTING=ON || exit -1

# Build all targets
cmake --build build || exit -1
Expand Down
4 changes: 2 additions & 2 deletions examples/driver_testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_executable(driver_test
src/driver.test.cpp
)
target_include_directories(driver_test PRIVATE include)
target_link_libraries(driver_test PRIVATE gtest fff)
target_link_libraries(driver_test PRIVATE GTest::gtest_main fff)
target_compile_definitions(driver_test PUBLIC TEST_USER_OWN_TR1_TUPLE=1 TESTING)

# Create the driver fff test binary
Expand All @@ -16,7 +16,7 @@ add_executable(driver_fff_test
src/driver.test.fff.cpp
)
target_include_directories(driver_fff_test PRIVATE include)
target_link_libraries(driver_fff_test PRIVATE gtest fff)
target_link_libraries(driver_fff_test PRIVATE GTest::gtest_main fff)
target_compile_definitions(driver_fff_test PUBLIC TEST_USER_OWN_TR1_TUPLE=1 TESTING)

# Add tests to ctest
Expand Down
2 changes: 1 addition & 1 deletion examples/embedded_ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ target_link_libraries(ui_test_ansic PRIVATE fff)
# Create the ui_test_cpp test binary
add_executable(ui_test_cpp src/UI_test_cpp.cpp src/UI.c)
target_include_directories(ui_test_cpp PRIVATE include)
target_link_libraries(ui_test_cpp PRIVATE gtest fff)
target_link_libraries(ui_test_cpp PRIVATE GTest::gtest_main fff)

# Add tests to ctest
add_test(
Expand Down
12 changes: 0 additions & 12 deletions gtest/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 5111c61

Please sign in to comment.