Skip to content

Commit

Permalink
build: add test coverage
Browse files Browse the repository at this point in the history
issue #3
  • Loading branch information
cthulhu-irl committed Jul 28, 2023
1 parent dc065b5 commit 333f3d8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
22 changes: 18 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ if (NOT DEFINED PARSI_MAIN_PROJECT)
endif()

project(parsi
DESCRIPTION "lightweight simple parser combinator"
DESCRIPTION "lightweight parser combinator"
LANGUAGES CXX)

option(PARSI_TESTS "build tests" ${PARSI_MAIN_PROJECT})
option(PARSI_ENABLE_COVERAGE "enable code coverage" ${PARSI_MAIN_PROJECT})
option(PARSI_DOCS "build docs" ${PARSI_MAIN_PROJECT})
option(PARSI_EXAMPLES "build examples" ${PARSI_MAIN_PROJECT})
option(PARSI_BENCHMARK "build benchmarks" OFF)
Expand All @@ -25,7 +26,7 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)

if (NOT DEFINED CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Debug)
endif()

if (NOT DEFINED CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
Expand All @@ -45,9 +46,22 @@ target_compile_features(${PROJECT_NAME}-options INTERFACE cxx_std_20)
target_include_directories(${PROJECT_NAME}-options
INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
$<INSTALL_INTERFACE:include>
)

if (PARSI_ENABLE_COVERAGE)
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
message(WARNING "code-coverage with non-Debug build is inaccurate")
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(${PROJECT_NAME}-options INTERFACE --coverage -O0 -g)
target_link_libraries(${PROJECT_NAME}-options INTERFACE --coverage)
message(STATUS "added compiler flags to generate coverage report")
else()
message(FATAL_ERROR "no known rule to add coverage to compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()
endif()

# TODO decide whether to go header-only or not.
add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}-options)
Expand Down
10 changes: 10 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@
"displayName": "Linux x64 Release",
"configurePreset": "linux-x64-release"
},
{
"name": "linux-x64-debug",
"displayName": "Linux x64 Debug",
"configurePreset": "linux-x64-debug"
},
{
"name": "Linux-x64-release",
"displayName": "Linux x64 Release",
"configurePreset": "linux-x64-release"
},
{
"name": "android-arm-debug",
"displayName": "Android ARM32 Debug",
Expand Down
4 changes: 4 additions & 0 deletions include/parsi/parsi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

namespace parsi {

inline bool sample() {
return true;
}

} // namespace parsi

#endif // PARSI_PARSI_HPP
Expand Down
27 changes: 26 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ target_link_libraries(${PROJECT_NAME}-tests
${PROJECT_NAME}
${PROJECT_NAME}-options
Catch2::Catch2
Catch2::Catch2WithMain)
Catch2::Catch2WithMain
)

list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)

Expand All @@ -19,3 +20,27 @@ include(Catch)
if (NOT ANDROID)
catch_discover_tests(${PROJECT_NAME}-tests)
endif()

if (PARSI_ENABLE_COVERAGE)
find_program(GCOVR_PROGRAM gcovr REQUIRED)

set(COVERAGE_DIR ${PROJECT_BINARY_DIR}/coverage)

add_custom_target(coverage
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory ${COVERAGE_DIR}
COMMAND ctest --output-on-failure
COMMAND ${GCOVR_PROGRAM}
-r "${PROJECT_SOURCE_DIR}/include"
--print-summary
--fail-under-line 70
--fail-under-branch 70
--html "${COVERAGE_DIR}/index.html" --html-details
--xml "${COVERAGE_DIR}/coverage.xml"
--json "${COVERAGE_DIR}/coverage.json"
--object-directory=${PROJECT_BINARY_DIR}
DEPENDS ${PROJECT_NAME}-tests
BYPRODUCTS ${COVERAGE_DIR}/coverage.xml
VERBATIM
)
endif()
5 changes: 3 additions & 2 deletions tests/sample.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <catch2/catch_all.hpp>

#include "parsi/parsi.hpp"

TEST_CASE("sample") {
CHECK(true);
CHECK(parsi::sample());
}

0 comments on commit 333f3d8

Please sign in to comment.