]> granicus.if.org Git - graphviz/commitdiff
add optional ctest & catch2 based test infrastructure
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Mon, 5 Jul 2021 09:44:15 +0000 (11:44 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Wed, 7 Jul 2021 04:53:27 +0000 (06:53 +0200)
This commit adds a CMakeLists.txt in the tests directory containing
the foundation for easily creating new test executables. It includes
creation of a shared library that the individual test executables can
be linked to, allowing them to be as small as possible.

The tests are optional and can be enabled with the CMake option
with_cxx_tests.

Also added is a first simple test case that always passes to show how
to create test cases.

CMakeLists.txt
tests/CMakeLists.txt [new file with mode: 0644]
tests/catch2_main.cpp [new file with mode: 0644]
tests/test_simple.cpp [new file with mode: 0644]

index bd8a27d5ba2418ab7e2d70a8d73686cb646377d0..1df011e2fb4b9a12e57d70c40d399e598839c901 100644 (file)
@@ -11,6 +11,7 @@ option(with_ortho      "ORTHO features in neato layout engine." ON )
 option(with_sfdp       "sfdp layout engine." ON )
 option(with_smyrna     "SMYRNA large graph viewer (disabled by default - experimental)" OFF)
 option(use_sanitizers  "enables using address and undefined behavior sanitizer" OFF)
+option(with_cxx_tests  "enables building the C++ tests" OFF)
 
 if (enable_ltdl)
         add_definitions(-DENABLE_LTDL)
@@ -224,10 +225,16 @@ include(InstallRequiredSystemLibraries)
 include(package_info)
 include(CPack)
 
+# ==================================== Test ====================================
+Include(CTest)
+
 # ======================= Specify subdirectories to build ======================
 add_subdirectory(lib)
 add_subdirectory(plugin)
 add_subdirectory(cmd)
+if (with_cxx_tests)
+    add_subdirectory(tests)
+endif()
 
 MATH(EXPR GRAPHVIZ_PLUGIN_VERSION "${GRAPHVIZ_PLUGIN_VERSION}+1")
 set(GVPLUGIN_VERSION "${GRAPHVIZ_PLUGIN_VERSION}")
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5d87a44
--- /dev/null
@@ -0,0 +1,30 @@
+cmake_minimum_required (VERSION 3.12 FATAL_ERROR)
+
+find_package(Catch2 REQUIRED)
+
+enable_testing()
+
+if (NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
+    add_compile_options(-Wextra)
+endif()
+
+# for convenience, make a library that depends on everything so each
+# separate test can be as small as possible
+add_library(test_common STATIC
+            catch2_main.cpp
+    )
+set_target_properties(test_common PROPERTIES CXX_STANDARD 20)
+set_target_properties(test_common PROPERTIES CXX_STANDARD_REQUIRED ON)
+target_link_libraries(test_common PUBLIC Catch2::Catch2)
+
+macro(create_test testname)
+    add_executable(test_${testname} test_${testname}.cpp)
+    set_target_properties(test_${testname} PROPERTIES CXX_STANDARD 20)
+    set_target_properties(test_${testname} PROPERTIES CXX_STANDARD_REQUIRED ON)
+    add_test(NAME test_${testname} COMMAND test_${testname})
+    target_link_libraries(test_${testname} PRIVATE
+        test_common
+    )
+endmacro()
+
+create_test(simple)
diff --git a/tests/catch2_main.cpp b/tests/catch2_main.cpp
new file mode 100644 (file)
index 0000000..f481b16
--- /dev/null
@@ -0,0 +1,3 @@
+#define CATCH_CONFIG_MAIN
+
+#include <catch2/catch.hpp>
diff --git a/tests/test_simple.cpp b/tests/test_simple.cpp
new file mode 100644 (file)
index 0000000..f01f5c4
--- /dev/null
@@ -0,0 +1,4 @@
+#include <catch2/catch.hpp>
+
+TEST_CASE("always pass") {
+}