diff --git a/.gitmodules b/.gitmodules index 9c5ac2d51..ee26c1926 100644 --- a/.gitmodules +++ b/.gitmodules @@ -28,3 +28,6 @@ [submodule "depends/luacov"] path = depends/luacov url = ../../DFHack/luacov.git +[submodule "depends/googletest"] + path = depends/googletest + url = https://github.com/google/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index caf53c2a0..40dc64b8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -426,6 +426,39 @@ if(NOT GIT_FOUND) message(SEND_ERROR "could not find git") endif() +# Testing with CTest +include(CTest) + +include(CMakeDependentOption) +cmake_dependent_option( + BUILD_TEST_SCRIPTS "Install integration tests in hack/scripts/test" OFF + "BUILD_TESTING" OFF) +mark_as_advanced(FORCE BUILD_TESTS) + +# Handle deprecated BUILD_TESTS option +option(BUILD_TESTS "Deprecated option; please use BUILD_TEST_SCRIPTS=ON" OFF) +if(BUILD_TESTING AND BUILD_TESTS) + set(BUILD_TEST_SCRIPTS ON FORCE) +endif() + +if(BUILD_TESTING) + include_directories(depends/googletest/googletest/include) + if(BUILD_TEST_SCRIPTS) + if(EXISTS "${dfhack_SOURCE_DIR}/test/scripts") + message(SEND_ERROR "test/scripts must not exist in the dfhack repo since it would conflict with the tests installed from the scripts repo.") + endif() + install(DIRECTORY ${dfhack_SOURCE_DIR}/test + DESTINATION ${DFHACK_DATA_DESTINATION}/scripts) + install(FILES ci/test.lua DESTINATION ${DFHACK_DATA_DESTINATION}/scripts) + endif() +else() + add_custom_target(test + COMMENT "Nothing to do: CMake option BUILD_TESTS is OFF" + # Portable NOOP; need to put something here or the comment isn't displayed + COMMAND cd + ) +endif() + # build the lib itself if(BUILD_LIBRARY) add_subdirectory(library) @@ -527,38 +560,6 @@ if(BUILD_DOCS) install(FILES "README.html" DESTINATION "${DFHACK_DATA_DESTINATION}") endif() -# Testing with CTest -include(CTest) - -include(CMakeDependentOption) -cmake_dependent_option( - BUILD_TEST_SCRIPTS "Install integration tests in hack/scripts/test" OFF - "BUILD_TESTING" OFF) -mark_as_advanced(FORCE BUILD_TESTS) - -# Handle deprecated BUILD_TESTS option -option(BUILD_TESTS "Deprecated option; please use BUILD_TEST_SCRIPTS=ON" OFF) -if(BUILD_TESTING AND BUILD_TESTS) - set(BUILD_TEST_SCRIPTS ON FORCE) -endif() - -if(BUILD_TESTING) - if(BUILD_TEST_SCRIPTS) - if(EXISTS "${dfhack_SOURCE_DIR}/test/scripts") - message(SEND_ERROR "test/scripts must not exist in the dfhack repo since it would conflict with the tests installed from the scripts repo.") - endif() - install(DIRECTORY ${dfhack_SOURCE_DIR}/test - DESTINATION ${DFHACK_DATA_DESTINATION}/scripts) - install(FILES ci/test.lua DESTINATION ${DFHACK_DATA_DESTINATION}/scripts) - endif() -else() - add_custom_target(test - COMMENT "Nothing to do: CMake option BUILD_TESTS is OFF" - # Portable NOOP; need to put something here or the comment isn't displayed - COMMAND cd - ) -endif() - # Packaging with CPack! set(DFHACK_PACKAGE_SUFFIX "") if(UNIX) diff --git a/depends/CMakeLists.txt b/depends/CMakeLists.txt index 405a9555e..1d43fbf86 100644 --- a/depends/CMakeLists.txt +++ b/depends/CMakeLists.txt @@ -3,6 +3,9 @@ add_subdirectory(lodepng) add_subdirectory(lua) add_subdirectory(md5) add_subdirectory(protobuf) +if(BUILD_TESTING) + add_subdirectory(googletest) +endif() # Don't build tinyxml if it's being externally linked against. if(NOT TinyXML_FOUND) diff --git a/depends/googletest b/depends/googletest new file mode 160000 index 000000000..15460959c --- /dev/null +++ b/depends/googletest @@ -0,0 +1 @@ +Subproject commit 15460959cbbfa20e66ef0b5ab497367e47fc0a04 diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index a62eb2fe0..3c85d5861 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -82,11 +82,17 @@ set(MAIN_SOURCES RemoteTools.cpp ) +macro(dfhack_test name files) + message("dfhack_test files: ${files}") + add_executable(${name} test.cpp ${files}) + target_link_libraries(${name} dfhack gtest) + add_test(NAME ${name} COMMAND ${name}) +endmacro() + if(BUILD_TESTING) - # TODO Make a function or macro for this - add_executable(MiscUtils.test MiscUtils.test.cpp) - target_link_libraries(MiscUtils.test dfhack) - add_test(NAME MiscUtils.test COMMAND MiscUtils.test) + file(GLOB_RECURSE TEST_SOURCES LIST_DIRECTORIES false *.test.cpp) + dfhack_test(test-all ${TEST_SOURCES}) + dfhack_test(test-MiscUtils MiscUtils.test.cpp) # How to get `test` to ensure everything is up to date before running # tests? This add_dependencies() fails with: diff --git a/library/MiscUtils.test.cpp b/library/MiscUtils.test.cpp index f3360b121..e0cabe234 100644 --- a/library/MiscUtils.test.cpp +++ b/library/MiscUtils.test.cpp @@ -1,47 +1,26 @@ -#include "Internal.h" -#include "MiscUtils.h" +#include "MiscUtils.h" +#include #include #include -using namespace std; -int compare_result(const vector &expect, const vector &result) -{ - if (result == expect) - { - cout << "ok\n"; - return 0; - } - else { - cout << "not ok\n"; - auto e = expect.begin(); - auto r = result.begin(); - cout << "# " << setw(20) << left << "Expected" << " " << left << "Got\n"; - while (e < expect.end() || r < result.end()) - { - cout - << "# " - << setw(20) << left << ":" + (e < expect.end() ? *e++ : "") + ":" - << " " - << setw(20) << left << ":" + (r < result.end() ? *r++ : "") + ":" - << "\n"; - } - return 1; - } -} +TEST(MiscUtils, wordwrap) { + std::vector result; -int main() -{ - int fails = 0; -#define TEST_WORDWRAP(label, expect, args) \ - { \ - vector result; \ - cout << label << "... "; \ - word_wrap args; \ - fails += compare_result(expect, result); \ - } + std::cout << "MiscUtils.wordwrap: 0 wrap" << "... "; + word_wrap(&result, "123", 3); + ASSERT_EQ(result.size(), 1); + std::cout << "ok\n"; - TEST_WORDWRAP("Short line, no wrap", vector({"12345"}), (&result, "12345", 15)); + result.clear(); + std::cout << "MiscUtils.wordwrap: 1 wrap" << "... "; + word_wrap(&result, "12345", 3); + ASSERT_EQ(result.size(), 2); + std::cout << "ok\n"; - return fails == 0 ? 0 : 1; + result.clear(); + std::cout << "MiscUtils.wordwrap: 2 wrap" << "... "; + word_wrap(&result, "1234567", 3); + ASSERT_EQ(result.size(), 3); + std::cout << "ok\n"; } diff --git a/library/test.cpp b/library/test.cpp new file mode 100644 index 000000000..2dc3787b7 --- /dev/null +++ b/library/test.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file