From ed5ac2e9239eb873591a92f4f686c1f7cfe734e4 Mon Sep 17 00:00:00 2001 From: verysecrethero Date: Wed, 13 Jan 2016 14:37:56 +0100 Subject: [PATCH] 6 new tests for CapturePointSystem have been created! TODO: refactor CapturePointSystem --- src/Tests/CapturePointTest.cpp | 369 ++++++++++++++++++++++++++++++--- src/Tests/CapturePointTest.h | 16 +- 2 files changed, 354 insertions(+), 31 deletions(-) diff --git a/src/Tests/CapturePointTest.cpp b/src/Tests/CapturePointTest.cpp index 1991acc9..32570534 100644 --- a/src/Tests/CapturePointTest.cpp +++ b/src/Tests/CapturePointTest.cpp @@ -10,12 +10,11 @@ using boost::unit_test_framework::test_case; #include "Core/EntityFileWriter.h" #include "Game/CapturePointSystem.h" -BOOST_AUTO_TEST_SUITE(ShootEventTestSuite) +BOOST_AUTO_TEST_SUITE(CapturePointTestSuite) //dont use the same name as the classname in test cases... -BOOST_AUTO_TEST_CASE(CapturePointTest1) +BOOST_AUTO_TEST_CASE(CapturePointTest1_OnePlayerOnCapturePoint) { - //Test firing primary weapon CapturePointTest game(1); //100 loops will be more than enough to do the test int loops = 100; @@ -29,7 +28,93 @@ BOOST_AUTO_TEST_CASE(CapturePointTest1) loops--; } //The system will process the events, hence it will take a while before we can read anything - success = true; + BOOST_TEST(success); +} +BOOST_AUTO_TEST_CASE(CapturePointTest2_TwoPlayersOnCapturePoint) +{ + CapturePointTest game(2); + //100 loops will be more than enough to do the test + int loops = 100; + bool success = false; + while (loops > 0) { + game.Tick(); + if (game.TestSucceeded) { + success = true; + break; + } + loops--; + } + //The system will process the events, hence it will take a while before we can read anything + BOOST_TEST(success); +} +BOOST_AUTO_TEST_CASE(CapturePointTest3_NoPlayersOnCapturePoint) +{ + CapturePointTest game(3); + //100 loops will be more than enough to do the test + int loops = 100; + bool success = false; + while (loops > 0) { + game.Tick(); + //successCheck needs to know when were close to 100 to check if anything happened then (NumLoops) + game.NumLoops++; + if (game.TestSucceeded) { + success = true; + break; + } + loops--; + } + //The system will process the events, hence it will take a while before we can read anything + BOOST_TEST(success); +} +BOOST_AUTO_TEST_CASE(CapturePointTest4_TwoCapturePointsBeingCaptured) +{ + CapturePointTest game(4); + //100 loops will be more than enough to do the test + int loops = 100; + bool success = false; + while (loops > 0) { + game.Tick(); + if (game.TestSucceeded) { + success = true; + break; + } + loops--; + } + //The system will process the events, hence it will take a while before we can read anything + BOOST_TEST(success); +} +BOOST_AUTO_TEST_CASE(CapturePointTest5_SameCapturePointContestedAndTakenOver) +{ + CapturePointTest game(5); + //100 loops will be more than enough to do the test + int loops = 100; + bool success = false; + while (loops > 0) { + game.Tick(); + if (game.TestSucceeded) { + success = true; + break; + } + loops--; + } + //The system will process the events, hence it will take a while before we can read anything + BOOST_TEST(success); +} +BOOST_AUTO_TEST_CASE(CapturePointTest6_Team1CapturedTheLastPointAndWon) +{ + CapturePointTest game(6); + //100 loops will be more than enough to do the test + int loops = 100; + bool success = false; + while (loops > 0) { + game.Tick(); + if (game.TestSucceeded) { + success = true; + break; + } + loops--; + } + //The system will process the events, hence it will take a while before we can read anything BOOST_TEST(success); } BOOST_AUTO_TEST_SUITE_END() @@ -65,8 +150,7 @@ CapturePointTest::CapturePointTest(int runTestNumber) fp.MergeEntities(m_World); } - //The Test - //create entity which has transform,player,model,health in it. i.e. is a player + //create 2 players and 3 capturepoints for testing EntityID playerID = m_World->CreateEntity(); m_PlayerID = playerID; ComponentWrapper& player = m_World->AttachComponent(playerID, "Player"); @@ -89,32 +173,42 @@ CapturePointTest::CapturePointTest(int runTestNumber) EntityID capturePointID2 = m_World->CreateEntity(); ComponentWrapper& capturePoint2 = m_World->AttachComponent(capturePointID2, "CapturePoint"); //this capturePoint is homeBase for team 1 - capturePoint2["IsHomeCapturePointForTeamNumber"] = 1; + capturePoint2["IsHomeCapturePointForTeamNumber"] = 0; capturePoint2["CapturePointNumber"] = 1; m_CapturePointID2 = capturePointID2; + EntityID capturePointID3 = m_World->CreateEntity(); + ComponentWrapper& capturePoint3 = m_World->AttachComponent(capturePointID3, "CapturePoint"); + //this capturePoint is homeBase for team 1 + capturePoint3["IsHomeCapturePointForTeamNumber"] = 1; + capturePoint3["CapturePointNumber"] = 2; + m_CapturePointID3 = capturePointID3; + m_RunTestNumber = runTestNumber; - //add some touch/leave events - Events::TriggerTouch eTriggerTouched; - eTriggerTouched.Entity = m_PlayerID; - eTriggerTouched.Trigger = m_CapturePointID; - m_EventBroker->Publish(eTriggerTouched); - - //Events::TriggerTouch eTriggerTouched2; - //eTriggerTouched2.Entity = m_PlayerID2; - //eTriggerTouched2.Trigger = m_CapturePointID; - //m_EventBroker->Publish(eTriggerTouched2); - - Events::TriggerLeave eTriggerLeft; - eTriggerLeft.Entity = m_PlayerID; - eTriggerLeft.Trigger = m_CapturePointID; - m_EventBroker->Publish(eTriggerLeft); - - Events::TriggerTouch eTriggerTouched3; - eTriggerTouched3.Entity = m_PlayerID; - eTriggerTouched3.Trigger = m_CapturePointID2; - m_EventBroker->Publish(eTriggerTouched3); + switch (runTestNumber) + { + case 1: + TestSetup1_OnePlayerOnCapturePoint(); + break; + case 2: + TestSetup2_TwoPlayersOnCapturePoint(); + break; + case 3: + TestSetup3_NoPlayersOnCapturePoint(); + break; + case 4: + TestSetup4_TwoCapturePointsBeingCaptured(); + break; + case 5: + TestSetup5_SameCapturePointContestedAndTakenOver(); + break; + case 6: + TestSetup6_Team1CapturedTheLastPointAndWon(); + break; + default: + break; + } //init glfw so dt works glfwInit(); @@ -127,13 +221,205 @@ CapturePointTest::~CapturePointTest() delete m_EventBroker; } +void CapturePointTest::TestSetup1_OnePlayerOnCapturePoint() +{ + Events::TriggerTouch touchEvent; + Events::TriggerLeave leaveEvent; + + //player touches,leaves,touches m_CapturePointID. and enters m_CapturePointID3 + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); + + leaveEvent.Entity = m_PlayerID; + leaveEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(leaveEvent); + + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); + + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID3; + m_EventBroker->Publish(touchEvent); +} +void CapturePointTest::TestSetup2_TwoPlayersOnCapturePoint() +{ + Events::TriggerTouch touchEvent; + Events::TriggerLeave leaveEvent; + + //player touches,leaves m_CapturePointID. and enters m_CapturePointID3 + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); + + leaveEvent.Entity = m_PlayerID; + leaveEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(leaveEvent); + + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID3; + m_EventBroker->Publish(touchEvent); + + //player2 touches m_CapturePointID,m_CapturePointID2 + touchEvent.Entity = m_PlayerID2; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); + + touchEvent.Entity = m_PlayerID2; + touchEvent.Trigger = m_CapturePointID2; + m_EventBroker->Publish(touchEvent); +} +void CapturePointTest::TestSetup3_NoPlayersOnCapturePoint() +{ + Events::TriggerTouch touchEvent; + Events::TriggerLeave leaveEvent; + + //player1 touches and leaves m_CapturePointID + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); + + leaveEvent.Entity = m_PlayerID; + leaveEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(leaveEvent); + + //player2 touches and leaves m_CapturePointID2 + touchEvent.Entity = m_PlayerID2; + touchEvent.Trigger = m_CapturePointID2; + m_EventBroker->Publish(touchEvent); + + leaveEvent.Entity = m_PlayerID2; + leaveEvent.Trigger = m_CapturePointID2; + m_EventBroker->Publish(leaveEvent); + +} +void CapturePointTest::TestSetup4_TwoCapturePointsBeingCaptured() +{ + Events::TriggerTouch touchEvent; + + //player1 touches m_CapturePointID3 + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID3; + m_EventBroker->Publish(touchEvent); + + //player2 touches m_CapturePointID + touchEvent.Entity = m_PlayerID2; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); +} +void CapturePointTest::TestSetup5_SameCapturePointContestedAndTakenOver() +{ + //NOTE: setup events need to trigger first then the real event will be allowed by the system later + Events::TriggerTouch touchEvent; + + //"SETUP" homebase->same capturep + //player1 touches m_CapturePointID3 + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID3; + m_EventBroker->Publish(touchEvent); + + //player2 touches m_CapturePointID + touchEvent.Entity = m_PlayerID2; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); + + //contested same, player1 touches the contested + //player1 touches m_CapturePointID2 + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID2; + m_EventBroker->Publish(touchEvent); + + //player2 does nothing + +} +void CapturePointTest::TestSetup6_Team1CapturedTheLastPointAndWon() +{ + //NOTE: setup events need to trigger first then the real event will be allowed by the system later + Events::TriggerTouch touchEvent; + + //"SETUP" team1 captures point 2,3 + //player1 touches m_CapturePointID3 + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID3; + m_EventBroker->Publish(touchEvent); + + //player1 touches m_CapturePointID2 + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID2; + m_EventBroker->Publish(touchEvent); + + //team1 captures point 1 + //player1 touches m_CapturePointID + touchEvent.Entity = m_PlayerID; + touchEvent.Trigger = m_CapturePointID; + m_EventBroker->Publish(touchEvent); + + //player2 does nothing +} +void CapturePointTest::TestSuccess1() { + //TestSetup1_OnePlayerOnCapturePoint + + //if ammocount reaches -- we know the test has succeeded, i.e. a shot has been fired + int ownedByID3 = m_World->GetComponent(m_CapturePointID3, "CapturePoint")["OwnedBy"]; + if (ownedByID3 == 1) + TestSucceeded = true; +} +void CapturePointTest::TestSuccess2() { + //TestSetup2_TwoPlayersOnCapturePoint + int ownedByID3 = m_World->GetComponent(m_CapturePointID3, "CapturePoint")["OwnedBy"]; + int ownedByID1 = m_World->GetComponent(m_CapturePointID, "CapturePoint")["OwnedBy"]; + if (ownedByID3 == 1 && ownedByID1 == 2) + TestSucceeded = true; +} +void CapturePointTest::TestSuccess3() { + //TestSetup3_NoPlayersOnCapturePoint + //only do this test if were at the final loopcount + //if any capturePoint changed then, its a failure else a success + if (NumLoops == 95) { + TestSucceeded = true; + int ownedByID1 = m_World->GetComponent(m_CapturePointID, "CapturePoint")["OwnedBy"]; + int ownedByID2 = m_World->GetComponent(m_CapturePointID2, "CapturePoint")["OwnedBy"]; + int ownedByID3 = m_World->GetComponent(m_CapturePointID3, "CapturePoint")["OwnedBy"]; + if (ownedByID1 != 0 || ownedByID2 != 0 || ownedByID3 != 0) + TestSucceeded = false; + } +} +void CapturePointTest::TestSuccess4() { + //TestSetup4_TwoCapturePointsBeingCaptured + int ownedByID1 = m_World->GetComponent(m_CapturePointID, "CapturePoint")["OwnedBy"]; + int ownedByID2 = m_World->GetComponent(m_CapturePointID2, "CapturePoint")["OwnedBy"]; + int ownedByID3 = m_World->GetComponent(m_CapturePointID3, "CapturePoint")["OwnedBy"]; + if (ownedByID1 == 2 && ownedByID3 == 1) + TestSucceeded = true; +} +void CapturePointTest::TestSuccess5() { + //TestSetup5_SameCapturePointContestedAndTakenOver + int ownedByID1 = m_World->GetComponent(m_CapturePointID, "CapturePoint")["OwnedBy"]; + int ownedByID2 = m_World->GetComponent(m_CapturePointID2, "CapturePoint")["OwnedBy"]; + int ownedByID3 = m_World->GetComponent(m_CapturePointID3, "CapturePoint")["OwnedBy"]; + if (ownedByID1 == 2 && ownedByID2 == 1 && ownedByID3 == 1) + TestSucceeded = true; +} +void CapturePointTest::TestSuccess6() { + //NOTE: the actual win-event will have to be manually checked if it triggered or not + //TestSetup6_Team1CapturedTheLastPointAndWon + int ownedByID1 = m_World->GetComponent(m_CapturePointID, "CapturePoint")["OwnedBy"]; + int ownedByID2 = m_World->GetComponent(m_CapturePointID2, "CapturePoint")["OwnedBy"]; + int ownedByID3 = m_World->GetComponent(m_CapturePointID3, "CapturePoint")["OwnedBy"]; + if (ownedByID1 == 1 && ownedByID2 == 1 && ownedByID3 == 1) + TestSucceeded = true; +} void CapturePointTest::Tick() { glfwPollEvents(); - double currentTime = glfwGetTime(); - double dt = currentTime - m_LastTime; - m_LastTime = currentTime; + //double currentTime = glfwGetTime(); + //double dt = currentTime - m_LastTime; + //m_LastTime = currentTime; + + //just set dt to 10.0 since we want fast testing + double dt = 10.0; // Iterate through systems and update world! m_SystemPipeline->Update(m_World, dt); @@ -141,4 +427,27 @@ void CapturePointTest::Tick() m_EventBroker->Swap(); m_EventBroker->Clear(); + switch (m_RunTestNumber) + { + case 1: + TestSuccess1(); + break; + case 2: + TestSuccess2(); + break; + case 3: + TestSuccess3(); + break; + case 4: + TestSuccess4(); + break; + case 5: + TestSuccess5(); + break; + case 6: + TestSuccess6(); + break; + default: + break; + } } diff --git a/src/Tests/CapturePointTest.h b/src/Tests/CapturePointTest.h index 1170c613..89aea389 100644 --- a/src/Tests/CapturePointTest.h +++ b/src/Tests/CapturePointTest.h @@ -27,6 +27,20 @@ public: void Tick(); bool TestSucceeded = false; + int NumLoops = 0; + + void TestSetup1_OnePlayerOnCapturePoint(); + void TestSetup2_TwoPlayersOnCapturePoint(); + void TestSetup3_NoPlayersOnCapturePoint(); + void TestSetup4_TwoCapturePointsBeingCaptured(); + void TestSetup5_SameCapturePointContestedAndTakenOver(); + void TestSetup6_Team1CapturedTheLastPointAndWon(); + void TestSuccess1(); + void TestSuccess2(); + void TestSuccess3(); + void TestSuccess4(); + void TestSuccess5(); + void TestSuccess6(); private: double m_LastTime; @@ -34,7 +48,7 @@ private: EventBroker* m_EventBroker; World* m_World; SystemPipeline* m_SystemPipeline; - int m_PlayerID, m_PlayerID2, m_CapturePointID, m_CapturePointID2; + int m_PlayerID, m_PlayerID2, m_CapturePointID, m_CapturePointID2, m_CapturePointID3; int m_RunTestNumber; };