diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 83f3490da..d7e8a52b8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -96,8 +96,13 @@ jobs: run: | ninja -C build-ci install ccache --show-stats - - name: Run tests - id: run_tests + - name: Run cpp unit tests + id: run_tests_cpp + run: | + ninja -C build-ci test + exit $? + - name: Run lua tests + id: run_tests_lua run: | export TERM=dumb status=0 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/.gitmodules b/.gitmodules index 9c5ac2d51..c349de288 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 = ../../google/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index c03037973..9fde3e8f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -421,6 +421,16 @@ include_directories(depends/clsocket/src) include_directories(depends/xlsxio/include) add_subdirectory(depends) +# Testing with CTest +macro(dfhack_test name files) + message("dfhack_test(${name}, ${files})") + add_executable(${name} ${files}) + target_include_directories(${name} PUBLIC depends/googletest/googletest/include) + target_link_libraries(${name} dfhack gtest SDL) + add_test(NAME ${name} COMMAND ${name}) +endmacro() +include(CTest) + find_package(Git REQUIRED) if(NOT GIT_FOUND) message(SEND_ERROR "could not find git") diff --git a/depends/CMakeLists.txt b/depends/CMakeLists.txt index 405a9555e..e1c07bd92 100644 --- a/depends/CMakeLists.txt +++ b/depends/CMakeLists.txt @@ -3,6 +3,8 @@ add_subdirectory(lodepng) add_subdirectory(lua) add_subdirectory(md5) add_subdirectory(protobuf) +add_subdirectory(googletest) +set_target_properties(gtest PROPERTIES COMPILE_FLAGS "-Wno-maybe-uninitialized -Wno-sign-compare") # 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..2fe3bd994 --- /dev/null +++ b/depends/googletest @@ -0,0 +1 @@ +Subproject commit 2fe3bd994b3189899d93f1d5a881e725e046fdc2 diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index f969ade92..aee5184c8 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -82,6 +82,11 @@ set(MAIN_SOURCES RemoteTools.cpp ) +file(GLOB_RECURSE TEST_SOURCES + LIST_DIRECTORIES false + *test.cpp) +dfhack_test(dfhack-test "${TEST_SOURCES}") + 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..d3410dad2 --- /dev/null +++ b/library/MiscUtils.test.cpp @@ -0,0 +1,19 @@ + +#include "MiscUtils.h" +#include +#include + +TEST(MiscUtils, wordwrap) { + std::vector result; + + word_wrap(&result, "123", 3); + ASSERT_EQ(result.size(), 1); + + result.clear(); + word_wrap(&result, "123456", 3); + ASSERT_EQ(result.size(), 2); + + result.clear(); + word_wrap(&result, "1234567", 3); + ASSERT_EQ(result.size(), 3); +} diff --git a/library/main.test.cpp b/library/main.test.cpp new file mode 100644 index 000000000..76f841f1b --- /dev/null +++ b/library/main.test.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}