diff options
author | Joseph Herlant <aerostitch@debian.org> | 2019-10-29 21:08:22 -0700 |
---|---|---|
committer | jkoan <jkoan@gmx.de> | 2023-01-06 09:34:37 +0100 |
commit | e88b9776fe82463990ea8c4553d56d65a32450b1 (patch) | |
tree | 0d3e7876e377144f514b6bc1b7dd7daadf46a0d3 | |
parent | 0d5abca5d7e4ca54d04b73c8199d0ba003fe88bc (diff) | |
download | navit-e88b9776fe82463990ea8c4553d56d65a32450b1.tar.gz |
add:tests:Add a way to run unit tests in Navit
-rwxr-xr-x | CMakeLists.txt | 51 | ||||
-rw-r--r-- | test/CMakeLists.txt | 10 | ||||
-rw-r--r-- | test/README.md | 7 | ||||
-rw-r--r-- | test/main.cpp | 6 | ||||
-rw-r--r-- | test/search.cpp | 15 |
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(", */ "), "*"); +} |