1
0
mirror of synced 2024-12-14 16:52:53 +01:00
ImHex/lib/libimhex/include/hex/test/tests.hpp
iTrooz 47362559ef
tests: Add infrastructure for testing plugins (#1538)
This PR adds a test architecture to be able to test plugins

Main infrastructure done by @WerWolv

---------

Co-authored-by: WerWolv <werwolv98@gmail.com>
2024-02-26 20:51:08 +01:00

96 lines
2.8 KiB
C++

#pragma once
#include <hex.hpp>
#include <utility>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/api/plugin_manager.hpp>
#include <wolv/utils/preproc.hpp>
#include <string>
#include <map>
#include <functional>
#define TEST_SEQUENCE(...) static auto WOLV_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
#define TEST_ASSERT(x, ...) \
do { \
auto ret = (x); \
if (!ret) { \
hex::log::error("Test assert '" #x "' failed {} at {}:{}", \
hex::format("" __VA_ARGS__), \
__FILE__, \
__LINE__); \
return EXIT_FAILURE; \
} \
} while (0)
#define INIT_PLUGIN(name) \
if (!hex::test::initPluginImpl(name)) TEST_FAIL();
namespace hex::test {
using Function = int(*)();
struct Test {
Function function;
bool shouldFail;
};
class Tests {
public:
static auto addTest(const std::string &name, Function func, bool shouldFail) noexcept {
s_tests.insert({
name, {func, shouldFail}
});
return 0;
}
static auto &get() noexcept {
return s_tests;
}
private:
static std::map<std::string, Test> s_tests;
};
template<class F>
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 m_name;
}
[[nodiscard]] bool shouldFail() const noexcept {
return m_shouldFail;
}
private:
std::string m_name;
bool m_shouldFail;
};
template<typename F>
TestSequence<F> operator+(const TestSequenceExecutor &executor, F &&f) noexcept {
return TestSequence<F>(executor.getName(), std::forward<F>(f), executor.shouldFail());
}
bool initPluginImpl(std::string name);
}