Skip to content

Commit

Permalink
unit tests: Add CTest support, and a trivial first unit test
Browse files Browse the repository at this point in the history
If BUILD_TESTS=ON:
- Adds a 'test' target for ninja
- Adds a library/MiscUtils.test unit test executable
  • Loading branch information
softmoth committed Apr 21, 2022
1 parent 62b02c2 commit 26a9bf9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ build/Makefile
build/CMakeCache.txt
build/cmake_install.cmake
build/CMakeFiles
build/CTestTestfile.cmake
build/DartConfiguration.tcl
build/data
build/docs
build/lua
Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,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!
Expand Down
10 changes: 10 additions & 0 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
47 changes: 47 additions & 0 deletions library/MiscUtils.test.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 26a9bf9

Please sign in to comment.