unit tests: Add CTest support, and a trivial first unit test

If BUILD_TESTS=ON:
- Adds a 'test' target for ninja
- Adds a library/MiscUtils.test unit test executable
develop
Tim Siegel 2022-04-20 20:28:13 -04:00 committed by Josh Cooper
parent 344ed4312b
commit c5be87e381
4 changed files with 68 additions and 1 deletions

2
.gitignore vendored

@ -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

@ -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!

@ -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()

@ -0,0 +1,47 @@
#include "Internal.h"
#include "MiscUtils.h"
#include <iostream>
#include <string>
using namespace std;
int compare_result(const vector<string> &expect, const vector<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()
{
int fails = 0;
#define TEST_WORDWRAP(label, expect, args) \
{ \
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));
return fails == 0 ? 0 : 1;
}