Skip to content

Commit

Permalink
Merge pull request #129 from ndml28/fix-tests
Browse files Browse the repository at this point in the history
enable tests by default and always include googletest
  • Loading branch information
vornkat-iis authored Aug 1, 2024
2 parents 90f3f5d + 8ae4881 commit f124790
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 51 deletions.
84 changes: 35 additions & 49 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)

# 'cmake -DCMAKE_BUILD_TYPE=Debug ../' from build folder for debug output #
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_BUILD_TYPE "Release")
endif()

file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*)
Expand All @@ -29,12 +29,11 @@ target_include_directories(japi_objs PUBLIC include/ ${JSONC_INCLUDE_DIRS})
set_property(TARGET japi_objs PROPERTY POSITION_INDEPENDENT_CODE ON)

# set up actual libraries
add_library(japi SHARED $<TARGET_OBJECTS:japi_objs>)
add_library(japi SHARED $<TARGET_OBJECTS:japi_objs>)
add_library(japi-static STATIC $<TARGET_OBJECTS:japi_objs>)

set_target_properties(japi PROPERTIES
PUBLIC_HEADER "${PUBLIC_HEADERS}"
SOVERSION ${SOVERSION})
set_target_properties(japi PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}"
SOVERSION ${SOVERSION})

target_link_libraries(japi PkgConfig::JSONC)
target_link_libraries(japi-static PkgConfig::JSONC)
Expand All @@ -54,8 +53,10 @@ install(TARGETS japi
find_package(Doxygen)

if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxydir/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(doc
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxydir/Doxyfile.in
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(
doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
Expand All @@ -64,59 +65,44 @@ endif(DOXYGEN_FOUND)

################################
# Create unit test excecutable #
find_package(GTest QUIET)
if(${GTest_FOUND})
# Gtest on System Installed
message("GTest: Installed using GTest")
include(GoogleTest)
else()
option(LIBJAPI_ENABLE_TESTING "Enable testing with googletest" ON)
if(LIBJAPI_ENABLE_TESTING)
enable_testing()

# Include GoogleTest in the project
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.1
)
GIT_TAG release-1.8.0)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
endif()

# Define test executable
add_executable(testsuite
EXCLUDE_FROM_ALL
test/japi_test.cc
)

target_compile_options(testsuite PUBLIC "-pthread")
# Define test executable
add_executable(testsuite test/japi_test.cc)

target_link_libraries(testsuite
gtest_main
japi
)
target_compile_options(testsuite PUBLIC "-pthread")

# Include directories
target_include_directories(testsuite
PUBLIC
include/
src/
${JSONC_INCLUDE_DIRS}
)
target_link_libraries(testsuite gtest_main japi)

add_custom_target(run_test COMMAND testsuite DEPENDS testsuite)
# Include directories
target_include_directories(testsuite PUBLIC include/ src/
${JSONC_INCLUDE_DIRS})

gtest_discover_tests(testsuite)

################################
# Test code coverage #

if(CMAKE_BUILD_TYPE MATCHES "Debug")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)
set(COVERAGE_EXCLUDES
"doxydir"
"/usr/include/*"
"googletest/*"
"test/*"
)
append_coverage_compiler_flags()

setup_target_for_coverage_lcov(NAME coverage EXECUTABLE testsuite)
endif()
if(CMAKE_BUILD_TYPE MATCHES "Debug")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake)
set(COVERAGE_EXCLUDES
"doxydir"
"/usr/include"
"_deps/googletest"
"test/*"
)
append_coverage_compiler_flags()
setup_target_for_coverage_lcov(NAME coverage EXECUTABLE testsuite)
endif()
endif(LIBJAPI_ENABLE_TESTING)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ A Makefile is generated. Run 'make' to build the libjapi libraries.

$ make

A shared and a static library is built.
A shared and a static library is built. Tests are built by default. They can be disabled using the command `cmake -DLIBJAPI_ENABLE_TESTING=FALSE ../` variable.

To run the internal tests run

$ make run_test
$ ctest

## Demo
You can clone the [demo project](https://git01.iis.fhg.de/ks-ip-lib/software/libjapi-demo), with examples for all features from the repository listed below:
Expand Down

0 comments on commit f124790

Please sign in to comment.