Skip to content

Commit

Permalink
#174 Restructure tests and add mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
IcemarkUK committed Oct 26, 2023
1 parent c1f0aac commit 8a34722
Show file tree
Hide file tree
Showing 28 changed files with 308 additions and 133 deletions.
32 changes: 24 additions & 8 deletions main/midnight/tests/CMakeLists.txt
Expand Up @@ -8,19 +8,35 @@ if(TESTS)

FetchContent_MakeAvailable(Catch2)

FILE(GLOB HDR_TESTS1 tests/${APP_NAME}/*.h)
FILE(GLOB SRC_TESTS1 tests/${APP_NAME}/*.cpp)
FILE(GLOB HDR_TESTS2 tests/*.h)
FILE(GLOB SRC_TESTS2 tests/*.cpp)
FILE(GLOB HDR tests/*.h)
FILE(GLOB SRC tests/*.cpp)

FILE(GLOB_RECURSE HDR_APP tests/${APP_NAME}/*.h)
FILE(GLOB_RECURSE SRC_APP tests/${APP_NAME}/*.cpp)

FILE(GLOB HDR_MOCKS tests/mocks/*.h)
FILE(GLOB SRC_MOCKS tests/mocks/*.cpp)

FILE(GLOB HDR_STEPS tests/steps/*.h)
FILE(GLOB SRC_STEPS tests/steps/*.cpp)

FILE(GLOB HDR_TESTS_TESTS tests/tests/*.h)
FILE(GLOB SRC_TESTS_TESTS tests/tests/*.cpp)

list(APPEND GAME_HEADER
${HDR_TESTS1}
${HDR_TESTS2}
${HDR}
${HDR_APP}
${HDR_TESTS}
${HDR_STEPS}
${HDR_MOCKS}
)

list(APPEND GAME_SOURCE
${SRC_TESTS1}
${SRC_TESTS2}
${SRC}
${SRC_APP}
${SRC_TESTS}
${SRC_STEPS}
${SRC_MOCKS}
)

add_compile_options(-D_UNIT_TESTS_)
Expand Down
Expand Up @@ -6,10 +6,10 @@
//

#include "battle_steps.h"
#include "../../src/tme/baseinc/processor_battle.h"
#include "../../../src/tme/baseinc/processor_battle.h"

static const LPCSTR ch_leader = "CH_LUXOR";
static const LPCSTR ch_follower = "CH_MORKIN";
static const string ch_leader = "CH_LUXOR";
static const string ch_follower = "CH_MORKIN";
static mxcharacter* leader = nullptr;
static mxcharacter* follower = nullptr;
static mxgridref previous_location;
Expand Down
Expand Up @@ -7,7 +7,7 @@

#pragma once

#include "../tme_steps.h"
#include "../../steps/tme_steps.h"

struct BattleSteps
{
Expand Down
Expand Up @@ -7,7 +7,7 @@

#pragma once

#include "../tme_steps.h"
#include "../../steps/tme_steps.h"

class LOMStep
{
Expand Down
@@ -1,11 +1,11 @@
//
// battle_tests.cpp
// revenge
// midnight
//
// Created by Chris Wild on 02/09/2023.
//

#include "battle_steps.h"
#include "../steps/battle_steps.h"

// GIVEN a hard game
// AND a lord has followers
Expand Down
Expand Up @@ -4,8 +4,8 @@
//
// Created by Chris Wild on 25/03/2023.
//
#include "common_steps.h"
#include "../map_steps.h"
#include "../steps/common_steps.h"
#include "../../steps/map_steps.h"

SCENARIO("Morkin can recruit Shadows")
{
Expand Down
Expand Up @@ -5,7 +5,7 @@
// Created by Chris Wild on 03/01/2021.
//

#include "common_steps.h"
#include "../steps/common_steps.h"

TEST_CASE( "Lom Scenario has been setup corretly" )
{
Expand Down
Expand Up @@ -11,7 +11,7 @@
// their command."
//

#include "common_steps.h"
#include "../steps/common_steps.h"

void Victory(std::string text)
{
Expand Down
File renamed without changes.
Expand Up @@ -6,8 +6,8 @@
//
#pragma once

#include "../src/system/moonring.h"
#include "../src/ui/uipanel.h"
#include "../../src/system/moonring.h"
#include "../../src/ui/uipanel.h"

class storymanagermock : public storymanager
{
Expand Down
92 changes: 92 additions & 0 deletions main/midnight/tests/mocks/mocks_entity.h
@@ -0,0 +1,92 @@
//
// mocks.hpp
// midnight
//
// Created by Chris Wild on 26/10/2023
//
#pragma once

#if defined(_LOM_)
#include "../../../../src/tme/scenarios/lom/scenario_lom_internal.h"

#define BASE_CHARACTER mxcharacter
#define BASE_FACTORY lom_entityfactory

#endif

using namespace std;
using namespace tme;

class MockData {
public:
void Clear() {
events.clear();
properties.clear();
}

public:
vector<string> events;
map<string, string> properties;
};


#define TRIGGERED(x) \
SetEvent(__FUNCTION__); \
BASE_CHARACTER::x();

#define TRACK_VOID(x) \
virtual void x() override { SetEvent(__FUNCTION__); BASE_CHARACTER::x(); }

#define HANDLE_BOOL(x) \
virtual bool x() const override { BOOL_OVERRIDE_CHECK(x); }

#define BOOL_OVERRIDE_CHECK(x) \
if ( GetProperty(__FUNCTION__) == "false" ) return false; \
if ( GetProperty(__FUNCTION__) == "true" ) return true; \
BASE_CHARACTER::x()

class mockcharacter : public BASE_CHARACTER
{
public:
void SetEvent( string name ) {
mockData.events.push_back(name);
}

string GetProperty( string name ) const {
return mockData.properties.find(name) == mockData.properties.end()
? ""
: mockData.properties.at(name);
}

bool HasEvent(string event)
{
auto vec = mockData.events;
return ( std::find(vec.begin(), vec.end(), event) != vec.end() );
}

//
//
//

TRACK_VOID ( Cmd_Dead );
TRACK_VOID ( LostFight );
TRACK_VOID ( Dismount );

HANDLE_BOOL ( ShouldLoseHorse );
HANDLE_BOOL ( ShouldDieInFight );

public:
MockData mockData;
};

class mockentityfactory : public BASE_FACTORY
{
public:
mxentity* Create(id_type_t type) override
{
if ( type == IDT_CHARACTER )
return static_cast<mxentity*>(new mockcharacter);

return BASE_FACTORY::Create(type);
}
};
File renamed without changes.
File renamed without changes.
Expand Up @@ -12,3 +12,13 @@ void MapStep::ClearObjectFromLocation(loc_t loc)
{
tme::mx->gamemap->GetAt(loc).object = OB_NONE;
}

void MapStep::SetObjectAtLocation(loc_t loc, mxthing_t thing)
{
tme::mx->gamemap->GetAt(loc).object = thing;
}

mxthing_t MapStep::GetObjectAtLocation(loc_t loc)
{
return (mxthing_t)tme::mx->gamemap->GetAt(loc).object;
}
Expand Up @@ -13,4 +13,7 @@
class MapStep {
public:
static void ClearObjectFromLocation(loc_t loc);
static void SetObjectAtLocation(loc_t loc, mxthing_t thing);
static mxthing_t GetObjectAtLocation(loc_t loc);

};

0 comments on commit 8a34722

Please sign in to comment.