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:
parent
032ef0722d
commit
47362559ef
4
.github/workflows/tests.yml
vendored
4
.github/workflows/tests.yml
vendored
@ -2,9 +2,8 @@ name: "Unit Tests"
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ master ]
|
branches: ["*"]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ master ]
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@ -49,6 +48,7 @@ jobs:
|
|||||||
cd build
|
cd build
|
||||||
CC=gcc-12 CXX=g++-12 cmake \
|
CC=gcc-12 CXX=g++-12 cmake \
|
||||||
-DCMAKE_BUILD_TYPE=Debug \
|
-DCMAKE_BUILD_TYPE=Debug \
|
||||||
|
-DIMHEX_ENABLE_UNIT_TESTS=ON \
|
||||||
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
|
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
|
||||||
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
|
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
|
||||||
-DCMAKE_C_FLAGS="-fuse-ld=lld -fsanitize=address,leak,undefined -fno-sanitize-recover=all" \
|
-DCMAKE_C_FLAGS="-fuse-ld=lld -fsanitize=address,leak,undefined -fno-sanitize-recover=all" \
|
||||||
|
@ -19,6 +19,7 @@ option(IMHEX_ENABLE_UNITY_BUILD "Enables building ImHex as a unity build
|
|||||||
option(IMHEX_GENERATE_PDBS "Enable generating PDB files in non-debug builds (Windows only)" OFF)
|
option(IMHEX_GENERATE_PDBS "Enable generating PDB files in non-debug builds (Windows only)" OFF)
|
||||||
option(IMHEX_REPLACE_DWARF_WITH_PDB "Remove DWARF information from binaries when generating PDBS (Windows only)" OFF)
|
option(IMHEX_REPLACE_DWARF_WITH_PDB "Remove DWARF information from binaries when generating PDBS (Windows only)" OFF)
|
||||||
option(IMHEX_ENABLE_STD_ASSERTS "Enable debug asserts in the C++ std library. (Breaks Plugin ABI!)" OFF)
|
option(IMHEX_ENABLE_STD_ASSERTS "Enable debug asserts in the C++ std library. (Breaks Plugin ABI!)" OFF)
|
||||||
|
option(IMHEX_ENABLE_UNIT_TESTS "Enable building unit tests" OFF)
|
||||||
|
|
||||||
# Basic compiler and cmake configurations
|
# Basic compiler and cmake configurations
|
||||||
set(CMAKE_CXX_STANDARD 23)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
@ -61,8 +62,10 @@ add_subdirectory(lib/libimhex)
|
|||||||
add_subdirectory(main)
|
add_subdirectory(main)
|
||||||
|
|
||||||
# Add unit tests
|
# Add unit tests
|
||||||
enable_testing()
|
if (IMHEX_ENABLE_UNIT_TESTS)
|
||||||
add_subdirectory(tests EXCLUDE_FROM_ALL)
|
enable_testing()
|
||||||
|
add_subdirectory(tests EXCLUDE_FROM_ALL)
|
||||||
|
endif ()
|
||||||
|
|
||||||
# Configure more resources that will be added to the install package
|
# Configure more resources that will be added to the install package
|
||||||
createPackage()
|
createPackage()
|
||||||
|
@ -88,6 +88,12 @@ macro(add_imhex_plugin)
|
|||||||
elseif (UNIX)
|
elseif (UNIX)
|
||||||
set_target_properties(${IMHEX_PLUGIN_NAME} PROPERTIES INSTALL_RPATH_USE_ORIGIN ON INSTALL_RPATH "$ORIGIN/")
|
set_target_properties(${IMHEX_PLUGIN_NAME} PROPERTIES INSTALL_RPATH_USE_ORIGIN ON INSTALL_RPATH "$ORIGIN/")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt AND IMHEX_ENABLE_UNIT_TESTS)
|
||||||
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||||
|
target_link_libraries(${IMHEX_PLUGIN_NAME} PUBLIC ${IMHEX_PLUGIN_NAME}_tests)
|
||||||
|
target_compile_definitions(${IMHEX_PLUGIN_NAME}_tests PRIVATE IMHEX_PROJECT_NAME="${IMHEX_PLUGIN_NAME}-tests")
|
||||||
|
endif()
|
||||||
endmacro()
|
endmacro()
|
||||||
|
|
||||||
macro(add_romfs_resource input output)
|
macro(add_romfs_resource input output)
|
||||||
|
4
dist/rpm/imhex.spec
vendored
4
dist/rpm/imhex.spec
vendored
@ -95,10 +95,6 @@ CXXFLAGS+=" -std=gnu++2b"
|
|||||||
%set_build_flags
|
%set_build_flags
|
||||||
CXXFLAGS+=" -std=gnu++2b"
|
CXXFLAGS+=" -std=gnu++2b"
|
||||||
%endif
|
%endif
|
||||||
# build binaries required for tests
|
|
||||||
%cmake_build --target unit_tests
|
|
||||||
%ctest --exclude-regex '(Helpers/StoreAPI|Helpers/TipsAPI|Helpers/ContentAPI)'
|
|
||||||
# Helpers/*API exclude tests that require network access
|
|
||||||
|
|
||||||
|
|
||||||
%install
|
%install
|
||||||
|
@ -38,6 +38,8 @@ set(LIBIMHEX_SOURCES
|
|||||||
source/helpers/tar.cpp
|
source/helpers/tar.cpp
|
||||||
source/helpers/debugging.cpp
|
source/helpers/debugging.cpp
|
||||||
|
|
||||||
|
source/test/tests.cpp
|
||||||
|
|
||||||
source/providers/provider.cpp
|
source/providers/provider.cpp
|
||||||
source/providers/memory_provider.cpp
|
source/providers/memory_provider.cpp
|
||||||
source/providers/undo/stack.cpp
|
source/providers/undo/stack.cpp
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <hex.hpp>
|
#include <hex.hpp>
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <mutex>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -308,4 +309,5 @@ namespace hex {
|
|||||||
* The 'from' provider should not have any per provider data after this, and should be immediately deleted
|
* The 'from' provider should not have any per provider data after this, and should be immediately deleted
|
||||||
*/
|
*/
|
||||||
EVENT_DEF(MovePerProviderData, prv::Provider *, prv::Provider *);
|
EVENT_DEF(MovePerProviderData, prv::Provider *, prv::Provider *);
|
||||||
|
|
||||||
}
|
}
|
@ -111,6 +111,7 @@ namespace hex {
|
|||||||
|
|
||||||
static void addPlugin(const std::string &name, PluginFunctions functions);
|
static void addPlugin(const std::string &name, PluginFunctions functions);
|
||||||
|
|
||||||
|
static Plugin* getPlugin(const std::string &name);
|
||||||
static const std::list<Plugin>& getPlugins();
|
static const std::list<Plugin>& getPlugins();
|
||||||
static const std::vector<std::fs::path>& getPluginPaths();
|
static const std::vector<std::fs::path>& getPluginPaths();
|
||||||
static const std::vector<std::fs::path>& getPluginLoadPaths();
|
static const std::vector<std::fs::path>& getPluginLoadPaths();
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <fmt/color.h>
|
#include <fmt/color.h>
|
||||||
|
|
||||||
#include <wolv/io/file.hpp>
|
#include <wolv/io/file.hpp>
|
||||||
|
#include <hex/helpers/fmt.hpp>
|
||||||
|
|
||||||
namespace hex::log {
|
namespace hex::log {
|
||||||
|
|
||||||
@ -88,7 +89,6 @@ namespace hex::log {
|
|||||||
hex::log::impl::print(fg(impl::color::fatal()) | fmt::emphasis::bold, "[FATAL]", fmt, args...);
|
hex::log::impl::print(fg(impl::color::fatal()) | fmt::emphasis::bold, "[FATAL]", fmt, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[[maybe_unused]] void print(const std::string &fmt, auto && ... args) {
|
[[maybe_unused]] void print(const std::string &fmt, auto && ... args) {
|
||||||
std::scoped_lock lock(impl::getLoggerMutex());
|
std::scoped_lock lock(impl::getLoggerMutex());
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ namespace hex::prv {
|
|||||||
*/
|
*/
|
||||||
[[nodiscard]] virtual std::string getName() const = 0;
|
[[nodiscard]] virtual std::string getName() const = 0;
|
||||||
|
|
||||||
void resize(u64 newSize);
|
bool resize(u64 newSize);
|
||||||
void insert(u64 offset, u64 size);
|
void insert(u64 offset, u64 size);
|
||||||
void remove(u64 offset, u64 size);
|
void remove(u64 offset, u64 size);
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <hex/helpers/utils.hpp>
|
#include <hex/helpers/utils.hpp>
|
||||||
#include <hex/helpers/fmt.hpp>
|
#include <hex/helpers/fmt.hpp>
|
||||||
#include <hex/helpers/logger.hpp>
|
#include <hex/helpers/logger.hpp>
|
||||||
|
#include <hex/api/plugin_manager.hpp>
|
||||||
|
|
||||||
#include <wolv/utils/preproc.hpp>
|
#include <wolv/utils/preproc.hpp>
|
||||||
|
|
||||||
@ -28,16 +29,20 @@
|
|||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#define INIT_PLUGIN(name) \
|
||||||
|
if (!hex::test::initPluginImpl(name)) TEST_FAIL();
|
||||||
|
|
||||||
namespace hex::test {
|
namespace hex::test {
|
||||||
|
|
||||||
|
using Function = int(*)();
|
||||||
struct Test {
|
struct Test {
|
||||||
std::function<int()> function;
|
Function function;
|
||||||
bool shouldFail;
|
bool shouldFail;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Tests {
|
class Tests {
|
||||||
public:
|
public:
|
||||||
static auto addTest(const std::string &name, const std::function<int()> &func, bool shouldFail) noexcept {
|
static auto addTest(const std::string &name, Function func, bool shouldFail) noexcept {
|
||||||
s_tests.insert({
|
s_tests.insert({
|
||||||
name, {func, shouldFail}
|
name, {func, shouldFail}
|
||||||
});
|
});
|
||||||
@ -50,7 +55,7 @@ namespace hex::test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static inline std::map<std::string, Test> s_tests;
|
static std::map<std::string, Test> s_tests;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class F>
|
template<class F>
|
||||||
@ -86,4 +91,5 @@ namespace hex::test {
|
|||||||
return TestSequence<F>(executor.getName(), std::forward<F>(f), executor.shouldFail());
|
return TestSequence<F>(executor.getName(), std::forward<F>(f), executor.shouldFail());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool initPluginImpl(std::string name);
|
||||||
}
|
}
|
@ -321,6 +321,15 @@ namespace hex {
|
|||||||
return plugins;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Plugin* PluginManager::getPlugin(const std::string &name) {
|
||||||
|
for (auto &plugin : getPluginsMutable()) {
|
||||||
|
if (plugin.getPluginName() == name)
|
||||||
|
return &plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
const std::vector<std::fs::path>& PluginManager::getPluginPaths() {
|
const std::vector<std::fs::path>& PluginManager::getPluginPaths() {
|
||||||
return s_pluginPaths;
|
return s_pluginPaths;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <hex/api/event_manager.hpp>
|
||||||
#include <hex/helpers/logger.hpp>
|
#include <hex/helpers/logger.hpp>
|
||||||
#include <hex/helpers/fs.hpp>
|
#include <hex/helpers/fs.hpp>
|
||||||
#include <hex/helpers/fmt.hpp>
|
#include <hex/helpers/fmt.hpp>
|
||||||
|
@ -74,7 +74,11 @@ namespace hex::prv {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Provider::resize(u64 newSize) {
|
bool Provider::resize(u64 newSize) {
|
||||||
|
if (newSize >> 63) {
|
||||||
|
log::error("new provider size is very large ({}). Is it a negative number ?", newSize);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
i64 difference = newSize - this->getActualSize();
|
i64 difference = newSize - this->getActualSize();
|
||||||
|
|
||||||
if (difference > 0)
|
if (difference > 0)
|
||||||
@ -83,6 +87,7 @@ namespace hex::prv {
|
|||||||
EventProviderDataRemoved::post(this, this->getActualSize() + difference, -difference);
|
EventProviderDataRemoved::post(this, this->getActualSize() + difference, -difference);
|
||||||
|
|
||||||
this->markDirty();
|
this->markDirty();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Provider::insert(u64 offset, u64 size) {
|
void Provider::insert(u64 offset, u64 size) {
|
||||||
|
23
lib/libimhex/source/test/tests.cpp
Normal file
23
lib/libimhex/source/test/tests.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include <hex/test/tests.hpp>
|
||||||
|
|
||||||
|
namespace hex::test {
|
||||||
|
std::map<std::string, Test> Tests::s_tests;
|
||||||
|
|
||||||
|
bool initPluginImpl(std::string name) {
|
||||||
|
if (name != "Built-in") {
|
||||||
|
if(!initPluginImpl("Built-in")) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
hex::Plugin *plugin = hex::PluginManager::getPlugin(name);
|
||||||
|
if (plugin == nullptr) {
|
||||||
|
hex::log::fatal("Plugin '{}' was not found !", name);
|
||||||
|
return false;
|
||||||
|
}else if (!plugin->initializePlugin()) {
|
||||||
|
hex::log::fatal("Failed to initialize plugin '{}' !", name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
hex::log::info("Initialized plugin '{}' successfully", name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -114,8 +114,6 @@ add_imhex_plugin(
|
|||||||
source/content/views/view_achievements.cpp
|
source/content/views/view_achievements.cpp
|
||||||
source/content/views/view_highlight_rules.cpp
|
source/content/views/view_highlight_rules.cpp
|
||||||
source/content/views/view_tutorials.cpp
|
source/content/views/view_tutorials.cpp
|
||||||
|
|
||||||
source/content/helpers/notification.cpp
|
|
||||||
INCLUDES
|
INCLUDES
|
||||||
include
|
include
|
||||||
|
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
namespace hex::plugin::builtin {
|
|
||||||
|
|
||||||
void showError(const std::string& message);
|
|
||||||
|
|
||||||
void showWarning(const std::string& message);
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
#include <hex/helpers/logger.hpp>
|
|
||||||
|
|
||||||
#include <toasts/toast_notification.hpp>
|
|
||||||
|
|
||||||
namespace hex::plugin::builtin {
|
|
||||||
|
|
||||||
void showError(const std::string& message){
|
|
||||||
ui::ToastError::open(message);
|
|
||||||
log::error(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void showWarning(const std::string& message){
|
|
||||||
ui::ToastWarning::open(message);
|
|
||||||
log::warn(message);
|
|
||||||
}
|
|
||||||
}
|
|
@ -13,8 +13,7 @@
|
|||||||
#include <hex/providers/provider.hpp>
|
#include <hex/providers/provider.hpp>
|
||||||
#include <hex/helpers/fmt.hpp>
|
#include <hex/helpers/fmt.hpp>
|
||||||
#include <hex/helpers/logger.hpp>
|
#include <hex/helpers/logger.hpp>
|
||||||
|
#include <toasts/toast_notification.hpp>
|
||||||
#include <content/helpers/notification.hpp>
|
|
||||||
|
|
||||||
namespace hex::plugin::builtin {
|
namespace hex::plugin::builtin {
|
||||||
|
|
||||||
@ -23,26 +22,29 @@ namespace hex::plugin::builtin {
|
|||||||
|
|
||||||
bool load(const std::fs::path &filePath) {
|
bool load(const std::fs::path &filePath) {
|
||||||
if (!wolv::io::fs::exists(filePath) || !wolv::io::fs::isRegularFile(filePath)) {
|
if (!wolv::io::fs::exists(filePath) || !wolv::io::fs::isRegularFile(filePath)) {
|
||||||
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
||||||
hex::format("hex.builtin.popup.error.project.load.file_not_found"_lang,
|
hex::format("hex.builtin.popup.error.project.load.file_not_found"_lang,
|
||||||
wolv::util::toUTF8String(filePath)
|
wolv::util::toUTF8String(filePath)
|
||||||
)));
|
)));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Tar tar(filePath, Tar::Mode::Read);
|
Tar tar(filePath, Tar::Mode::Read);
|
||||||
if (!tar.isValid()) {
|
if (!tar.isValid()) {
|
||||||
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
||||||
hex::format("hex.builtin.popup.error.project.load.invalid_tar"_lang,
|
hex::format("hex.builtin.popup.error.project.load.invalid_tar"_lang,
|
||||||
tar.getOpenErrorString()
|
tar.getOpenErrorString()
|
||||||
)));
|
)));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tar.contains(MetadataPath)) {
|
if (!tar.contains(MetadataPath)) {
|
||||||
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
||||||
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
|
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
|
||||||
));
|
));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,9 +52,10 @@ namespace hex::plugin::builtin {
|
|||||||
const auto metadataContent = tar.readVector(MetadataPath);
|
const auto metadataContent = tar.readVector(MetadataPath);
|
||||||
|
|
||||||
if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic)) {
|
if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic)) {
|
||||||
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
||||||
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
|
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
|
||||||
));
|
));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,4 +179,4 @@ namespace hex::plugin::builtin {
|
|||||||
void registerProjectHandlers() {
|
void registerProjectHandlers() {
|
||||||
hex::ProjectFile::setProjectFunctions(load, store);
|
hex::ProjectFile::setProjectFunctions(load, store);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,13 @@
|
|||||||
#include <content/providers/process_memory_provider.hpp>
|
#include <content/providers/process_memory_provider.hpp>
|
||||||
#include <content/providers/base64_provider.hpp>
|
#include <content/providers/base64_provider.hpp>
|
||||||
#include <popups/popup_notification.hpp>
|
#include <popups/popup_notification.hpp>
|
||||||
#include "content/helpers/notification.hpp"
|
|
||||||
|
|
||||||
#include <hex/api/project_file_manager.hpp>
|
#include <hex/api/project_file_manager.hpp>
|
||||||
#include <hex/api/task_manager.hpp>
|
#include <hex/api/task_manager.hpp>
|
||||||
#include <hex/helpers/fmt.hpp>
|
#include <hex/helpers/fmt.hpp>
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <toasts/toast_notification.hpp>
|
||||||
|
|
||||||
#include <wolv/utils/guards.hpp>
|
#include <wolv/utils/guards.hpp>
|
||||||
|
|
||||||
@ -70,9 +70,11 @@ namespace hex::plugin::builtin {
|
|||||||
if (newProvider == nullptr) {
|
if (newProvider == nullptr) {
|
||||||
// If a provider is not created, it will be overwritten when saving the project,
|
// If a provider is not created, it will be overwritten when saving the project,
|
||||||
// so we should prevent the project from loading at all
|
// so we should prevent the project from loading at all
|
||||||
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
|
ui::ToastError::open(
|
||||||
hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType)
|
hex::format("hex.builtin.popup.error.project.load"_lang,
|
||||||
));
|
hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType)
|
||||||
|
)
|
||||||
|
);
|
||||||
success = false;
|
success = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -94,29 +96,27 @@ namespace hex::plugin::builtin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string warningMsg;
|
std::string warningMessage;
|
||||||
for (const auto &warning : providerWarnings){
|
for (const auto &warning : providerWarnings){
|
||||||
ImHexApi::Provider::remove(warning.first);
|
ImHexApi::Provider::remove(warning.first);
|
||||||
warningMsg.append(
|
warningMessage.append(
|
||||||
hex::format("\n - {} : {}", warning.first->getName(), warning.second));
|
hex::format("\n - {} : {}", warning.first->getName(), warning.second));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no providers were opened, display an error with
|
// If no providers were opened, display an error with
|
||||||
// the warnings that happened when opening them
|
// the warnings that happened when opening them
|
||||||
if (ImHexApi::Provider::getProviders().size() == 0) {
|
if (ImHexApi::Provider::getProviders().empty()) {
|
||||||
showError(hex::format("hex.builtin.popup.error.project.load"_lang,
|
ui::ToastError::open(hex::format("{}{}", "hex.builtin.popup.error.project.load"_lang, "hex.builtin.popup.error.project.load.no_providers"_lang, warningMessage));
|
||||||
hex::format("hex.builtin.popup.error.project.load.no_providers"_lang)) + warningMsg);
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
// Else, if there are warnings, still display them
|
||||||
// Else, if are warnings, still display them
|
if (warningMessage.empty()) {
|
||||||
if (warningMsg.empty()) {
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
showWarning(
|
ui::ToastWarning::open(hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMessage));
|
||||||
hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMsg));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
19
plugins/builtin/tests/CMakeLists.txt
Normal file
19
plugins/builtin/tests/CMakeLists.txt
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
project(${IMHEX_PLUGIN_NAME}_tests)
|
||||||
|
|
||||||
|
# Add new tests here #
|
||||||
|
set(AVAILABLE_TESTS
|
||||||
|
Providers/ReadWrite
|
||||||
|
Providers/InvalidResize
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(${PROJECT_NAME} SHARED
|
||||||
|
source/main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/plugins/builtin/include)
|
||||||
|
|
||||||
|
target_link_libraries(${PROJECT_NAME} PRIVATE libimhex)
|
||||||
|
|
||||||
|
foreach (test IN LISTS AVAILABLE_TESTS)
|
||||||
|
add_test(NAME "Plugin_${IMHEX_PLUGIN_NAME}/${test}" COMMAND $<TARGET_FILE:plugins_test> "${test}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||||
|
endforeach ()
|
43
plugins/builtin/tests/source/main.cpp
Normal file
43
plugins/builtin/tests/source/main.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <hex/test/tests.hpp>
|
||||||
|
#include <hex/api/plugin_manager.hpp>
|
||||||
|
#include <content/providers/memory_file_provider.hpp>
|
||||||
|
#include <content/views/view_patches.hpp>
|
||||||
|
#include <hex/api/task_manager.hpp>
|
||||||
|
|
||||||
|
using namespace hex;
|
||||||
|
using namespace hex::plugin::builtin;
|
||||||
|
|
||||||
|
TEST_SEQUENCE("Providers/ReadWrite") {
|
||||||
|
INIT_PLUGIN("Built-in");
|
||||||
|
|
||||||
|
auto &pr = *ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true);
|
||||||
|
|
||||||
|
TEST_ASSERT(pr.getSize() == 0);
|
||||||
|
TEST_ASSERT(!pr.isDirty());
|
||||||
|
|
||||||
|
pr.resize(50);
|
||||||
|
TEST_ASSERT(pr.getSize() == 50);
|
||||||
|
TEST_ASSERT(pr.isDirty());
|
||||||
|
|
||||||
|
char buf[] = "\x99\x99"; // temporary value that should be overwriten
|
||||||
|
pr.read(0, buf, 2);
|
||||||
|
TEST_ASSERT(std::equal(buf, buf+2, "\x00\x00"));
|
||||||
|
|
||||||
|
pr.write(0, "\xFF\xFF", 2);
|
||||||
|
char buf2[] = "\x99\x99"; // temporary value that should be overwriten
|
||||||
|
pr.read(0, buf2, 2);
|
||||||
|
TEST_ASSERT(std::equal(buf2, buf2+2, "\xFF\xFF"));
|
||||||
|
|
||||||
|
TEST_SUCCESS();
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_SEQUENCE("Providers/InvalidResize") {
|
||||||
|
INIT_PLUGIN("Built-in");
|
||||||
|
|
||||||
|
auto &pr = *ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true);
|
||||||
|
|
||||||
|
|
||||||
|
TEST_ASSERT(!pr.resize(-1));
|
||||||
|
TEST_SUCCESS();
|
||||||
|
};
|
@ -1,11 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <hex/api/imhex_api.hpp>
|
|
||||||
#include <hex/ui/imgui_imhex_extensions.h>
|
#include <hex/ui/imgui_imhex_extensions.h>
|
||||||
#include <hex/ui/toast.hpp>
|
#include <hex/ui/toast.hpp>
|
||||||
|
|
||||||
#include <fonts/codicons_font.h>
|
#include <fonts/codicons_font.h>
|
||||||
#include <hex/helpers/utils.hpp>
|
#include <hex/helpers/utils.hpp>
|
||||||
|
#include <hex/helpers/logger.hpp>
|
||||||
|
|
||||||
#include <popups/popup_notification.hpp>
|
#include <popups/popup_notification.hpp>
|
||||||
|
|
||||||
@ -49,17 +49,23 @@ namespace hex::ui {
|
|||||||
|
|
||||||
struct ToastInfo : impl::ToastNotification<ToastInfo> {
|
struct ToastInfo : impl::ToastNotification<ToastInfo> {
|
||||||
ToastInfo(std::string message)
|
ToastInfo(std::string message)
|
||||||
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerInfo), ICON_VS_INFO, "hex.ui.common.info", std::move(message)) {}
|
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerInfo), ICON_VS_INFO, "hex.ui.common.info", std::move(message)) {
|
||||||
|
log::info("{}", message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ToastWarning : impl::ToastNotification<ToastWarning> {
|
struct ToastWarning : impl::ToastNotification<ToastWarning> {
|
||||||
ToastWarning(std::string message)
|
ToastWarning(std::string message)
|
||||||
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerWarning), ICON_VS_WARNING, "hex.ui.common.warning", std::move(message)) {}
|
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerWarning), ICON_VS_WARNING, "hex.ui.common.warning", std::move(message)) {
|
||||||
|
log::warn("{}", message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ToastError : impl::ToastNotification<ToastError> {
|
struct ToastError : impl::ToastNotification<ToastError> {
|
||||||
ToastError(std::string message)
|
ToastError(std::string message)
|
||||||
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerError), ICON_VS_ERROR, "hex.ui.common.error", std::move(message)) {}
|
: ToastNotification(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_LoggerError), ICON_VS_ERROR, "hex.ui.common.error", std::move(message)) {
|
||||||
|
log::error("{}", message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
project(unit_tests)
|
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)
|
add_subdirectory(common)
|
||||||
target_compile_definitions(tests_common PUBLIC IMHEX_PROJECT_NAME="${PROJECT_NAME}")
|
target_compile_definitions(tests_common PUBLIC IMHEX_PROJECT_NAME="${PROJECT_NAME}")
|
||||||
|
|
||||||
add_subdirectory(helpers)
|
add_subdirectory(helpers)
|
||||||
add_subdirectory(algorithms)
|
add_subdirectory(algorithms)
|
||||||
|
add_subdirectory(plugins)
|
||||||
|
@ -5,5 +5,4 @@ project(tests_common)
|
|||||||
add_library(tests_common STATIC
|
add_library(tests_common STATIC
|
||||||
source/main.cpp
|
source/main.cpp
|
||||||
)
|
)
|
||||||
target_include_directories(tests_common PUBLIC include)
|
|
||||||
target_link_libraries(tests_common PUBLIC libimhex ${FMT_LIBRARIES} libwolv)
|
target_link_libraries(tests_common PUBLIC libimhex ${FMT_LIBRARIES} libwolv)
|
||||||
|
@ -3,8 +3,10 @@
|
|||||||
#include <hex/api/event_manager.hpp>
|
#include <hex/api/event_manager.hpp>
|
||||||
#include <hex/helpers/utils.hpp>
|
#include <hex/helpers/utils.hpp>
|
||||||
#include <hex/helpers/logger.hpp>
|
#include <hex/helpers/logger.hpp>
|
||||||
|
|
||||||
#include <hex/test/tests.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>
|
#include <cstdlib>
|
||||||
|
|
||||||
@ -48,7 +50,10 @@ int main(int argc, char **argv) {
|
|||||||
else
|
else
|
||||||
hex::log::info("Failed!");
|
hex::log::info("Failed!");
|
||||||
|
|
||||||
|
hex::TaskManager::exit();
|
||||||
|
hex::EventImHexClosing::post();
|
||||||
hex::EventManager::clear();
|
hex::EventManager::clear();
|
||||||
|
hex::PluginManager::unload();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
16
tests/plugins/CMakeLists.txt
Normal file
16
tests/plugins/CMakeLists.txt
Normal 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})
|
16
tests/plugins/source/plugins.cpp
Normal file
16
tests/plugins/source/plugins.cpp
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user