Skip to content

Commit 8e53227

Browse files
authored
Merge pull request #10 from TheIndra55/tests
Add tests
2 parents 2d7a180 + d8e5ca8 commit 8e53227

File tree

10 files changed

+155
-36
lines changed

10 files changed

+155
-36
lines changed

.github/workflows/build.yml

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,36 @@ name: Build
33
on: [push, pull_request]
44

55
jobs:
6+
test:
7+
runs-on: windows-latest
8+
9+
steps:
10+
# setup
11+
- uses: actions/checkout@v4
12+
with:
13+
submodules: true
14+
15+
- uses: microsoft/[email protected]
16+
- uses: ilammy/msvc-dev-cmd@v1
17+
18+
- name: Download premake5
19+
run: |
20+
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
21+
tar -xf premake5.zip
22+
23+
- name: Generate project files
24+
run: .\premake5 vs2022 --with-tests
25+
26+
# tests
27+
- name: Build tests
28+
run: MSBuild TRAE-menu-hook.sln /t:Tests /p:Configuration=Release /p:Platform=TR7
29+
30+
- name: Test
31+
run: .\bin\TR7\Release\Tests.exe
32+
633
build:
734
runs-on: windows-latest
35+
needs: test
836

937
steps:
1038
# setup
@@ -29,16 +57,23 @@ jobs:
2957
run: .\premake5 vs2022
3058

3159
# compile
60+
- name: Build Legend
61+
run: MSBuild /p:Configuration=Release /p:Platform=TR7
62+
3263
- name: Build Anniversary
3364
run: MSBuild /p:Configuration=Release /p:Platform=TRAE
3465

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

38-
- name: Build Legend
39-
run: MSBuild /p:Configuration=Release /p:Platform=TR7
40-
4169
# upload
70+
- uses: actions/upload-artifact@v3
71+
with:
72+
name: Legend
73+
path: |
74+
bin/TR7/Release/TR7-Menu-Hook.asi
75+
bin/TR7/Release/TR7-Menu-Hook.pdb
76+
4277
- uses: actions/upload-artifact@v3
4378
with:
4479
name: Anniversary
@@ -52,10 +87,3 @@ jobs:
5287
path: |
5388
bin/TR8/Release/TR8-Menu-Hook.asi
5489
bin/TR8/Release/TR8-Menu-Hook.pdb
55-
56-
- uses: actions/upload-artifact@v3
57-
with:
58-
name: Legend
59-
path: |
60-
bin/TR7/Release/TR7-Menu-Hook.asi
61-
bin/TR7/Release/TR7-Menu-Hook.pdb

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "vendor/minhook"]
88
path = vendor/minhook
99
url = https://github.com/TsudaKageyu/minhook
10+
[submodule "vendor/catch2"]
11+
path = vendor/catch2
12+
url = https://github.com/catchorg/Catch2

premake5.lua

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
newoption {
2+
trigger = "with-tests",
3+
description = "Include the tests project"
4+
}
5+
16
workspace "TRAE-menu-hook"
27
architecture "x86"
38
configurations { "Debug", "Release" }
49
platforms { "TR7", "TRAE", "TR8" }
510

11+
-- Main project
612
project "TRAE-menu-hook"
713
kind "SharedLib"
814
targetextension ".asi"
@@ -12,25 +18,12 @@ project "TRAE-menu-hook"
1218

1319
links { "d3d9.lib" }
1420

15-
-- Source files'
21+
-- Source files
1622
files "src/**"
1723
includedirs { "src" }
1824

1925
-- Vendor files
20-
files {
21-
"vendor/minhook/src/**",
22-
"vendor/patterns/*.cpp",
23-
"vendor/imgui/*.cpp",
24-
"vendor/imgui/backends/imgui_impl_win32.cpp",
25-
"vendor/imgui/backends/imgui_impl_dx9.cpp"
26-
}
27-
28-
includedirs {
29-
"vendor/minhook/include",
30-
"vendor/patterns",
31-
"vendor/imgui",
32-
"vendor/imgui/backends"
33-
}
26+
dofile "vendor.lua"
3427

3528
defines { "IMGUI_IMPL_WIN32_DISABLE_GAMEPAD" }
3629

@@ -56,3 +49,33 @@ project "TRAE-menu-hook"
5649
filter "platforms:TR8"
5750
defines { "TR8" }
5851
targetname "TR8-Menu-Hook"
52+
53+
-- Tests
54+
if _OPTIONS["with-tests"] then
55+
56+
project "Tests"
57+
kind "ConsoleApp"
58+
59+
language "C++"
60+
cppdialect "C++17"
61+
62+
files {
63+
"tests/**",
64+
"src/**",
65+
"vendor/catch2/extras/catch_amalgamated.cpp"
66+
}
67+
68+
includedirs { "src", "vendor/catch2/extras" }
69+
dofile "vendor.lua"
70+
71+
filter "configurations:Debug"
72+
defines { "DEBUG", "_DEBUG" }
73+
74+
filter "configurations:Release"
75+
defines { "NDEBUG" }
76+
optimize "On"
77+
78+
-- Define this as dummy game
79+
defines { "TR7" }
80+
81+
end

src/Hook.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ void Hook::OnDevice()
118118
PostInitialize();
119119
}
120120

121-
template<typename T>
122-
void Hook::RegisterModule()
123-
{
124-
auto mod = std::make_shared<T>();
125-
126-
m_modules.insert({ typeid(T).hash_code(), mod });
127-
}
128-
129121
void Hook::RegisterModules()
130122
{
131123
// Register these first

src/Hook.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class Hook
1414

1515
void PostInitialize();
1616

17-
template<typename T>
18-
void RegisterModule();
1917
void RegisterModules();
2018

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

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

31+
// Adds a module
32+
template<typename T>
33+
void RegisterModule()
34+
{
35+
auto mod = std::make_shared<T>();
36+
m_modules.insert({ typeid(T).hash_code(), mod });
37+
}
38+
3339
// Gets all modules
3440
const auto& GetModules() const noexcept
3541
{

src/util/Helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ class Helpers
66
{
77
public:
88
// Converts a value to integer with support for units such as megabytes
9-
static int StringToInt(const std::string& value, int defaultValue);
9+
static int StringToInt(const std::string& value, int defaultValue = 0);
1010
};

tests/TestHelpers.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <catch_amalgamated.hpp>
2+
3+
#include "util/Helpers.h"
4+
5+
TEST_CASE("can convert string to int")
6+
{
7+
SECTION("read numbers")
8+
{
9+
auto value = Helpers::StringToInt("1024");
10+
11+
REQUIRE(value == 1024);
12+
}
13+
14+
SECTION("read units")
15+
{
16+
auto value1 = Helpers::StringToInt("256K");
17+
auto value2 = Helpers::StringToInt("256M");
18+
19+
REQUIRE(value1 == 0x40000);
20+
REQUIRE(value2 == 0x10000000);
21+
}
22+
23+
SECTION("returns a default")
24+
{
25+
auto value = Helpers::StringToInt("lara", 42);
26+
27+
REQUIRE(value == 42);
28+
}
29+
}

tests/TestModules.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <catch_amalgamated.hpp>
2+
3+
#include "Hook.h"
4+
5+
class TestModule : public Module
6+
{
7+
public:
8+
int GetValue() const noexcept { return 42; }
9+
};
10+
11+
TEST_CASE("can register and get modules")
12+
{
13+
auto& hook = Hook::GetInstance();
14+
hook.RegisterModule<TestModule>();
15+
16+
auto test = hook.GetModule<TestModule>();
17+
18+
REQUIRE_FALSE(test == nullptr);
19+
20+
auto value = test->GetValue();
21+
22+
REQUIRE(value == 42);
23+
}

vendor.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
files {
2+
"vendor/minhook/src/**",
3+
"vendor/patterns/*.cpp",
4+
"vendor/imgui/*.cpp",
5+
"vendor/imgui/backends/imgui_impl_win32.cpp",
6+
"vendor/imgui/backends/imgui_impl_dx9.cpp"
7+
}
8+
9+
includedirs {
10+
"vendor/minhook/include",
11+
"vendor/patterns",
12+
"vendor/imgui",
13+
"vendor/imgui/backends"
14+
}

vendor/catch2

Submodule catch2 added at 4e8d92b

0 commit comments

Comments
 (0)