]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Build and run the tests as part of the cmake build.
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 23 Nov 2019 20:15:48 +0000 (15:15 -0500)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 23 Nov 2019 20:15:48 +0000 (15:15 -0500)
CMakeLists.txt
README.md
tests/CMakeLists.txt [new file with mode: 0644]

index ffb1db3dc4eddc6d2af46b2c2827acabb0645069..8a47ed3cc5f7448291c6a004eb351c6830af81e6 100644 (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}
index 0580878d19cddd56ef8c44be4310c5fbdc71516c..dcc98dcc64b037c24c1dd7bc4c5e9885ce8d27b2 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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">
 ----------------------
 
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d452017
--- /dev/null
@@ -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)
+