From 1b6035d6c686c2e0ed52e9bbb98546210bf102f3 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 14 Oct 2021 21:19:31 +0200 Subject: [PATCH] tests: Added properly working custom unit tests --- dist/Brewfile | 1 - dist/Dockerfile | 3 +- dist/ImHex-9999.ebuild | 1 - dist/get_deps_archlinux.sh | 3 +- dist/get_deps_debian.sh | 1 - dist/get_deps_fedora.sh | 3 +- dist/get_deps_msys2.sh | 3 +- dist/msys2/PKGBUILD | 3 +- tests/algorithms/CMakeLists.txt | 24 +++++---- tests/algorithms/include/tests.hpp | 75 +++++++++++++++++++++++++++ tests/algorithms/source/hash.cpp | 12 +++-- tests/algorithms/source/main.cpp | 52 +++++++++++++++++++ tests/pattern_language/CMakeLists.txt | 1 - 13 files changed, 156 insertions(+), 26 deletions(-) create mode 100644 tests/algorithms/include/tests.hpp create mode 100644 tests/algorithms/source/main.cpp diff --git a/dist/Brewfile b/dist/Brewfile index e70b5f7c1..a2bc5b94e 100644 --- a/dist/Brewfile +++ b/dist/Brewfile @@ -8,7 +8,6 @@ brew "python3" brew "freetype2" brew "libmagic" brew "pkg-config" -brew "catch2" # TODO: Remove this when XCode version of clang will support the same level as LLVM 10 brew "llvm" diff --git a/dist/Dockerfile b/dist/Dockerfile index 659fccce9..6e6a9d6e2 100644 --- a/dist/Dockerfile +++ b/dist/Dockerfile @@ -16,8 +16,7 @@ RUN pacman -S --needed --noconfirm \ capstone \ python3 \ freetype2 \ - gtk3 \ - catch2 + gtk3 # Clone ImHex RUN git clone https://github.com/WerWolv/ImHex --recurse-submodules /root/ImHex diff --git a/dist/ImHex-9999.ebuild b/dist/ImHex-9999.ebuild index 1723f018b..64e7edfc7 100644 --- a/dist/ImHex-9999.ebuild +++ b/dist/ImHex-9999.ebuild @@ -29,6 +29,5 @@ RDEPEND="${DEPEND} dev-libs/capstone dev-cpp/nlohmann_json x11-libs/gtk+ - dev-cpp/catch " BDEPEND="${DEPEND}" diff --git a/dist/get_deps_archlinux.sh b/dist/get_deps_archlinux.sh index 263152708..a0c172eac 100755 --- a/dist/get_deps_archlinux.sh +++ b/dist/get_deps_archlinux.sh @@ -9,5 +9,4 @@ pacman -S --needed \ capstone \ python3 \ freetype2 \ - gtk3 \ - catch2 + gtk3 diff --git a/dist/get_deps_debian.sh b/dist/get_deps_debian.sh index ec1651eb2..6c1c504fd 100755 --- a/dist/get_deps_debian.sh +++ b/dist/get_deps_debian.sh @@ -26,7 +26,6 @@ apt install -y \ python3-dev \ libfreetype-dev \ libgtk-3-dev \ - catch echo "Please consider this before running cmake (useful on e.g. Ubuntu 20.04):" echo "export CXX=g++-10" diff --git a/dist/get_deps_fedora.sh b/dist/get_deps_fedora.sh index 6efbe5fa0..4616c710b 100755 --- a/dist/get_deps_fedora.sh +++ b/dist/get_deps_fedora.sh @@ -10,5 +10,4 @@ dnf install \ mbedtls-devel \ python-devel \ freetype-devel \ - gtk3 \ - catch-devel + gtk3 diff --git a/dist/get_deps_msys2.sh b/dist/get_deps_msys2.sh index 1dce18558..70b286f75 100755 --- a/dist/get_deps_msys2.sh +++ b/dist/get_deps_msys2.sh @@ -11,5 +11,4 @@ pacman -S --needed --noconfirm \ mingw-w64-x86_64-mbedtls \ mingw-w64-x86_64-python \ mingw-w64-x86_64-freetype \ - mingw-w64-x86_64-dlfcn \ - mingw-w64-x86_64-catch + mingw-w64-x86_64-dlfcn diff --git a/dist/msys2/PKGBUILD b/dist/msys2/PKGBUILD index 4c280a3ea..ce7688aca 100644 --- a/dist/msys2/PKGBUILD +++ b/dist/msys2/PKGBUILD @@ -18,8 +18,7 @@ makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-mbedtls" "${MINGW_PACKAGE_PREFIX}-polly" "${MINGW_PACKAGE_PREFIX}-python" - "${MINGW_PACKAGE_PREFIX}-freetype" - "${MINGW_PACKAGE_PREFIX}-catch") + "${MINGW_PACKAGE_PREFIX}-freetype") source=() sha256sums=() diff --git a/tests/algorithms/CMakeLists.txt b/tests/algorithms/CMakeLists.txt index 01cdf8eb4..e4cf17932 100644 --- a/tests/algorithms/CMakeLists.txt +++ b/tests/algorithms/CMakeLists.txt @@ -1,15 +1,21 @@ cmake_minimum_required(VERSION 3.16) -project(algorithms_tests) +project(algorithms_test) -find_package(Catch2 REQUIRED) -add_executable(algorithms_tests source/hash.cpp) -target_include_directories(algorithms_tests PRIVATE include) -target_link_libraries(algorithms_tests libimhex Catch2::Catch2) +# Add new tests here # +set(AVAILABLE_TESTS + TestSucceeding + TestFailing +) -set_target_properties(algorithms_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) -include(CTest) -include(Catch) -catch_discover_tests(algorithms_tests TEST_PREFIX "Algorithms/") \ No newline at end of file +add_executable(algorithms_test source/hash.cpp source/main.cpp include/tests.hpp) +target_include_directories(algorithms_test PRIVATE include) +target_link_libraries(algorithms_test libimhex) + +set_target_properties(algorithms_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + +foreach (test IN LISTS AVAILABLE_TESTS) + add_test(NAME "Algorithms/${test}" COMMAND algorithms_test "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +endforeach () \ No newline at end of file diff --git a/tests/algorithms/include/tests.hpp b/tests/algorithms/include/tests.hpp new file mode 100644 index 000000000..e8c2cedf9 --- /dev/null +++ b/tests/algorithms/include/tests.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +#define TEST_SEQUENCE(...) static auto ANONYMOUS_VARIABLE(TEST_SEQUENCE) = ::hex::test::TestSequenceExecutor(__VA_ARGS__) + []() -> int +#define TEST_FAIL() return EXIT_FAILURE; +#define TEST_SUCCESS() return EXIT_SUCCESS; +#define FAILING true + +namespace hex::test { + + struct Test { + std::function function; + bool shouldFail; + }; + + class Tests { + public: + static auto addTest(const std::string &name, const std::function &func, bool shouldFail) noexcept { + s_tests.insert({ name, { func, shouldFail } }); + + return 0; + } + + static auto& get()noexcept { + return s_tests; + } + private: + static inline std::map s_tests; + }; + + template + class TestSequence { + public: + TestSequence(const std::string& name, F func, bool shouldFail) noexcept { + Tests::addTest(name, func, shouldFail); + } + + TestSequence& operator=(TestSequence &&) = delete; + }; + + struct TestSequenceExecutor { + explicit TestSequenceExecutor(std::string name, bool shouldFail = false) noexcept : m_name(std::move(name)), m_shouldFail(shouldFail) { + + } + + [[nodiscard]] + const auto& getName() const noexcept { + return this->m_name; + } + + [[nodiscard]] + bool shouldFail() const noexcept { + return this->m_shouldFail; + } + + private: + std::string m_name; + bool m_shouldFail; + }; + + + template + TestSequence operator+(TestSequenceExecutor executor, F&& f) noexcept { + return TestSequence(executor.getName(), std::forward(f), executor.shouldFail()); + } + +} + diff --git a/tests/algorithms/source/hash.cpp b/tests/algorithms/source/hash.cpp index 83d886732..dff6517ec 100644 --- a/tests/algorithms/source/hash.cpp +++ b/tests/algorithms/source/hash.cpp @@ -1,5 +1,11 @@ -#define CATCH_CONFIG_MAIN -#include - #include #include "test_provider.hpp" +#include "tests.hpp" + +TEST_SEQUENCE("TestSucceeding") { + TEST_SUCCESS(); +}; + +TEST_SEQUENCE("TestFailing", FAILING) { + TEST_FAIL(); +}; \ No newline at end of file diff --git a/tests/algorithms/source/main.cpp b/tests/algorithms/source/main.cpp new file mode 100644 index 000000000..ba0b7982c --- /dev/null +++ b/tests/algorithms/source/main.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include "tests.hpp" + +#include + +int test(int argc, char **argv) { + // Check if a test to run has been provided + if (argc != 2) { + hex::log::fatal("Invalid number of arguments specified! {}", argc); + return EXIT_FAILURE; + } + + // Check if that test exists + std::string testName = argv[1]; + if (!hex::test::Tests::get().contains(testName)) { + hex::log::fatal("No test with name {} found!", testName); + return EXIT_FAILURE; + } + + auto test = hex::test::Tests::get()[testName]; + + auto result = test.function(); + + if (test.shouldFail) { + switch (result) { + case EXIT_SUCCESS: return EXIT_FAILURE; + case EXIT_FAILURE: return EXIT_SUCCESS; + default: return result; + } + } else { + return result; + } +} + +int main(int argc, char **argv) { + int result = EXIT_SUCCESS; + + for (u32 i = 0; i < 16; i++) { + result = test(argc, argv); + if (result != EXIT_SUCCESS) + break; + } + + if (result == EXIT_SUCCESS) + hex::log::info("Success!"); + else + hex::log::info("Failed!"); + + return result; +} \ No newline at end of file diff --git a/tests/pattern_language/CMakeLists.txt b/tests/pattern_language/CMakeLists.txt index 92591aca5..10c89e8e3 100644 --- a/tests/pattern_language/CMakeLists.txt +++ b/tests/pattern_language/CMakeLists.txt @@ -22,7 +22,6 @@ set(AVAILABLE_TESTS ) - add_executable(pattern_language_tests source/main.cpp source/tests.cpp) target_include_directories(pattern_language_tests PRIVATE include) target_link_libraries(pattern_language_tests libimhex)