Skip to content

Commit

Permalink
Add basic Catch2 setup for unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Sep 23, 2022
1 parent c46bded commit 2e02914
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: cvmfs-contrib/github-action-cvmfs@v2
- uses: aidasoft/run-lcg-view@v3
with:
coverity-cmake-command: 'cmake -C $ILCSOFT/ILCSoft.cmake ..'
coverity-cmake-command: 'cmake -C $ILCSOFT/ILCSoft.cmake -DBUILD_TESTING=OFF ..'
coverity-project: 'iLCSoft%2FMarlinUtil'
coverity-project-token: ${{ secrets.MARLINUTIL_COVERITY_TOKEN }}
github-pat: ${{ secrets.READ_COVERITY_IMAGE }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
run: |
mkdir build
cd build
cmake -GNinja -C ${ILCSOFT}/ILCSoft.cmake -DCMAKE_CXX_FLAGS=" -fdiagnostics-color=always " -DINSTALL_DOC=ON ..
cmake -GNinja -C ${ILCSOFT}/ILCSoft.cmake -DCMAKE_CXX_FLAGS=" -fdiagnostics-color=always " -DINSTALL_DOC=ON -DUSE_EXTERNAL_CATCH2=OFF ..
ninja -k0
ctest --output-on-failure
ninja install
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ SET_SOURCE_FILES_PROPERTIES( "./source/src/ann/kd_pr_search.cpp" PROPERTIES COMP
#ADD_SHARED_LIBRARY( ${PROJECT_NAME}_ann ${ann_library_sources} )
#INSTALL_SHARED_LIBRARY( ${PROJECT_NAME}_ann DESTINATION lib )

option(USE_EXTERNAL_CATCH2 "Link against an external Catch2 v3 static library, otherwise build it locally" ON)

IF( BUILD_TESTING )
ADD_SUBDIRECTORY(source/tests)
Expand Down
39 changes: 39 additions & 0 deletions source/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,42 @@ MACRO( ADD_MARLINUTIL_TEST _name )
ENDMACRO()

ADD_MARLINUTIL_TEST( testmarlinutil )

#--- Unit test setup. Use existing Catch2 or fetch a suitable version and build it
if(USE_EXTERNAL_CATCH2)
FIND_PACKAGE(Catch2 3.0.0 REQUIRED)
else()
MESSAGE(STATUS "Fetching local copy of Catch2 library for unit-tests...")
# Build Catch2 with the default flags, to avoid generating warnings when we
# build it
SET(CXX_FLAGS_CMAKE_USED ${CMAKE_CXX_FLAGS})
SET(CMAKE_CXX_FLAGS ${CXX_FLAGS_CMAKE_DEFAULTS})
INCLUDE(FetchContent)
fETCHCONTENT_DECLARE(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.1
)
FETCHCONTENT_MAKEAVAILABLE(Catch2)
set(CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras ${CMAKE_MODULE_PATH})

# Disable clang-tidy on external contents
SET_TARGET_PROPERTIES(Catch2 PROPERTIES CXX_CLANG_TIDY "")

# Hack around the fact, that the include directories are not declared as
# SYSTEM for the targets defined this way. Otherwise warnings can still occur
# in Catch2 code when templates are evaluated (which happens quite a bit)
GET_TARGET_PROPERTY(CATCH2_IF_INC_DIRS Catch2 INTERFACE_INCLUDE_DIRECTORIES)
SET_TARGET_PROPERTIES(Catch2 PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${CATCH2_IF_INC_DIRS}")

# Reset the flags
SET(CMAKE_CXX_FLAGS ${CXX_FLAGS_CMAKE_USED})
endif()

INCLUDE(Catch)

ADD_EXECUTABLE(unittests unittests/TestHelixClass.cpp)
TARGET_LINK_LIBRARIES(unittests PUBLIC ${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain)
CATCH_DISCOVER_TESTS(unittests
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
20 changes: 20 additions & 0 deletions source/tests/unittests/TestHelixClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "HelixClass.h"
#include "HelixClass_double.h"

#include <catch2/catch_approx.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

#include <tuple>

TEST_CASE("Example") {
REQUIRE(true); // obviously
REQUIRE(2 == (1 + 1)); // also quite expected
REQUIRE(0.1 + 0.2 == Catch::Approx(0.3)); // The famous IEEE
}

using HelixTypes = std::tuple<HelixClass, HelixClass_double>;

TEMPLATE_LIST_TEST_CASE("template list example", "", HelixTypes) {
REQUIRE(true); // This will be executed for each HelixType
}

0 comments on commit 2e02914

Please sign in to comment.