Skip to content
Permalink
Browse files

Add Catch2 for unit testing (#1550)

Adds catch 2 to CMakeLists in a basic form which duplicates all files
across, with an example test to validate it runs correctly

Moves the implementation of CorsixTH into a main executable and a
seperate static lib we can link against for unit testing. This means we
don't have to mess around with deps for unit testing.

Appveyor and travis changes
  • Loading branch information...
DavidFair authored and TheCycoONE committed Jun 14, 2019
1 parent 5bc48d3 commit 1dd16721273d65ac81053748d8ef7ef7eccc196a
@@ -1,4 +1,3 @@
sudo: false
dist: xenial
language: cpp
compiler: gcc
@@ -13,6 +12,14 @@ addons:
- ant

before_install:
# Xenial does not have catch2 in its repo so clone and install locally
- cd $HOME
- git clone https://github.com/catchorg/Catch2.git --depth=1
- cd Catch2
- cmake -Bbuild -H. -DBUILD_TESTING=OFF
- cd build && sudo make install
- cd ${TRAVIS_BUILD_DIR}

- luarocks --local install --server=http://luarocks.org luasec OPENSSL_DIR=/usr OPENSSL_LIBDIR=/usr/lib/x86_64-linux-gnu/
# Required for LDocGen
- luarocks --local install lpeg
@@ -32,7 +39,7 @@ install:
- cd $TRAVIS_BUILD_DIR
# Create unix makefiles
- cp $HOME/deps/travisci/cmake/FindFreetype.cmake CMake/FindFreetype.cmake
- cmake -DLUA_PROGRAM_PATH=`which lua` -DWITH_AUDIO=ON -DWITH_MOVIES=ON -DWITH_LUAJIT=OFF -DCMAKE_INCLUDE_PATH="$CMAKE_INCLUDE_PATH;$CMAKE_INCLUDE_PATH/SDL2" -DCMAKE_LIBRARY_PATH="$CMAKE_LIBRARY_PATH" -DLUA_LIBRARY="$CMAKE_LIBRARY_PATH/liblua.so" -Bfresh -H. --debug-output
- cmake -DLUA_PROGRAM_PATH=`which lua` -DENABLE_UNIT_TESTS=ON -DWITH_AUDIO=ON -DWITH_MOVIES=ON -DWITH_LUAJIT=OFF -DCMAKE_INCLUDE_PATH="$CMAKE_INCLUDE_PATH;$CMAKE_INCLUDE_PATH/SDL2" -DCMAKE_LIBRARY_PATH="$CMAKE_LIBRARY_PATH" -DLUA_LIBRARY="$CMAKE_LIBRARY_PATH/liblua.so" -Bfresh -H. --debug-output
before_script:
# Don't ask for confirmation when using scp
- echo -e "Host armedpineapple.co.uk\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
@@ -55,6 +62,11 @@ script:
- cd $TRAVIS_BUILD_DIR/CorsixTH/Luatest
- eval `luarocks --local path`
- LUA_PATH="../Lua/?.lua;$LUA_PATH" /home/travis/.luarocks/bin/busted

# Run CPP Tests
- cd $TRAVIS_BUILD_DIR/fresh/CorsixTH
- eval 'ctest -VV'

# Build LevelEdit
- cd $TRAVIS_BUILD_DIR/LevelEdit && ant dist
# Build documentation
@@ -15,11 +15,14 @@ cmake_minimum_required(VERSION 3.5)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)

option(ENABLE_UNIT_TESTS "Enables Unit Testing Targets" OFF)

if(CMAKE_GENERATOR MATCHES "^Visual Studio 14 2015" OR CMAKE_GENERATOR MATCHES "^Visual Studio 15 2017")
option(USE_VCPKG_DEPS "Build vcpkg dependencies locally" OFF)
endif()

if(USE_VCPKG_DEPS)
# This must be before the project for tool-chain to work correctly
message("Note: Using locally built vcpkg dependencies.")
include(VcpkgDeps)
endif()
@@ -34,6 +34,9 @@ include_directories(${CMAKE_BINARY_DIR}/CorsixTH/Src/)

# Generate source files list
# Note: Done after generating config.h

# XXX: We should manually specify the files to include in each directory rather
# than glob them all
file(GLOB_RECURSE corsixth_source_files
${CMAKE_SOURCE_DIR}/CorsixTH/SrcUnshared/*.cpp
${CMAKE_SOURCE_DIR}/CorsixTH/SrcUnshared/*.c
@@ -46,13 +49,29 @@ file(GLOB_RECURSE corsixth_source_files
${CMAKE_SOURCE_DIR}/common/rnc.cpp
${CMAKE_SOURCE_DIR}/common/rnc.h
${CMAKE_SOURCE_DIR}/CorsixTH/Src/shaders/*.psh
${CMAKE_BINARY_DIR}/CorsixTH/Src/config.h
${CMAKE_SOURCE_DIR}/CorsixTH/Lua/api_version.lua
${CMAKE_SOURCE_DIR}/CorsixTH/CorsixTH.rc
${CMAKE_SOURCE_DIR}/LFS/*.c
${CMAKE_SOURCE_DIR}/LPEG/*.c

${CMAKE_BINARY_DIR}/CorsixTH/Src/config.h
)

set(corsixth_main_files
${CMAKE_SOURCE_DIR}/CorsixTH/Src/main.cpp
${CMAKE_SOURCE_DIR}/CorsixTH/SrcUnshared/main.cpp
${CMAKE_SOURCE_DIR}/CorsixTH/Src/main.h
)

message(${corsixth_main_files})

# Remove main.cpp and main.h from the files we compile in to lib
list(REMOVE_ITEM corsixth_source_files "${CMAKE_SOURCE_DIR}/CorsixTH/Src/main.cpp"
"${CMAKE_SOURCE_DIR}/CorsixTH/SrcUnshared/main.cpp")
list(REMOVE_ITEM corsixth_source_files "${CMAKE_SOURCE_DIR}/CorsixTH/Src/main.h")

add_library(CorsixTH_lib STATIC ${corsixth_source_files})

# Declaration of the executable
if(APPLE)
set(corsixth_icon_file ${CMAKE_SOURCE_DIR}/CorsixTH/Icon.icns)
@@ -63,22 +82,28 @@ if(APPLE)
)
set(MACOSX_BUNDLE_ICON_FILE Icon.icns)

add_executable(CorsixTH MACOSX_BUNDLE ${corsixth_source_files} ${corsixth_icon_file})
add_executable(CorsixTH MACOSX_BUNDLE ${corsixth_main_files} ${corsixth_icon_file})

set_target_properties(CorsixTH PROPERTIES LINK_FLAGS_MINSIZEREL "-dead_strip")
set_target_properties(CorsixTH PROPERTIES XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "@executable_path/../Frameworks")

target_link_libraries(CorsixTH SDL2main)
include_directories(${CMAKE_BINARY_DIR}/CorsixTH/SDLMain/)
else()
add_executable(CorsixTH ${corsixth_source_files})
add_executable(CorsixTH ${corsixth_main_files})
endif()

target_link_libraries(CorsixTH CorsixTH_lib)

if(UNIX AND NOT APPLE)
set_target_properties(CorsixTH PROPERTIES OUTPUT_NAME corsix-th)
endif()

# Set language standard
set_property(TARGET CorsixTH_lib PROPERTY CXX_STANDARD 14)
set_property(TARGET CorsixTH_lib PROPERTY CXX_EXTENSIONS OFF)
set_property(TARGET CorsixTH_lib PROPERTY CXX_STANDARD_REQUIRED ON)

set_property(TARGET CorsixTH PROPERTY CXX_STANDARD 14)
set_property(TARGET CorsixTH PROPERTY CXX_EXTENSIONS OFF)
set_property(TARGET CorsixTH PROPERTY CXX_STANDARD_REQUIRED ON)
@@ -93,7 +118,7 @@ ENDIF()
# Find SDL
if(MSVC AND USE_VCPKG_DEPS)
find_package(SDL2 CONFIG REQUIRED)
target_link_libraries(CorsixTH SDL2::SDL2)
target_link_libraries(CorsixTH_lib SDL2::SDL2)
target_link_libraries(CorsixTH SDL2::SDL2main)
else()
find_package(SDL2 REQUIRED)
@@ -224,6 +249,14 @@ elseif(MSVC)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CorsixTH.vcxproj.user.in ${CMAKE_CURRENT_BINARY_DIR}/CorsixTH.vcxproj.user @ONLY)
endif()

# Adding testing files whilst we also have the list of source files and have found libs
if(ENABLE_UNIT_TESTS)
message("Building Unit Tests")
# Enable testing if set at this level so we can use CTest from the root of the build dir
enable_testing()
add_subdirectory(${PROJECT_SOURCE_DIR}/CppTest)
endif()


# Declaration of the install process
if(NOT USE_SOURCE_DATADIRS)
@@ -0,0 +1,48 @@
# MIT License
#
# Copyright (c) 2019 David Fairbrother
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

set (TESTING_FILES
test_main.cpp
example.cpp
)

find_package(Catch2 CONFIG REQUIRED)

add_custom_target(AllTests)

# Expose the header files from CorsixTH
include_directories(${CMAKE_SOURCE_DIR}/CorsixTH/Src/)

add_executable(UnitTests ${TESTING_FILES} ${TEST_SOURCE_FILES})
target_link_libraries(UnitTests Catch2::Catch2)
target_link_libraries(UnitTests CorsixTH_lib)

set_property(TARGET UnitTests PROPERTY CXX_STANDARD 14)
set_property(TARGET UnitTests PROPERTY CXX_EXTENSIONS OFF)
set_property(TARGET UnitTests PROPERTY CXX_STANDARD_REQUIRED ON)

# Add the target to CTest
add_test(NAME UnitTests COMMAND UnitTests)
set_target_properties(UnitTests PROPERTIES FOLDER UnitTests)

# Make sure we add it as a dependency to all tests (some day we can add LuaTest here)
add_dependencies(AllTests UnitTests)
@@ -0,0 +1,6 @@
#include "catch2/catch.hpp"

TEST_CASE("Example Test", "[example]")
{
REQUIRE(true);
}
@@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"
@@ -10,10 +10,16 @@ cache:
- vcpkg -> CMake/VcpkgDeps.cmake, scripts/build_vcpkg_deps.ps1
configuration: Release
before_build:
- cmd: cmake -G "%GENERATOR%" -DUSE_VCPKG_DEPS=ON .
- cmd: cmake -G "%GENERATOR%" -DUSE_VCPKG_DEPS=ON -DENABLE_UNIT_TESTS=ON .
build:
project: CorsixTH_Top_Level.sln
verbosity: minimal

test_script:
- cd CorsixTH
- cmd: ctest -VV -C Release
- cd ..

after_build:
- cp -R %APPVEYOR_BUILD_FOLDER%/CorsixTH/Lua %APPVEYOR_BUILD_FOLDER%/CorsixTH/Release/Lua
- cp -R %APPVEYOR_BUILD_FOLDER%/CorsixTH/Bitmap %APPVEYOR_BUILD_FOLDER%/CorsixTH/Release/Bitmap
@@ -24,17 +24,17 @@

# Parameters
Param(
[Parameter(Mandatory=$true)][bool]$BuildAnimView,
[Parameter(Mandatory=$true)][string]$VcpkgTriplet,
[Parameter(Mandatory=$true)][string]$VcpkgCommitSha
[Parameter(Mandatory = $true)][bool]$BuildAnimView,
[Parameter(Mandatory = $true)][string]$VcpkgTriplet,
[Parameter(Mandatory = $true)][string]$VcpkgCommitSha
)

################
# Variables
################

$anim_view_libs = "wxwidgets"
$corsixth_libs = "ffmpeg", "freetype", "lua", "luafilesystem", "lpeg", "sdl2", "sdl2-mixer[libflac,mpg123,libmodplug,libvorbis]", "luasocket"
$corsixth_libs = "ffmpeg", "freetype", "lua", "luafilesystem", "lpeg", "sdl2", "sdl2-mixer[libflac,mpg123,libmodplug,libvorbis]", "luasocket", "catch2"

$vcpkg_git_url = "https://github.com/CorsixTH/vcpkg"

@@ -71,15 +71,16 @@ function run_script {
run_command -command "git clone $vcpkg_git_url $dest_folder_name"
Set-Location -Path $dest_folder_path
run_command "git checkout $VcpkgCommitSha"
} else {
}
else {
# Move into vcpkg folder and update to latest version
Set-Location -Path $dest_folder_path
run_command "git reset --hard; git fetch origin; git checkout $VcpkgCommitSha"
}

$commit_id_filename = "commit_id.txt"
if (-Not (Test-Path $commit_id_filename) -or
(Get-Content $commit_id_filename | Where-Object {$_ -ne $VcpkgCommitSha })) {
(Get-Content $commit_id_filename | Where-Object { $_ -ne $VcpkgCommitSha })) {
# Sha does not match or does not exist.
Write-Output "Dependencies have changed. Bootstrapping and updating vcpkg."
run_command ".\bootstrap-vcpkg.bat"
@@ -136,7 +137,8 @@ try {
run_script
# Move back up a dir to return user to original location
Set-Location -Path $starting_dir
} catch [Exception]{
}
catch [Exception] {
Set-Location -Path $starting_dir
# Echo the exception back out
$_

0 comments on commit 1dd1672

Please sign in to comment.
You can’t perform that action at this time.