summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Herlant <aerostitch@debian.org>2019-10-29 21:08:22 -0700
committerjkoan <jkoan@gmx.de>2023-02-04 20:21:12 +0100
commitbafada437240515be045f31f7310024b971294a2 (patch)
tree57cea989b455131e27ef2bb3c2aa79f02f4c693c
parent659aec34320d78bd57f685edf6ff2d85f52f3757 (diff)
downloadnavit-bafada437240515be045f31f7310024b971294a2.tar.gz
add:tests:Add a way to run unit tests in Navit
-rwxr-xr-xCMakeLists.txt51
-rw-r--r--test/CMakeLists.txt10
-rw-r--r--test/README.md7
-rw-r--r--test/main.cpp6
-rw-r--r--test/search.cpp15
5 files changed, 89 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6f777ed6c..555a65bc5 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -930,3 +930,54 @@ add_subdirectory (man)
if (USE_NATIVE_LANGUAGE_SUPPORT)
add_subdirectory (po)
endif(USE_NATIVE_LANGUAGE_SUPPORT)
+
+# we will use the network to fetch Google Test sources
+# make it possible to disable unit tests when not on network
+option(ENABLE_UNIT_TESTS "Enable unit tests" ON)
+message(STATUS "Enable testing: ${ENABLE_UNIT_TESTS}")
+include(googletest.cmake)
+
+if(ENABLE_UNIT_TESTS)
+ include(ExternalProject)
+ ExternalProject_Add(
+ googletest
+ SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/googletest-src"
+ BINARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/googletest-build"
+ GIT_REPOSITORY https://github.com/google/googletest.git
+ GIT_TAG release-1.8.0
+ CONFIGURE_COMMAND ""
+ BUILD_COMMAND ""
+ INSTALL_COMMAND ""
+ TEST_COMMAND ""
+ )
+ execute_process(
+ COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
+ WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
+ )
+ execute_process(
+ COMMAND "${CMAKE_COMMAND}" --build .
+ WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
+ )
+ add_subdirectory(
+ "${CMAKE_CURRENT_SOURCE_DIR}/googletest-src"
+ "${CMAKE_CURRENT_SOURCE_DIR}/googletest-build"
+ )
+
+ add_executable(unit_tests "")
+
+ target_sources(
+ unit_tests
+ PRIVATE
+ calculator.cpp
+ main.cpp
+ )
+
+ target_link_libraries(
+ unit_tests
+ PRIVATE
+ calculator
+ gtest_main
+ )
+
+ add_test(unit ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/unit_tests)
+endif()
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 000000000..eef472b3a
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,10 @@
+file(GLOB TEST_FILES_SOURCES "*.cpp")
+
+add_executable(unit_tests ${TEST_FILES_SOURCES})
+
+target_link_libraries(unit_tests navit)
+
+add_test(
+ NAME unit
+ COMMAND ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/unit_tests
+ )
diff --git a/test/README.md b/test/README.md
new file mode 100644
index 000000000..15702e5ef
--- /dev/null
+++ b/test/README.md
@@ -0,0 +1,7 @@
+# Unit tests in Navit
+
+Unit tests in Navit are in their infancy and are totally optional.
+
+They are mostly useful for verifying non regression on tricky corner cases.
+
+We don't target to test internal functions but more plugin interface level ones.
diff --git a/test/main.cpp b/test/main.cpp
new file mode 100644
index 000000000..64becff4b
--- /dev/null
+++ b/test/main.cpp
@@ -0,0 +1,6 @@
+#include <gtest/gtest.h>
+
+int main(int argc, char **argv) {
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/test/search.cpp b/test/search.cpp
new file mode 100644
index 000000000..a5f114e0a
--- /dev/null
+++ b/test/search.cpp
@@ -0,0 +1,15 @@
+#include "search.h"
+#include <gtest/gtest.h>
+
+TEST(SearchTestSuite, search_fix_spaces) {
+ const char *in = " Hello, world/! ";
+ ASSERT_STREQ(search_fix_spaces(in), "Hello world !");
+ // Explicit cast is necessary here because cpputest runs with -Wwrite-strings which assumes ISO C++ is respected
+ char *non_const = (char *)"Spaces after string... ";
+ ASSERT_STREQ(search_fix_spaces(non_const), "Spaces after string...");
+ ASSERT_STREQ(search_fix_spaces(" Spaces before string."), "Spaces before string.");
+ ASSERT_STREQ(search_fix_spaces("Don't change a thing!"), "Don't change a thing!");
+ ASSERT_STREQ(search_fix_spaces(""), "");
+ ASSERT_STREQ(search_fix_spaces(", / "), "");
+ ASSERT_STREQ(search_fix_spaces(", */ "), "*");
+}