diff options
-rw-r--r-- | .github/workflows/core_build.yml | 2 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 38 | ||||
-rwxr-xr-x | apps/ci/ci-install.sh | 1 | ||||
-rwxr-xr-x | apps/ci/ci-run-unit-tests.sh | 3 | ||||
-rw-r--r-- | apps/compiler/includes/functions.sh | 1 | ||||
-rw-r--r-- | conf/dist/config.cmake | 1 | ||||
-rw-r--r-- | conf/dist/config.sh | 2 | ||||
-rw-r--r-- | src/cmake/googletest-download.cmake | 20 | ||||
-rw-r--r-- | src/cmake/googletest.cmake | 32 | ||||
-rw-r--r-- | src/cmake/showoptions.cmake | 6 | ||||
-rw-r--r-- | src/test/CMakeLists.txt | 23 | ||||
-rw-r--r-- | src/test/server/game/Miscellaneous/FormulasTest.cpp | 13 |
13 files changed, 132 insertions, 11 deletions
diff --git a/.github/workflows/core_build.yml b/.github/workflows/core_build.yml index 7d9106132c..c36b912efb 100644 --- a/.github/workflows/core_build.yml +++ b/.github/workflows/core_build.yml @@ -42,6 +42,8 @@ jobs: run: source ./apps/ci/ci-worldserver-dry-run.sh - name: Check startup errors run: source ./apps/ci/ci-error-check.sh + - name: Run unit tests + run: source ./apps/ci/ci-run-unit-tests.sh docker-build: strategy: diff --git a/.gitignore b/.gitignore index 755983db24..807ec82280 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,7 @@ nbproject/ .vscode .idea cmake-build-debug/* +cmake-build-debug-coverage/* # # Eclipse diff --git a/CMakeLists.txt b/CMakeLists.txt index e266cbf48f..79c73eccbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,8 @@ set(AC_PATH_ROOT "${CMAKE_SOURCE_DIR}") # set macro-directory list(APPEND CMAKE_MODULE_PATH - "${CMAKE_SOURCE_DIR}/src/cmake/macros" - "${CMAKE_SOURCE_DIR}/deps/cotire/CMake") + "${CMAKE_SOURCE_DIR}/src/cmake/macros" + "${CMAKE_SOURCE_DIR}/deps/cotire/CMake") include(CheckCXXSourceRuns) include(CheckIncludeFiles) @@ -49,7 +49,7 @@ include(conf/dist/config.cmake) # load custom configurations for cmake if exists if(EXISTS "${CMAKE_SOURCE_DIR}/conf/config.cmake") - include(conf/config.cmake) + include(conf/config.cmake) endif() # @@ -63,7 +63,7 @@ FOREACH(subdir ${sub_DIRS}) get_filename_component(MODULENAME ${subdir} NAME) if (";${DISABLED_AC_MODULES};" MATCHES ";${MODULENAME};") - continue() + continue() endif() STRING(REGEX REPLACE "^${CMAKE_SOURCE_DIR}/" "" subdir_rel ${subdir}) @@ -77,13 +77,13 @@ CU_RUN_HOOK("AFTER_LOAD_CONF") # build in Release-mode by default if not explicitly set if( NOT CMAKE_BUILD_TYPE ) - set(CMAKE_BUILD_TYPE "Release") + set(CMAKE_BUILD_TYPE "Release") endif() # turn off PCH totally if enabled (hidden setting, mainly for devs) if( NOPCH ) - set(USE_COREPCH 0) - set(USE_SCRIPTPCH 0) + set(USE_COREPCH 0) + set(USE_SCRIPTPCH 0) endif() include(ConfigureBaseTargets) @@ -101,11 +101,11 @@ find_package(ACE REQUIRED) find_package(MySQL REQUIRED) if(UNIX AND WITH_PERFTOOLS) - find_package(Gperftools) + find_package(Gperftools) endif() if(NOT WITHOUT_GIT) - find_package(Git) + find_package(Git) endif() # Find revision ID and hash of the sourcetree @@ -121,11 +121,11 @@ include(src/cmake/showoptions.cmake) add_subdirectory(deps) if( SERVERS OR TOOLS) - add_subdirectory(src/common) + add_subdirectory(src/common) endif() if( TOOLS ) - add_subdirectory(src/tools) + add_subdirectory(src/tools) endif() # @@ -138,3 +138,19 @@ CU_RUN_HOOK("BEFORE_SRC_LOAD") add_subdirectory(src) CU_RUN_HOOK("AFTER_SRC_LOAD") + +if( UNIT_TESTS ) + # we use this to get code coverage + if(CMAKE_CXX_COMPILER_ID MATCHES GNU) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") + endif() + + include(src/cmake/googletest.cmake) + fetch_googletest( + ${PROJECT_SOURCE_DIR}/src/cmake + ${PROJECT_BINARY_DIR}/googletest + ) + + enable_testing() + add_subdirectory(src/test) +endif() diff --git a/apps/ci/ci-install.sh b/apps/ci/ci-install.sh index 6d6d8f0777..8ae35ee7d2 100755 --- a/apps/ci/ci-install.sh +++ b/apps/ci/ci-install.sh @@ -8,6 +8,7 @@ CWARNINGS=ON CDEBUG=OFF CTYPE=Release CSCRIPTS=ON +CUNIT_TESTS=ON CSERVERS=ON CTOOLS=ON CSCRIPTPCH=OFF diff --git a/apps/ci/ci-run-unit-tests.sh b/apps/ci/ci-run-unit-tests.sh new file mode 100755 index 0000000000..2f4a24eabc --- /dev/null +++ b/apps/ci/ci-run-unit-tests.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +time /home/runner/work/azerothcore-wotlk/azerothcore-wotlk/var/build/obj/src/test/unit_tests diff --git a/apps/compiler/includes/functions.sh b/apps/compiler/includes/functions.sh index e0f5560b14..f9ac5ce086 100644 --- a/apps/compiler/includes/functions.sh +++ b/apps/compiler/includes/functions.sh @@ -35,6 +35,7 @@ function comp_configure() { cmake $SRCPATH -DCMAKE_INSTALL_PREFIX=$BINPATH $DCONF -DSERVERS=$CSERVERS \ -DSCRIPTS=$CSCRIPTS \ + -DUNIT_TESTS=$CUNIT_TESTS \ -DTOOLS=$CTOOLS -DUSE_SCRIPTPCH=$CSCRIPTPCH -DUSE_COREPCH=$CCOREPCH -DWITH_COREDEBUG=$CDEBUG -DCMAKE_BUILD_TYPE=$CTYPE -DWITH_WARNINGS=$CWARNINGS \ -DCMAKE_C_COMPILER=$CCOMPILERC -DCMAKE_CXX_COMPILER=$CCOMPILERCXX "-DDISABLED_AC_MODULES=$CDISABLED_AC_MODULES" $CCUSTOMOPTIONS diff --git a/conf/dist/config.cmake b/conf/dist/config.cmake index 19e6c4c2aa..dd6abcbd25 100644 --- a/conf/dist/config.cmake +++ b/conf/dist/config.cmake @@ -1,5 +1,6 @@ option(SERVERS "Build worldserver and authserver" 1) option(SCRIPTS "Build core with scripts included" 1) +option(UNIT_TESTS "Build unit tests" 0) option(TOOLS "Build map/vmap/mmap extraction/assembler tools" 0) option(USE_SCRIPTPCH "Use precompiled headers when compiling scripts" 1) option(USE_COREPCH "Use precompiled headers when compiling servers" 1) diff --git a/conf/dist/config.sh b/conf/dist/config.sh index 339b68073f..9d6492d6df 100644 --- a/conf/dist/config.sh +++ b/conf/dist/config.sh @@ -47,6 +47,8 @@ CDEBUG=OFF CTYPE=Release # compile scripts CSCRIPTS=ON +# compile unit tests +CUNIT_TESTS=OFF # compile server CSERVERS=ON # compile tools diff --git a/src/cmake/googletest-download.cmake b/src/cmake/googletest-download.cmake new file mode 100644 index 0000000000..0da8335896 --- /dev/null +++ b/src/cmake/googletest-download.cmake @@ -0,0 +1,20 @@ +# code copied from https://crascit.com/2015/07/25/cmake-gtest/ +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) + +project(googletest-download NONE) + +include(ExternalProject) + +ExternalProject_Add( + googletest + SOURCE_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-src" + BINARY_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-build" + GIT_REPOSITORY + https://github.com/google/googletest.git + GIT_TAG + release-1.10.0 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/src/cmake/googletest.cmake b/src/cmake/googletest.cmake new file mode 100644 index 0000000000..de0c3bbb4c --- /dev/null +++ b/src/cmake/googletest.cmake @@ -0,0 +1,32 @@ +# the following code to fetch googletest +# is inspired by and adapted after https://crascit.com/2015/07/25/cmake-gtest/ +# download and unpack googletest at configure time + +macro(fetch_googletest _download_module_path _download_root) + set(GOOGLETEST_DOWNLOAD_ROOT ${_download_root}) + configure_file( + ${_download_module_path}/googletest-download.cmake + ${_download_root}/CMakeLists.txt + @ONLY + ) + unset(GOOGLETEST_DOWNLOAD_ROOT) + + execute_process( + COMMAND + "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . + WORKING_DIRECTORY + ${_download_root} + ) + execute_process( + COMMAND + "${CMAKE_COMMAND}" --build . + WORKING_DIRECTORY + ${_download_root} + ) + + # adds the targers: gtest, gtest_main, gmock, gmock_main + add_subdirectory( + ${_download_root}/googletest-src + ${_download_root}/googletest-build + ) +endmacro() diff --git a/src/cmake/showoptions.cmake b/src/cmake/showoptions.cmake index 7d10b7f706..f5fb5c4e19 100644 --- a/src/cmake/showoptions.cmake +++ b/src/cmake/showoptions.cmake @@ -40,6 +40,12 @@ else() message("* Build map/vmap tools : No (default)") endif() +if( UNIT_TESTS ) + message("* Build unit tests : Yes") +else() + message("* Build unit tests : No (default)") +endif() + if( USE_COREPCH ) message("* Build core w/PCH : Yes (default)") else() diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt new file mode 100644 index 0000000000..8ecff19c28 --- /dev/null +++ b/src/test/CMakeLists.txt @@ -0,0 +1,23 @@ +CollectSourceFiles( + ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE_SOURCES +) + +add_executable( + unit_tests + ${PRIVATE_SOURCES} +) + +target_link_libraries( + unit_tests + gtest_main + game + game-interface +) + +add_test( + NAME + unit + COMMAND + ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/unit_tests +) diff --git a/src/test/server/game/Miscellaneous/FormulasTest.cpp b/src/test/server/game/Miscellaneous/FormulasTest.cpp new file mode 100644 index 0000000000..74df50f6f6 --- /dev/null +++ b/src/test/server/game/Miscellaneous/FormulasTest.cpp @@ -0,0 +1,13 @@ +#include "gtest/gtest.h" +#include "Formulas.h" + +TEST(FormulasTest, hk_honor_at_level) +{ + EXPECT_EQ(acore::Honor::hk_honor_at_level(80), 124); + EXPECT_EQ(acore::Honor::hk_honor_at_level(80, 2), 248); + EXPECT_EQ(acore::Honor::hk_honor_at_level(80, 0.5), 62); + EXPECT_EQ(acore::Honor::hk_honor_at_level(1), 2); + EXPECT_EQ(acore::Honor::hk_honor_at_level(1, 10), 16); + EXPECT_EQ(acore::Honor::hk_honor_at_level(2), 4); + EXPECT_EQ(acore::Honor::hk_honor_at_level(3), 5); +} |