Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed May 9, 2024
1 parent 2d7a180 commit 61db737
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "vendor/minhook"]
path = vendor/minhook
url = https://github.com/TsudaKageyu/minhook
[submodule "vendor/catch2"]
path = vendor/catch2
url = https://github.com/catchorg/Catch2
29 changes: 29 additions & 0 deletions TestHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <catch_amalgamated.hpp>

#include "util/Helpers.h"

TEST_CASE("can convert string to int")
{
SECTION("read numbers")
{
auto value = Helpers::StringToInt("1024");

REQUIRE(value == 1024);
}

SECTION("read units")
{
auto value1 = Helpers::StringToInt("256K");
auto value2 = Helpers::StringToInt("256M");

REQUIRE(value1 == 0x40000);
REQUIRE(value2 == 0x10000000);
}

SECTION("returns a default")
{
auto value = Helpers::StringToInt("lara", 42);

REQUIRE(value == 42);
}
}
34 changes: 19 additions & 15 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,12 @@ project "TRAE-menu-hook"

links { "d3d9.lib" }

-- Source files'
-- Source files
files "src/**"
includedirs { "src" }

-- Vendor files
files {
"vendor/minhook/src/**",
"vendor/patterns/*.cpp",
"vendor/imgui/*.cpp",
"vendor/imgui/backends/imgui_impl_win32.cpp",
"vendor/imgui/backends/imgui_impl_dx9.cpp"
}

includedirs {
"vendor/minhook/include",
"vendor/patterns",
"vendor/imgui",
"vendor/imgui/backends"
}
dofile "vendor.lua"

defines { "IMGUI_IMPL_WIN32_DISABLE_GAMEPAD" }

Expand All @@ -56,3 +43,20 @@ project "TRAE-menu-hook"
filter "platforms:TR8"
defines { "TR8" }
targetname "TR8-Menu-Hook"

project "Tests"
kind "ConsoleApp"

language "C++"
cppdialect "C++17"

files {
"tests/**",
"src/**",
"vendor/catch2/extras/catch_amalgamated.cpp"
}

includedirs { "src", "vendor/catch2/extras" }
dofile "vendor.lua"

defines { "TR7" }
8 changes: 0 additions & 8 deletions src/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,6 @@ void Hook::OnDevice()
PostInitialize();
}

template<typename T>
void Hook::RegisterModule()
{
auto mod = std::make_shared<T>();

m_modules.insert({ typeid(T).hash_code(), mod });
}

void Hook::RegisterModules()
{
// Register these first
Expand Down
10 changes: 8 additions & 2 deletions src/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class Hook

void PostInitialize();

template<typename T>
void RegisterModule();
void RegisterModules();

void OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
Expand All @@ -30,6 +28,14 @@ class Hook

// These need to be defined here, else the linker becomes angry

// Adds a module
template<typename T>
void RegisterModule()
{
auto mod = std::make_shared<T>();
m_modules.insert({ typeid(T).hash_code(), mod });
}

// Gets all modules
const auto& GetModules() const noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion src/util/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class Helpers
{
public:
// Converts a value to integer with support for units such as megabytes
static int StringToInt(const std::string& value, int defaultValue);
static int StringToInt(const std::string& value, int defaultValue = 0);
};
23 changes: 23 additions & 0 deletions tests/TestModules.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <catch_amalgamated.hpp>

#include "Hook.h"

class TestModule : public Module
{
public:
int GetValue() const noexcept { return 42; }
};

TEST_CASE("can register and get modules")
{
auto& hook = Hook::GetInstance();
hook.RegisterModule<TestModule>();

auto test = hook.GetModule<TestModule>();

REQUIRE_FALSE(test == nullptr);

auto value = test->GetValue();

REQUIRE(value == 42);
}
14 changes: 14 additions & 0 deletions vendor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
files {
"vendor/minhook/src/**",
"vendor/patterns/*.cpp",
"vendor/imgui/*.cpp",
"vendor/imgui/backends/imgui_impl_win32.cpp",
"vendor/imgui/backends/imgui_impl_dx9.cpp"
}

includedirs {
"vendor/minhook/include",
"vendor/patterns",
"vendor/imgui",
"vendor/imgui/backends"
}
1 change: 1 addition & 0 deletions vendor/catch2
Submodule catch2 added at 4e8d92

0 comments on commit 61db737

Please sign in to comment.