Integrates googletest

develop
Josh Cooper 2022-11-11 14:48:00 -08:00
parent 8e18d610f5
commit 268719ed1f
7 changed files with 74 additions and 75 deletions

3
.gitmodules vendored

@ -28,3 +28,6 @@
[submodule "depends/luacov"] [submodule "depends/luacov"]
path = depends/luacov path = depends/luacov
url = ../../DFHack/luacov.git url = ../../DFHack/luacov.git
[submodule "depends/googletest"]
path = depends/googletest
url = https://github.com/google/googletest.git

@ -426,6 +426,39 @@ if(NOT GIT_FOUND)
message(SEND_ERROR "could not find git") message(SEND_ERROR "could not find git")
endif() 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 # build the lib itself
if(BUILD_LIBRARY) if(BUILD_LIBRARY)
add_subdirectory(library) add_subdirectory(library)
@ -527,38 +560,6 @@ if(BUILD_DOCS)
install(FILES "README.html" DESTINATION "${DFHACK_DATA_DESTINATION}") install(FILES "README.html" DESTINATION "${DFHACK_DATA_DESTINATION}")
endif() 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! # Packaging with CPack!
set(DFHACK_PACKAGE_SUFFIX "") set(DFHACK_PACKAGE_SUFFIX "")
if(UNIX) if(UNIX)

@ -3,6 +3,9 @@ add_subdirectory(lodepng)
add_subdirectory(lua) add_subdirectory(lua)
add_subdirectory(md5) add_subdirectory(md5)
add_subdirectory(protobuf) add_subdirectory(protobuf)
if(BUILD_TESTING)
add_subdirectory(googletest)
endif()
# Don't build tinyxml if it's being externally linked against. # Don't build tinyxml if it's being externally linked against.
if(NOT TinyXML_FOUND) if(NOT TinyXML_FOUND)

@ -0,0 +1 @@
Subproject commit 15460959cbbfa20e66ef0b5ab497367e47fc0a04

@ -82,11 +82,17 @@ set(MAIN_SOURCES
RemoteTools.cpp 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) if(BUILD_TESTING)
# TODO Make a function or macro for this file(GLOB_RECURSE TEST_SOURCES LIST_DIRECTORIES false *.test.cpp)
add_executable(MiscUtils.test MiscUtils.test.cpp) dfhack_test(test-all ${TEST_SOURCES})
target_link_libraries(MiscUtils.test dfhack) dfhack_test(test-MiscUtils MiscUtils.test.cpp)
add_test(NAME MiscUtils.test COMMAND MiscUtils.test)
# How to get `test` to ensure everything is up to date before running # How to get `test` to ensure everything is up to date before running
# tests? This add_dependencies() fails with: # tests? This add_dependencies() fails with:

@ -1,47 +1,26 @@
#include "Internal.h"
#include "MiscUtils.h"
#include "MiscUtils.h"
#include <gtest/gtest.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
using namespace std;
int compare_result(const vector<string> &expect, const vector<string> &result) TEST(MiscUtils, wordwrap) {
{ std::vector<std::string> 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;
}
}
int main() std::cout << "MiscUtils.wordwrap: 0 wrap" << "... ";
{ word_wrap(&result, "123", 3);
int fails = 0; ASSERT_EQ(result.size(), 1);
#define TEST_WORDWRAP(label, expect, args) \ std::cout << "ok\n";
{ \
vector<string> result; \
cout << label << "... "; \
word_wrap args; \
fails += compare_result(expect, result); \
}
TEST_WORDWRAP("Short line, no wrap", vector<string>({"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";
} }

@ -0,0 +1,6 @@
#include <gtest/gtest.h>
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}