[Test] Replace doctest with googletest

* Convert the only existing test (strutils) to googletest.
* Create a test target for vbam-wx-config.
* Add necessary fakes for the test binary.
* Add tests to CI.
This commit is contained in:
Fabrice de Gans 2024-05-05 21:28:55 -07:00 committed by Fabrice de Gans
parent fc82e06270
commit 13756bcbf9
19 changed files with 287 additions and 7435 deletions

View File

@ -55,3 +55,9 @@ jobs:
name: Build libretro core
run: >-
nix-shell --command 'make -C src/libretro ${{ matrix.libretro_build }}'
# Run tests
- if: matrix.build_options == 'default'
name: Run tests
run: >-
nix-shell --command 'cd build && ctest -j'

View File

@ -62,3 +62,8 @@ jobs:
- if: matrix.build_options == 'libretro'
name: Build libretro core
run: make -C src/libretro ${{ matrix.libretro_build }} CC=clang CXX=clang++
# Run tests
- if: matrix.build_options == 'default'
name: Run tests
run: cd build && ctest -j

View File

@ -58,6 +58,11 @@ jobs:
run: sudo ninja -C build install
# Libretro build
- name: Build libretro core
if: matrix.build_options == 'libretro'
- if: matrix.build_options == 'libretro'
name: Build libretro core
run: make -C src/libretro ${{ matrix.libretro_build }}
# Run tests
- if: matrix.build_options == 'default'
name: Run tests
run: cd build && ctest -j

View File

@ -101,3 +101,8 @@ jobs:
- name: Build
run: cmake --build build
# Run tests
- if: matrix.build_options == 'default' && matrix.msvc_arch != 'amd64_arm64'
name: Run tests
run: cd build && ctest -j

View File

@ -77,6 +77,26 @@ set(CMAKE_C_STANDARD_REQUIRED True)
project(VBA-M C CXX)
include(CTest)
include(FetchContent)
include(GNUInstallDirs)
include(Options)
include(Architecture)
include(Toolchain)
include(Dependencies)
# Configure gtest
if(BUILD_TESTING)
FetchContent_Declare(googletest
URL https://github.com/google/googletest/archive/2d16ed055d09c3689d44b272adc097393de948a0.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
endif()
if(NOT CMAKE_PREFIX_PATH AND (NOT ("$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")))
set(CMAKE_PREFIX_PATH "$ENV{CMAKE_PREFIX_PATH}")
endif()
@ -97,17 +117,6 @@ if(NOT "$ENV{MSYSTEM_PREFIX}" STREQUAL "")
set(MSYS ON)
endif()
include(CTest)
if(BUILD_TESTING)
enable_testing()
endif()
include(GNUInstallDirs)
include(Options)
include(Architecture)
include(Toolchain)
include(Dependencies)
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
include(GitTagVersion)
git_version(VBAM_VERSION VBAM_REVISION VBAM_VERSION_RELEASE)

View File

@ -1,175 +0,0 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
doctest
-----
This module defines a function to help use the doctest test framework.
The :command:`doctest_discover_tests` discovers tests by asking the compiled test
executable to enumerate its tests. This does not require CMake to be re-run
when tests change. However, it may not work in a cross-compiling environment,
and setting test properties is less convenient.
This command is intended to replace use of :command:`add_test` to register
tests, and will create a separate CTest test for each doctest test case. Note
that this is in some cases less efficient, as common set-up and tear-down logic
cannot be shared by multiple test cases executing in the same instance.
However, it provides more fine-grained pass/fail information to CTest, which is
usually considered as more beneficial. By default, the CTest test name is the
same as the doctest name; see also ``TEST_PREFIX`` and ``TEST_SUFFIX``.
.. command:: doctest_discover_tests
Automatically add tests with CTest by querying the compiled test executable
for available tests::
doctest_discover_tests(target
[TEST_SPEC arg1...]
[EXTRA_ARGS arg1...]
[WORKING_DIRECTORY dir]
[TEST_PREFIX prefix]
[TEST_SUFFIX suffix]
[PROPERTIES name1 value1...]
[TEST_LIST var]
)
``doctest_discover_tests`` sets up a post-build command on the test executable
that generates the list of tests by parsing the output from running the test
with the ``--list-test-cases`` argument. This ensures that the full
list of tests is obtained. Since test discovery occurs at build time, it is
not necessary to re-run CMake when the list of tests changes.
However, it requires that :prop_tgt:`CROSSCOMPILING_EMULATOR` is properly set
in order to function in a cross-compiling environment.
Additionally, setting properties on tests is somewhat less convenient, since
the tests are not available at CMake time. Additional test properties may be
assigned to the set of tests as a whole using the ``PROPERTIES`` option. If
more fine-grained test control is needed, custom content may be provided
through an external CTest script using the :prop_dir:`TEST_INCLUDE_FILES`
directory property. The set of discovered tests is made accessible to such a
script via the ``<target>_TESTS`` variable.
The options are:
``target``
Specifies the doctest executable, which must be a known CMake executable
target. CMake will substitute the location of the built executable when
running the test.
``TEST_SPEC arg1...``
Specifies test cases, wildcarded test cases, tags and tag expressions to
pass to the doctest executable with the ``--list-test-cases`` argument.
``EXTRA_ARGS arg1...``
Any extra arguments to pass on the command line to each test case.
``WORKING_DIRECTORY dir``
Specifies the directory in which to run the discovered test cases. If this
option is not provided, the current binary directory is used.
``TEST_PREFIX prefix``
Specifies a ``prefix`` to be prepended to the name of each discovered test
case. This can be useful when the same test executable is being used in
multiple calls to ``doctest_discover_tests()`` but with different
``TEST_SPEC`` or ``EXTRA_ARGS``.
``TEST_SUFFIX suffix``
Similar to ``TEST_PREFIX`` except the ``suffix`` is appended to the name of
every discovered test case. Both ``TEST_PREFIX`` and ``TEST_SUFFIX`` may
be specified.
``PROPERTIES name1 value1...``
Specifies additional properties to be set on all tests discovered by this
invocation of ``doctest_discover_tests``.
``TEST_LIST var``
Make the list of tests available in the variable ``var``, rather than the
default ``<target>_TESTS``. This can be useful when the same test
executable is being used in multiple calls to ``doctest_discover_tests()``.
Note that this variable is only available in CTest.
#]=======================================================================]
#------------------------------------------------------------------------------
function(doctest_discover_tests TARGET)
cmake_parse_arguments(
""
""
"TEST_PREFIX;TEST_SUFFIX;WORKING_DIRECTORY;TEST_LIST"
"TEST_SPEC;EXTRA_ARGS;PROPERTIES"
${ARGN}
)
if(NOT _WORKING_DIRECTORY)
set(_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
endif()
if(NOT _TEST_LIST)
set(_TEST_LIST ${TARGET}_TESTS)
endif()
## Generate a unique name based on the extra arguments
string(SHA1 args_hash "${_TEST_SPEC} ${_EXTRA_ARGS}")
string(SUBSTRING ${args_hash} 0 7 args_hash)
# Define rule to generate test list for aforementioned test executable
set(ctest_include_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_include-${args_hash}.cmake")
set(ctest_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_tests-${args_hash}.cmake")
get_property(crosscompiling_emulator
TARGET ${TARGET}
PROPERTY CROSSCOMPILING_EMULATOR
)
add_custom_command(
TARGET ${TARGET} POST_BUILD
BYPRODUCTS "${ctest_tests_file}"
COMMAND "${CMAKE_COMMAND}"
-D "TEST_TARGET=${TARGET}"
-D "TEST_EXECUTABLE=$<TARGET_FILE:${TARGET}>"
-D "TEST_EXECUTOR=${crosscompiling_emulator}"
-D "TEST_WORKING_DIR=${_WORKING_DIRECTORY}"
-D "TEST_SPEC=${_TEST_SPEC}"
-D "TEST_EXTRA_ARGS=${_EXTRA_ARGS}"
-D "TEST_PROPERTIES=${_PROPERTIES}"
-D "TEST_PREFIX=${_TEST_PREFIX}"
-D "TEST_SUFFIX=${_TEST_SUFFIX}"
-D "TEST_LIST=${_TEST_LIST}"
-D "CTEST_FILE=${ctest_tests_file}"
-P "${_DOCTEST_DISCOVER_TESTS_SCRIPT}"
VERBATIM
)
file(WRITE "${ctest_include_file}"
"if(EXISTS \"${ctest_tests_file}\")\n"
" include(\"${ctest_tests_file}\")\n"
"else()\n"
" add_test(${TARGET}_NOT_BUILT-${args_hash} ${TARGET}_NOT_BUILT-${args_hash})\n"
"endif()\n"
)
if(NOT CMAKE_VERSION VERSION_LESS 3.10)
# Add discovered tests to directory TEST_INCLUDE_FILES
set_property(DIRECTORY
APPEND PROPERTY TEST_INCLUDE_FILES "${ctest_include_file}"
)
else()
# Add discovered tests as directory TEST_INCLUDE_FILE if possible
get_property(test_include_file_set DIRECTORY PROPERTY TEST_INCLUDE_FILE SET)
if(NOT ${test_include_file_set})
set_property(DIRECTORY
PROPERTY TEST_INCLUDE_FILE "${ctest_include_file}"
)
else()
message(FATAL_ERROR
"Cannot set more than one TEST_INCLUDE_FILE"
)
endif()
endif()
endfunction()
###############################################################################
set(_DOCTEST_DISCOVER_TESTS_SCRIPT
${CMAKE_CURRENT_LIST_DIR}/doctestAddTests.cmake
)

View File

@ -1,81 +0,0 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
set(prefix "${TEST_PREFIX}")
set(suffix "${TEST_SUFFIX}")
set(spec ${TEST_SPEC})
set(extra_args ${TEST_EXTRA_ARGS})
set(properties ${TEST_PROPERTIES})
set(script)
set(suite)
set(tests)
function(add_command NAME)
set(_args "")
foreach(_arg ${ARGN})
if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
else()
set(_args "${_args} ${_arg}")
endif()
endforeach()
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
endfunction()
# Run test executable to get list of available tests
if(NOT EXISTS "${TEST_EXECUTABLE}")
message(FATAL_ERROR
"Specified test executable '${TEST_EXECUTABLE}' does not exist"
)
endif()
if("${spec}" MATCHES .)
set(spec "--test-case=${spec}")
endif()
execute_process(
COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-test-cases
OUTPUT_VARIABLE output
RESULT_VARIABLE result
)
if(NOT ${result} EQUAL 0)
message(FATAL_ERROR
"Error running test executable '${TEST_EXECUTABLE}':\n"
" Result: ${result}\n"
" Output: ${output}\n"
)
endif()
string(REPLACE "\n" ";" output "${output}")
# Parse output
foreach(line ${output})
if("${line}" STREQUAL "===============================================================================" OR "${line}" MATCHES [==[^\[doctest\] ]==])
continue()
endif()
set(test ${line})
# use escape commas to handle properly test cases with commas inside the name
string(REPLACE "," "\\," test_name ${test})
# ...and add to script
add_command(add_test
"${prefix}${test}${suffix}"
${TEST_EXECUTOR}
"${TEST_EXECUTABLE}"
"--test-case=${test_name}"
${extra_args}
)
add_command(set_tests_properties
"${prefix}${test}${suffix}"
PROPERTIES
WORKING_DIRECTORY "${TEST_WORKING_DIR}"
${properties}
)
list(APPEND tests "${prefix}${test}${suffix}")
endforeach()
# Create a list of all discovered tests, which users may use to e.g. set
# properties on the tests
add_command(set ${TEST_LIST} ${tests})
# Write CTest script
file(WRITE "${CTEST_FILE}" "${script}")

View File

@ -136,3 +136,5 @@ if(ENABLE_LINK)
PRIVATE ${NLS_LIBS}
)
endif()
add_subdirectory(test)

View File

@ -0,0 +1,13 @@
# This defines the `vbam-core-fake` library, which is used for providing a fake
# implementation of the core library for testing purposes.
if(NOT BUILD_TESTING)
return()
endif()
add_library(vbam-core-fake OBJECT)
target_sources(vbam-core-fake
PRIVATE
fake_core.cpp
)

View File

@ -0,0 +1,94 @@
#include "core/base/system.h"
void systemMessage(int, const char*, ...) {}
void log(const char*, ...) {}
bool systemPauseOnFrame() {
return false;
}
void systemGbPrint(uint8_t*, int, int, int, int, int) {}
void systemScreenCapture(int) {}
void systemDrawScreen() {}
void systemSendScreen() {}
bool systemReadJoypads() {
return false;
}
uint32_t systemReadJoypad(int) {
return 0;
}
uint32_t systemGetClock() {
return 0;
}
void systemSetTitle(const char*) {}
std::unique_ptr<SoundDriver> systemSoundInit() {
return nullptr;
}
void systemOnWriteDataToSoundBuffer(const uint16_t* /*finalWave*/, int /*length*/) {}
void systemOnSoundShutdown() {}
void systemScreenMessage(const char*) {}
void systemUpdateMotionSensor() {}
int systemGetSensorX() {
return 0;
}
int systemGetSensorY() {
return 0;
}
int systemGetSensorZ() {
return 0;
}
uint8_t systemGetSensorDarkness() {
return 0;
}
void systemCartridgeRumble(bool) {}
void systemPossibleCartridgeRumble(bool) {}
void updateRumbleFrame() {}
bool systemCanChangeSoundQuality() {
return false;
}
void systemShowSpeed(int) {}
void system10Frames() {}
void systemFrame() {}
void systemGbBorderOn() {}
void (*dbgOutput)(const char* s, uint32_t addr);
void (*dbgSignal)(int sig, int number);
uint16_t systemColorMap16[0x10000];
uint32_t systemColorMap32[0x10000];
uint16_t systemGbPalette[24];
int systemRedShift;
int systemGreenShift;
int systemBlueShift;
int systemColorDepth;
int systemVerbose;
int systemFrameSkip;
int systemSaveUpdateCounter;
int systemSpeed;
int emulating = 0;

View File

@ -207,7 +207,6 @@ foreach(DEF ${wxWidgets_DEFINITIONS})
list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D${DEF}")
endforeach()
# Sets the wxWidgets dependencies for a given target.
function(configure_wx_deps target)
get_target_property(target_type ${target} TYPE)
if(target_type STREQUAL "EXECUTABLE")
@ -230,6 +229,7 @@ endif()
endfunction()
# Sub-projects.
add_subdirectory(test)
add_subdirectory(config)
set(VBAM_ICON visualboyadvance-m.icns)
@ -970,10 +970,6 @@ install(
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(BUILD_TESTING AND (NOT CMAKE_CROSSCOMPILING))
add_subdirectory(tests)
endif()
# Installation scripts.
install(
PROGRAMS ${PROJECT_BINARY_DIR}/visualboyadvance-m${CMAKE_EXECUTABLE_SUFFIX}

View File

@ -69,3 +69,20 @@ target_link_libraries(vbam-wx-config
configure_wx_deps(vbam-wx-config)
target_include_directories(vbam-wx-config PUBLIC ${NONSTD_INCLUDE_DIR})
if (BUILD_TESTING)
add_executable(vbam-wx-config-tests
strutils_test.cpp
)
target_link_libraries(vbam-wx-config-tests
vbam-core
vbam-core-fake
vbam-wx-config
vbam-wx-fake-opts
GTest::gtest_main
)
if (NOT CMAKE_CROSSCOMPILING)
gtest_discover_tests(vbam-wx-config-tests)
endif()
endif()

View File

@ -0,0 +1,96 @@
#include "wx/config/strutils.h"
#include <gtest/gtest.h>
TEST(StrSplitTest, Basic) {
wxString foo = "foo|bar|baz";
auto vec = config::str_split(foo, '|');
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
}
TEST(StrSplitTest, MultiCharSep) {
wxString foo = "foo|-|bar|-|baz";
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
}
TEST(StrSplitTest, SkipEmptyToken) {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 3);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
}
TEST(StrSplitTest, EmptyInput) {
wxString foo;
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 0);
}
TEST(StrSplitTest, NoTokens) {
wxString foo = "|-||-||-||-||-|";
auto vec = config::str_split(foo, "|-|");
EXPECT_EQ(vec.size(), 0);
}
TEST(StrSplitWithSepTest, Basic) {
wxString foo = "foo|bar|baz|";
auto vec = config::str_split_with_sep(foo, '|');
EXPECT_EQ(vec.size(), 4);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
EXPECT_EQ(vec[3], "|");
}
TEST(StrSplitWithSepTest, MultiCharSep) {
wxString foo = "foo|-|bar|-|baz|-|";
auto vec = config::str_split_with_sep(foo, "|-|");
EXPECT_EQ(vec.size(), 4);
EXPECT_EQ(vec[0], "foo");
EXPECT_EQ(vec[1], "bar");
EXPECT_EQ(vec[2], "baz");
EXPECT_EQ(vec[3], "|-|");
}
TEST(StrSplitWithSepTest, MultipleSepTokens) {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = config::str_split_with_sep(foo, "|-|");
EXPECT_EQ(vec.size(), 6);
EXPECT_EQ(vec[0], "|-|");
EXPECT_EQ(vec[1], "foo");
EXPECT_EQ(vec[2], "|-|");
EXPECT_EQ(vec[3], "bar");
EXPECT_EQ(vec[4], "baz");
EXPECT_EQ(vec[5], "|-|");
}

View File

@ -0,0 +1,15 @@
# This defines the `vbam-wx-fake-opts` library, which is used for providing
# a fake implementation of the gopts object, for testing purposes.
if(NOT BUILD_TESTING)
return()
endif()
add_library(vbam-wx-fake-opts OBJECT)
target_sources(vbam-wx-fake-opts
PRIVATE
fake_opts.cpp
)
configure_wx_deps(vbam-wx-fake-opts)

View File

@ -0,0 +1,5 @@
#include "wx/opts.h"
opts_t gopts;
opts_t::opts_t() {}

View File

@ -1,32 +0,0 @@
# TODO: Does not link on CLANG64.
if(MSYS AND CMAKE_CXX_COMPILER_ID STREQUAL Clang)
return()
endif()
include(doctest)
function(add_doctest_test test_src)
string(REGEX REPLACE ".cpp$" "" test_name "${test_src}")
add_executable("${test_name}" "${ARGV}")
target_link_libraries("${test_name}" ${wxWidgets_LIBRARIES})
target_include_directories("${test_name}"
PRIVATE ${wxWidgets_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/third_party/include)
target_compile_options("${test_name}" PRIVATE ${wxWidgets_CXX_FLAGS})
target_compile_definitions("${test_name}" PRIVATE ${wxWidgets_DEFINITIONS})
if(CMAKE_BUILD_TYPE MATCHES "^(Debug|RelWithDebInfo)$")
target_compile_definitions("${test_name}" PRIVATE ${wxWidgets_DEFINITIONS_DEBUG})
endif()
set_target_properties("${test_name}"
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
)
doctest_discover_tests("${test_name}")
endfunction()
add_doctest_test(strutils.cpp ../config/strutils.h ../config/strutils.cpp)

View File

@ -1,96 +0,0 @@
#include "wx/config/strutils.h"
#include "tests.hpp"
TEST_CASE("config::str_split() basic test") {
wxString foo = "foo|bar|baz";
auto vec = config::str_split(foo, '|');
CHECK(vec.size() == 3);
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
}
TEST_CASE("config::str_split() multi-char separator") {
wxString foo = "foo|-|bar|-|baz";
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 3);
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
}
TEST_CASE("config::str_split() skips empty tokens") {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 3);
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
}
TEST_CASE("config::str_split() empty input") {
wxString foo;
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 0);
}
TEST_CASE("config::str_split() no tokens, just separators") {
wxString foo = "|-||-||-||-||-|";
auto vec = config::str_split(foo, "|-|");
CHECK(vec.size() == 0);
}
TEST_CASE("config::str_split_with_sep() basic test") {
wxString foo = "foo|bar|baz|";
auto vec = config::str_split_with_sep(foo, '|');
CHECK(vec.size() == 4);
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
CHECK(vec[3] == "|");
}
TEST_CASE("config::str_split_with_sep() multi-char sep") {
wxString foo = "foo|-|bar|-|baz|-|";
auto vec = config::str_split_with_sep(foo, "|-|");
CHECK(vec.size() == 4);
CHECK(vec[0] == "foo");
CHECK(vec[1] == "bar");
CHECK(vec[2] == "baz");
CHECK(vec[3] == "|-|");
}
TEST_CASE("config::str_split_with_sep() multiple sep tokens") {
wxString foo = "|-|foo|-||-|bar|-|baz|-|";
auto vec = config::str_split_with_sep(foo, "|-|");
CHECK(vec.size() == 6);
CHECK(vec[0] == "|-|");
CHECK(vec[1] == "foo");
CHECK(vec[2] == "|-|");
CHECK(vec[3] == "bar");
CHECK(vec[4] == "baz");
CHECK(vec[5] == "|-|");
}

View File

@ -1,13 +0,0 @@
#ifndef TESTS_HPP
#define TESTS_HPP
#ifdef _MSC_VER
# define DOCTEST_CONFIG_USE_STD_HEADERS
#endif
#define DOCTEST_THREAD_LOCAL // Avoid MinGW thread_local bug.
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#endif

File diff suppressed because it is too large Load Diff