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>
This commit is contained in:
iTrooz
2024-02-26 20:51:08 +01:00
committed by GitHub
parent 032ef0722d
commit 47362559ef
28 changed files with 205 additions and 69 deletions

View File

@@ -1,9 +1,10 @@
project(unit_tests)
add_custom_target(unit_tests DEPENDS helpers algorithms)
add_custom_target(unit_tests DEPENDS imhex_all helpers algorithms plugins)
add_subdirectory(common)
target_compile_definitions(tests_common PUBLIC IMHEX_PROJECT_NAME="${PROJECT_NAME}")
add_subdirectory(helpers)
add_subdirectory(algorithms)
add_subdirectory(plugins)

View File

@@ -5,5 +5,4 @@ project(tests_common)
add_library(tests_common STATIC
source/main.cpp
)
target_include_directories(tests_common PUBLIC include)
target_link_libraries(tests_common PUBLIC libimhex ${FMT_LIBRARIES} libwolv)

View File

@@ -1,62 +0,0 @@
#pragma once
#include <hex/providers/provider.hpp>
#include <nlohmann/json.hpp>
namespace hex::test {
using namespace hex::prv;
class TestProvider : public prv::Provider {
public:
explicit TestProvider(std::vector<u8> *data) {
this->setData(data);
}
~TestProvider() override = default;
[[nodiscard]] bool isAvailable() const override { return true; }
[[nodiscard]] bool isReadable() const override { return true; }
[[nodiscard]] bool isWritable() const override { return false; }
[[nodiscard]] bool isResizable() const override { return false; }
[[nodiscard]] bool isSavable() const override { return false; }
void setData(std::vector<u8> *data) {
m_data = data;
}
[[nodiscard]] std::string getName() const override {
return "";
}
[[nodiscard]] std::vector<Description> getDataDescription() const override {
return {};
}
void readRaw(u64 offset, void *buffer, size_t size) override {
if (offset + size > m_data->size()) return;
std::memcpy(buffer, m_data->data() + offset, size);
}
void writeRaw(u64 offset, const void *buffer, size_t size) override {
if (offset + size > m_data->size()) return;
std::memcpy(m_data->data() + offset, buffer, size);
}
[[nodiscard]] u64 getActualSize() const override {
return m_data->size();
}
[[nodiscard]] std::string getTypeName() const override { return "hex.test.provider.test"; }
bool open() override { return true; }
void close() override { }
nlohmann::json storeSettings(nlohmann::json) const override { return {}; }
void loadSettings(const nlohmann::json &) override {};
private:
std::vector<u8> *m_data = nullptr;
};
}

View File

@@ -1,89 +0,0 @@
#pragma once
#include <hex.hpp>
#include <utility>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.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)
namespace hex::test {
struct Test {
std::function<int()> function;
bool shouldFail;
};
class Tests {
public:
static auto addTest(const std::string &name, const std::function<int()> &func, bool shouldFail) noexcept {
s_tests.insert({
name, {func, shouldFail}
});
return 0;
}
static auto &get() noexcept {
return s_tests;
}
private:
static inline 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());
}
}

View File

@@ -3,8 +3,10 @@
#include <hex/api/event_manager.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/test/tests.hpp>
#include <hex/api/plugin_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <hex/api/event_manager.hpp>
#include <cstdlib>
@@ -48,7 +50,10 @@ int main(int argc, char **argv) {
else
hex::log::info("Failed!");
hex::TaskManager::exit();
hex::EventImHexClosing::post();
hex::EventManager::clear();
hex::PluginManager::unload();
return result;
}

View File

@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.16)
project(plugins_test)
add_executable(${PROJECT_NAME}
source/plugins.cpp
)
# ---- No need to change anything from here downwards unless you know what you're doing ---- #
target_include_directories(${PROJECT_NAME} PRIVATE include)
target_link_libraries(${PROJECT_NAME} PRIVATE libimhex tests_common ${FMT_LIBRARIES})
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
add_dependencies(unit_tests ${PROJECT_NAME})

View File

@@ -0,0 +1,16 @@
#include <hex/api/plugin_manager.hpp>
#include <hex/helpers/utils.hpp>
using namespace hex;
class PluginLoader {
public:
PluginLoader() {
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) {
PluginManager::addLoadPath(dir);
}
PluginManager::load();
}
};
static PluginLoader pluginLoader;