Added 3 PickupSpawnSystem tests.

This commit is contained in:
verysecrethero
2016-02-10 16:25:47 +01:00
parent 1ebe174b94
commit 2d5f71e442
2 changed files with 278 additions and 0 deletions
+205
View File
@@ -0,0 +1,205 @@
#include <boost/test/unit_test.hpp>
using boost::unit_test_framework::test_suite;
using boost::unit_test_framework::test_case;
#include "PickupSpawnTest.h"
BOOST_AUTO_TEST_SUITE(PickupSpawnTestSuite)
//dont use the same name as the classname in test cases...
BOOST_AUTO_TEST_CASE(PickupSpawnTest_HealthPickupRespawns_PlayerHealthPickupEventTriggers)
{
PickupSpawnTest game(1);
bool success = game.Game_Loop_OneHundredTimes();
BOOST_TEST(success);
}
BOOST_AUTO_TEST_CASE(PickupSpawnTest_APlayerAtMaxHealth_CantTakeHealthPickup)
{
PickupSpawnTest game(2);
bool success = game.Game_Loop_OneHundredTimes();
BOOST_TEST(success);
}
BOOST_AUTO_TEST_CASE(PickupSpawnTest_APickupCanRespawnSlowly)
{
PickupSpawnTest game(3);
bool success = game.Game_Loop_OneHundredTimes();
BOOST_TEST(success);
}
BOOST_AUTO_TEST_SUITE_END()
PickupSpawnTest::PickupSpawnTest(int runTestNumber)
{
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
ResourceManager::RegisterType<EntityFile>("EntityFile");
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
m_EventBroker = new EventBroker();
m_World = new World();
// Create system pipeline
m_SystemPipeline = new SystemPipeline(m_World, m_EventBroker);
m_SystemPipeline->AddSystem<HealthSystem>(0);
m_SystemPipeline->AddSystem<PickupSpawnSystem>(1);
//must register components (Components.xsd), else you cant create entities. Easiest done by loading a test xsd file
auto file = ResourceManager::Load<EntityFile>("Schema/Entities/HealthPickup.xml");
EntityFilePreprocessor fpp(file);
fpp.RegisterComponents(m_World);
EntityFileParser fp(file);
//connect the healthpickup to the world
m_HealthPickupID = fp.MergeEntities(m_World);
//create a player
m_PlayerID = m_World->CreateEntity();
auto& player = m_World->AttachComponent(m_PlayerID, "Player");
m_RunTestNumber = runTestNumber;
//further testsetups
TestSetup(m_RunTestNumber);
//init glfw so dt works
glfwInit();
//listen to the 2 events that are related to PickupSpawn
EVENT_SUBSCRIBE_MEMBER(m_HP, &PickupSpawnTest::OnHealthPickup);
EVENT_SUBSCRIBE_MEMBER(m_PS, &PickupSpawnTest::OnPickupSpawned);
}
bool PickupSpawnTest::OnHealthPickup(Events::PlayerHealthPickup& e) {
//very that the event has the correct healthgain number and playerid
if (m_RunTestNumber == 1) {
if (e.HealthAmount == 22.0 && e.Player.ID == m_PlayerID) {
testStage1Success = true;
}
}
if (m_RunTestNumber == 2) {
testStage1Success = false;
}
if (m_RunTestNumber == 3) {
if (e.HealthAmount == 50.0 && e.Player.ID == m_PlayerID) {
testStage1Success = true;
}
}
return true;
}
bool PickupSpawnTest::OnPickupSpawned(Events::PickupSpawned& e) {
//verify that the newly spawned pickup has the same variable values as the original one
if (m_RunTestNumber == 1) {
if ((double)e.Pickup["HealthPickup"]["HealthGain"] == 22.0 && (double)e.Pickup["HealthPickup"]["RespawnTimer"] == 2.0) {
testStage2Success = true;
}
}
if (m_RunTestNumber == 2) {
testStage2Success = false;
}
if (m_RunTestNumber == 3) {
testStage2Success = false;
}
return true;
}
void PickupSpawnTest::TestSetup(int testNumber)
{
//cant use switch here, since each case might initialize different variables
if (m_RunTestNumber == 1) {
//PickupSpawnTest_HealthPickupRespawns_PlayerHealthPickupEventTriggers
auto& healthPickupEW = EntityWrapper(m_World, m_HealthPickupID);
healthPickupEW["HealthPickup"]["RespawnTimer"] = 2.0;
healthPickupEW["HealthPickup"]["HealthGain"] = 22.0;
//create a player
auto& health = m_World->AttachComponent(m_PlayerID, "Health");
health["Health"] = 20.0;
health["MaxHealth"] = 100.0;
Events::TriggerTouch eTriggerTouch;
DoTouchEvent(m_PlayerID, m_HealthPickupID);
}
if (m_RunTestNumber == 2) {
//PickupSpawnTest_APlayerAtMaxHealth_CantTakeHealthPickup
auto& healthPickupEW = EntityWrapper(m_World, m_HealthPickupID);
healthPickupEW["HealthPickup"]["RespawnTimer"] = 1.0;
healthPickupEW["HealthPickup"]["HealthGain"] = 50.0;
//create a player at max health
auto& health = m_World->AttachComponent(m_PlayerID, "Health");
health["Health"] = 100.0;
health["MaxHealth"] = 100.0;
Events::TriggerTouch eTriggerTouch;
DoTouchEvent(m_PlayerID, m_HealthPickupID);
}
if (m_RunTestNumber == 3) {
//PickupSpawnTest_APickupCanRespawnSlowly
auto& healthPickupEW = EntityWrapper(m_World, m_HealthPickupID);
healthPickupEW["HealthPickup"]["RespawnTimer"] = 100.0;
healthPickupEW["HealthPickup"]["HealthGain"] = 50.0;
//create a player
auto& health = m_World->AttachComponent(m_PlayerID, "Health");
health["Health"] = 1.0;
health["MaxHealth"] = 100.0;
Events::TriggerTouch eTriggerTouch;
DoTouchEvent(m_PlayerID, m_HealthPickupID);
}
}
//generic stuff
void PickupSpawnTest::Tick()
{
glfwPollEvents();
//just set dt to 1.0 since we want fast testing
double dt = 1.0;
// Iterate through systems and update world!
m_SystemPipeline->Update(dt);
m_EventBroker->Swap();
m_EventBroker->Clear();
//verify that healthgain event has been published and pickup has respawned
if (m_RunTestNumber == 1 && testStage1Success && testStage2Success) {
m_TestSucceeded = true;
}
//verify that no healthgain event has been published and that no pickup has respawned
if (m_NumLoops > 90 && m_RunTestNumber == 2 && !testStage1Success && !testStage2Success) {
m_TestSucceeded = true;
}
//3: verify that the pickup hasnt spawned
if (m_NumLoops > 90 && m_RunTestNumber == 3 && testStage1Success && !testStage2Success) {
m_TestSucceeded = true;
}
}
bool PickupSpawnTest::Game_Loop_OneHundredTimes() {
//100 loops will be more than enough to do the test
int loops = 100;
bool success = false;
while (loops > 0) {
Tick();
m_NumLoops++;
if (m_TestSucceeded) {
success = true;
break;
}
loops--;
}
return success;
}
PickupSpawnTest::~PickupSpawnTest()
{
delete m_SystemPipeline;
delete m_World;
//delete m_EventBroker;
}
void PickupSpawnTest::DoTouchEvent(EntityID whoDidSomething, EntityID onWhatObject) {
Events::TriggerTouch touchEvent;
touchEvent.Entity = EntityWrapper(m_World, whoDidSomething);
touchEvent.Trigger = EntityWrapper(m_World, onWhatObject);
m_EventBroker->Publish(touchEvent);
}
+73
View File
@@ -0,0 +1,73 @@
#ifndef PickupSpawnTest_h__
#define PickupSpawnTest_h__
#include "Core/ResourceManager.h"
#include "Core/ConfigFile.h"
#include "Core/EventBroker.h"
#include "Core/World.h"
#include "Input/InputProxy.h"
#include "Input/KeyboardInputHandler.h"
#include "Input/MouseInputHandler.h"
#include "Core/EKeyDown.h"
#include "Core/EntityFile.h"
#include "Core/SystemPipeline.h"
#include "Core/EntityFilePreprocessor.h"
#include "Core/EntityFileParser.h"
#include "Core/EntityFileWriter.h"
#include "Engine/Collision/ETrigger.h"
//#include "Core/System.h"
//#include "Core/Transform.h"
//#include "Core/ResourceManager.h"
//#include "Core/EntityFileParser.h"
//#include "Core/EPickupSpawned.h"
//#include "Core/EPlayerHealthPickup.h"
#include "Engine/Collision/ETrigger.h"
//#include "Common.h"
//#include <tuple>
#include "Collision/TriggerSystem.h"
#include "Collision/CollisionSystem.h"
#include "Core/EntityFileWriter.h"
#include "Game/Systems/HealthSystem.h"
#include "Game/Systems/PickupSpawnSystem.h"
#include "Core/ResourceManager.h"
class PickupSpawnTest
{
public:
PickupSpawnTest(int runTestNumber);
~PickupSpawnTest();
void Tick();
bool m_TestSucceeded = false;
int m_NumLoops = 0;
bool Game_Loop_OneHundredTimes();
void TestSetup(int testNumber);
void DoTouchEvent(EntityID whoDidSomething, EntityID onWhatObject);
private:
double m_LastTime;
ConfigFile* m_Config = nullptr;
EventBroker* m_EventBroker;
World* m_World;
SystemPipeline* m_SystemPipeline;
EntityID m_PlayerID, m_HealthPickupID;
int m_RunTestNumber;
EventRelay<PickupSpawnSystem, Events::PlayerHealthPickup> m_HP;
bool OnHealthPickup(Events::PlayerHealthPickup& e);
EventRelay<PickupSpawnSystem, Events::PickupSpawned> m_PS;
bool OnPickupSpawned(Events::PickupSpawned& e);
bool testStage1Success = false;
bool testStage2Success = false;
};
#endif