Build and run the tests as part of the cmake build.

This commit is contained in:
Eric Haszlakiewicz
2019-11-23 15:15:48 -05:00
parent 25aedddcdf
commit bdaff94e9a
3 changed files with 103 additions and 3 deletions

View File

@@ -15,6 +15,12 @@ if(POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
include(CTest)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
add_subdirectory(tests)
endif()
# Set some packaging variables.
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VERSION_MAJOR "0")
@@ -183,6 +189,11 @@ elseif ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4701")
endif()
if ($ENV{VALGRIND})
# Build so that valgrind doesn't complain about linkhash.c
add_definitions(-DVALGRIND=1)
endif()
set(JSON_C_PUBLIC_HEADERS
${PROJECT_BINARY_DIR}/config.h
${PROJECT_BINARY_DIR}/json_config.h
@@ -232,9 +243,9 @@ set(JSON_C_SOURCES
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_BINARY_DIR})
# If -DBUILD_SHARED_LIBS is set in the CMake command-line, we'll be able to create shared libs, otherwise this would
# generate static libs. Good enough for most use-cases unless there's some serious requirement to create both
# simultaneously.
# XXX for a normal full distribution we'll need to figure out
# XXX how to build both shared and static libraries.
# Probably leverage that to build a local VALGRIND=1 library for testing too.
add_library(${PROJECT_NAME}
${JSON_C_SOURCES}
${JSON_C_HEADERS}

View File

@@ -174,6 +174,41 @@ Pass these options as `-D` on CMake's command-line.
cmake -DBUILD_SHARED_LIBS=OFF ...
```
Testing with cmake:
By default, if valgrind is available running tests uses it.
That can slow the tests down considerably, so to disable it use:
```sh
export USE_VALGRIND=0
```
To run tests:
```sh
mkdir build-test
cd build-test
# VALGRIND=1 causes -DVALGRIND=1 to be included when building
VALGRIND=1 cmake ..
make
make test
# By default, if valgrind is available running tests uses it.
make USE_VALGRIND=0 test # optionally skip using valgrind
```
If a test fails, check `Testing/Temporary/LastTest.log`,
`tests/testSubDir/${testname}/${testname}.vg.out`, and other similar files.
If there is insufficient output try:
```sh
VERBOSE=1 make test
```
or
```sh
JSONC_TEST_TRACE=1 make test
```
and check the log files again.
Linking to `libjson-c` <a name="linking">
----------------------

54
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,54 @@
add_executable(test1Formatted test1.c parse_flags.c parse_flags.h)
target_compile_definitions(test1Formatted PRIVATE TEST_FORMATTED=1)
target_link_libraries(test1Formatted PRIVATE ${PROJECT_NAME})
add_executable(test2Formatted test2.c parse_flags.c parse_flags.h)
target_compile_definitions(test2Formatted PRIVATE TEST_FORMATTED=1)
target_link_libraries(test2Formatted PRIVATE ${PROJECT_NAME})
# https://cmake.org/cmake/help/v3.0/command/add_test.html
# https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
include_directories(PUBLIC ${CMAKE_SOURCE_DIR})
foreach(TESTNAME
test1
test2
test4
testReplaceExisting
test_cast
test_charcase
test_compare
test_deep_copy
test_double_serializer
test_float
test_int_add
test_json_pointer
test_locale
test_null
test_parse
test_parse_int64
test_printbuf
test_set_serializer
test_set_value
test_util_file
test_visit)
add_executable(${TESTNAME} ${TESTNAME}.c)
add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test)
# XXX using the non-target_ versions of these doesn't work :(
target_include_directories(
${TESTNAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(
${TESTNAME}
PRIVATE
${PROJECT_NAME}
)
endforeach(TESTNAME)