Skip to content

Commit

Permalink
Merge pull request #10 from TheIndra55/tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
TheIndra55 committed May 12, 2024
2 parents 2d7a180 + d8e5ca8 commit 8e53227
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 36 deletions.
48 changes: 38 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,36 @@ name: Build
on: [push, pull_request]

jobs:
test:
runs-on: windows-latest

steps:
# setup
- uses: actions/checkout@v4
with:
submodules: true

- uses: microsoft/[email protected]
- uses: ilammy/msvc-dev-cmd@v1

- name: Download premake5
run: |
curl.exe -o premake5.zip -L https://github.com/premake/premake-core/releases/download/v5.0.0-beta2/premake-5.0.0-beta2-windows.zip
tar -xf premake5.zip
- name: Generate project files
run: .\premake5 vs2022 --with-tests

# tests
- name: Build tests
run: MSBuild TRAE-menu-hook.sln /t:Tests /p:Configuration=Release /p:Platform=TR7

- name: Test
run: .\bin\TR7\Release\Tests.exe

build:
runs-on: windows-latest
needs: test

steps:
# setup
Expand All @@ -29,16 +57,23 @@ jobs:
run: .\premake5 vs2022

# compile
- name: Build Legend
run: MSBuild /p:Configuration=Release /p:Platform=TR7

- name: Build Anniversary
run: MSBuild /p:Configuration=Release /p:Platform=TRAE

- name: Build Underworld
run: MSBuild /p:Configuration=Release /p:Platform=TR8

- name: Build Legend
run: MSBuild /p:Configuration=Release /p:Platform=TR7

# upload
- uses: actions/upload-artifact@v3
with:
name: Legend
path: |
bin/TR7/Release/TR7-Menu-Hook.asi
bin/TR7/Release/TR7-Menu-Hook.pdb
- uses: actions/upload-artifact@v3
with:
name: Anniversary
Expand All @@ -52,10 +87,3 @@ jobs:
path: |
bin/TR8/Release/TR8-Menu-Hook.asi
bin/TR8/Release/TR8-Menu-Hook.pdb
- uses: actions/upload-artifact@v3
with:
name: Legend
path: |
bin/TR7/Release/TR7-Menu-Hook.asi
bin/TR7/Release/TR7-Menu-Hook.pdb
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
53 changes: 38 additions & 15 deletions premake5.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
newoption {
trigger = "with-tests",
description = "Include the tests project"
}

workspace "TRAE-menu-hook"
architecture "x86"
configurations { "Debug", "Release" }
platforms { "TR7", "TRAE", "TR8" }

-- Main project
project "TRAE-menu-hook"
kind "SharedLib"
targetextension ".asi"
Expand All @@ -12,25 +18,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 +49,33 @@ project "TRAE-menu-hook"
filter "platforms:TR8"
defines { "TR8" }
targetname "TR8-Menu-Hook"

-- Tests
if _OPTIONS["with-tests"] then

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"

filter "configurations:Debug"
defines { "DEBUG", "_DEBUG" }

filter "configurations:Release"
defines { "NDEBUG" }
optimize "On"

-- Define this as dummy game
defines { "TR7" }

end
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);
};
29 changes: 29 additions & 0 deletions tests/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);
}
}
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 8e53227

Please sign in to comment.