diff --git a/.gitignore b/.gitignore index e91dcab3b..9b78fa31f 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,8 @@ build/Makefile build/CMakeCache.txt build/cmake_install.cmake build/CMakeFiles +build/CTestTestfile.cmake +build/DartConfiguration.tcl build/data build/docs build/lua diff --git a/CMakeLists.txt b/CMakeLists.txt index c03037973..e36385c43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -527,14 +527,22 @@ if(BUILD_DOCS) install(FILES "README.html" DESTINATION "${DFHACK_DATA_DESTINATION}") endif() -option(BUILD_TESTS "Include tests (currently just installs Lua tests into the scripts folder)" OFF) +option(BUILD_TESTS "Build 'test' target, and install integration tests in hack/scripts/test" OFF) if(BUILD_TESTS) + include(CTest) + # Install Lua tests into the scripts folder 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) +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! diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index f969ade92..640d17978 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -82,6 +82,16 @@ set(MAIN_SOURCES RemoteTools.cpp ) +if(BUILD_TESTS) + # TODO Make a function or macro for this + add_executable( + MiscUtils.test + MiscUtils.test.cpp + MiscUtils.cpp) + add_test(NAME MiscUtils.test + COMMAND MiscUtils.test) +endif() + if(WIN32) set(CONSOLE_SOURCES Console-windows.cpp) else() diff --git a/library/MiscUtils.test.cpp b/library/MiscUtils.test.cpp new file mode 100644 index 000000000..f3360b121 --- /dev/null +++ b/library/MiscUtils.test.cpp @@ -0,0 +1,47 @@ +#include "Internal.h" +#include "MiscUtils.h" + +#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; + } +} + +int main() +{ + int fails = 0; +#define TEST_WORDWRAP(label, expect, args) \ + { \ + vector result; \ + cout << label << "... "; \ + word_wrap args; \ + fails += compare_result(expect, result); \ + } + + TEST_WORDWRAP("Short line, no wrap", vector({"12345"}), (&result, "12345", 15)); + + return fails == 0 ? 0 : 1; +}