From e337bd9ffcc9141feb63cc25fb0d2cc5be4c755e Mon Sep 17 00:00:00 2001 From: przemek83 <4788832+przemek83@users.noreply.github.com> Date: Mon, 7 Oct 2024 13:32:32 +0200 Subject: [PATCH] Add test for checking victory condition when base is destroyed. --- test/CMakeLists.txt | 1 + test/Common.h | 29 ++++++ test/GameTest.cpp | 25 ++++-- test/MapTest.cpp | 208 ++++++++++++++++++++------------------------ 4 files changed, 143 insertions(+), 120 deletions(-) create mode 100644 test/Common.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d2cf164..49488b2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,6 +13,7 @@ set(${PROJECT_TEST}_SOURCES MenuTest.cpp StatusTest.cpp GameTest.cpp + Common.h ) add_executable(${PROJECT_TEST} ${${PROJECT_TEST}_SOURCES}) diff --git a/test/Common.h b/test/Common.h new file mode 100644 index 0000000..aa9ffc3 --- /dev/null +++ b/test/Common.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include +#include + +namespace common +{ +const int tileCount{5}; + +inline std::string getTestMap() +{ + return { + "E01TA\n" + "01SL1\n" + "E6010\n" + "0213M\n" + "50401\n"}; +} + +inline Point tileToPoint(int tileX, int tileY) +{ + static const int tileSize{Config::getInstance().getTileSize()}; + return {tileX * tileSize, tileY * tileSize}; +} + +const int testHitStrength{10}; +}; // namespace common diff --git a/test/GameTest.cpp b/test/GameTest.cpp index 6e10a87..43127fe 100644 --- a/test/GameTest.cpp +++ b/test/GameTest.cpp @@ -1,23 +1,29 @@ +#include +#include + #include +#include #include #include +#include -#include "src/Config.h" -#include "src/TankType.h" -#include "test/FakeDisplay.h" +#include "Common.h" +#include "FakeDisplay.h" TEST_CASE("Check winning conditions", "[Game]") { FakeDisplay display; Config::getInstance().setDefaultSleepTimeInSeconds(0); - std::list tanks; Point point{0, 0}; - Map map{3}; + Map map(common::tileCount); + std::stringstream stream(common::getTestMap()); + std::list tanks{map.loadMap(stream)}; SECTION("Check no more enemies") { + tanks.clear(); tanks.emplace_back(TankType::PLAYER_TIER_1, point); Game game(tanks, map); REQUIRE(game.isGameEnding(display)); @@ -25,9 +31,14 @@ TEST_CASE("Check winning conditions", "[Game]") SECTION("Check game continues") { - tanks.emplace_back(TankType::PLAYER_TIER_1, point); - tanks.emplace_back(TankType::ENEMY_TIER_1, point); Game game(tanks, map); REQUIRE(!game.isGameEnding(display)); } + + SECTION("Check base destroyed") + { + Game game(tanks, map); + map.hit(common::tileToPoint(1, 2), common::testHitStrength); + REQUIRE(game.isGameEnding(display)); + } } diff --git a/test/MapTest.cpp b/test/MapTest.cpp index 3c42509..8a3cd80 100644 --- a/test/MapTest.cpp +++ b/test/MapTest.cpp @@ -9,6 +9,7 @@ #include #include +#include "Common.h" #include "FakeDisplay.h" // 0 - plain @@ -25,33 +26,10 @@ // A - shield up // T - life up -namespace -{ -const int tileCount{5}; - -std::string getTestMap() -{ - return { - "E01TA\n" - "01SL1\n" - "E6010\n" - "0213M\n" - "50401\n"}; -} - -Point tileToPoint(int tileX, int tileY) -{ - static const int tileSize{Config::getInstance().getTileSize()}; - return {tileX * tileSize, tileY * tileSize}; -} - -const int testHitStrength{10}; -} // namespace - TEST_CASE("Map loading", "[map]") { - Map map(tileCount); - std::stringstream stream(getTestMap()); + Map map(common::tileCount); + std::stringstream stream(common::getTestMap()); SECTION("check number of tanks") { @@ -73,38 +51,38 @@ TEST_CASE("Map loading", "[map]") TEST_CASE("Check driving and flying", "[map]") { - Map map(tileCount); - std::stringstream stream(getTestMap()); + Map map(common::tileCount); + std::stringstream stream(common::getTestMap()); SECTION("check driving") { using TestData = std::pair; const auto [point, expectedCanDrive] = - GENERATE(TestData{tileToPoint(0, 0), true}, // enemy - TestData{tileToPoint(1, 0), true}, // plain - TestData{tileToPoint(2, 0), false}, // brick - TestData{tileToPoint(3, 0), true}, // life - TestData{tileToPoint(4, 0), true}, // shiel - TestData{tileToPoint(0, 1), true}, // plain - TestData{tileToPoint(1, 1), false}, // brick - TestData{tileToPoint(2, 1), true}, // speed - TestData{tileToPoint(3, 1), true}, // tier - TestData{tileToPoint(4, 1), false}, // brick - TestData{tileToPoint(0, 2), true}, // enemy - TestData{tileToPoint(1, 2), false}, // base - TestData{tileToPoint(2, 2), true}, // plain - TestData{tileToPoint(3, 2), false}, // brick - TestData{tileToPoint(4, 2), true}, // plain - TestData{tileToPoint(0, 3), true}, // plain - TestData{tileToPoint(1, 3), false}, // water - TestData{tileToPoint(2, 3), false}, // brick - TestData{tileToPoint(3, 3), true}, // plant - TestData{tileToPoint(4, 3), true}, // playe - TestData{tileToPoint(0, 4), false}, // steel - TestData{tileToPoint(1, 4), true}, // plain - TestData{tileToPoint(2, 4), true}, // ice - TestData{tileToPoint(3, 4), true}, // plain - TestData{tileToPoint(4, 4), false}); // brick + GENERATE(TestData{common::tileToPoint(0, 0), true}, // enemy + TestData{common::tileToPoint(1, 0), true}, // plain + TestData{common::tileToPoint(2, 0), false}, // brick + TestData{common::tileToPoint(3, 0), true}, // life + TestData{common::tileToPoint(4, 0), true}, // shiel + TestData{common::tileToPoint(0, 1), true}, // plain + TestData{common::tileToPoint(1, 1), false}, // brick + TestData{common::tileToPoint(2, 1), true}, // speed + TestData{common::tileToPoint(3, 1), true}, // tier + TestData{common::tileToPoint(4, 1), false}, // brick + TestData{common::tileToPoint(0, 2), true}, // enemy + TestData{common::tileToPoint(1, 2), false}, // base + TestData{common::tileToPoint(2, 2), true}, // plain + TestData{common::tileToPoint(3, 2), false}, // brick + TestData{common::tileToPoint(4, 2), true}, // plain + TestData{common::tileToPoint(0, 3), true}, // plain + TestData{common::tileToPoint(1, 3), false}, // water + TestData{common::tileToPoint(2, 3), false}, // brick + TestData{common::tileToPoint(3, 3), true}, // plant + TestData{common::tileToPoint(4, 3), true}, // playe + TestData{common::tileToPoint(0, 4), false}, // steel + TestData{common::tileToPoint(1, 4), true}, // plain + TestData{common::tileToPoint(2, 4), true}, // ice + TestData{common::tileToPoint(3, 4), true}, // plain + TestData{common::tileToPoint(4, 4), false}); // brick map.loadMap(stream); REQUIRE(map.canDrive(point) == expectedCanDrive); @@ -114,32 +92,31 @@ TEST_CASE("Check driving and flying", "[map]") { using TestData = std::pair; const auto [point, expectedCanFly] = - GENERATE(TestData{tileToPoint(0, 0), true}, // enemy - TestData{tileToPoint(1, 0), true}, // plain - TestData{tileToPoint(2, 0), false}, // brick - TestData{tileToPoint(3, 0), true}, // life up - TestData{tileToPoint(4, 0), true}, // shield up - TestData{tileToPoint(0, 1), true}, // plain - TestData{tileToPoint(1, 1), false}, // brick - TestData{tileToPoint(2, 1), true}, // speed up - TestData{tileToPoint(3, 1), true}, // tier up - TestData{tileToPoint(4, 1), false}, // brick - TestData{tileToPoint(0, 2), true}, // enemy - TestData{tileToPoint(1, 2), false}, // base - TestData{tileToPoint(2, 2), true}, // plain - TestData{tileToPoint(3, 2), false}, // brick - TestData{tileToPoint(4, 2), true}, // plain - TestData{tileToPoint(0, 3), true}, // plain - TestData{tileToPoint(1, 3), true}, // water - TestData{tileToPoint(2, 3), false}, // brick - TestData{tileToPoint(3, 3), true}, // plant - TestData{tileToPoint(4, 3), true}, // player - TestData{tileToPoint(0, 4), false}, // steel - TestData{tileToPoint(1, 4), true}, // plain - TestData{tileToPoint(2, 4), true}, // ice - TestData{tileToPoint(3, 4), true}, // plain - TestData{tileToPoint(4, 4), false}); // brick - + GENERATE(TestData{common::tileToPoint(0, 0), true}, // enemy + TestData{common::tileToPoint(1, 0), true}, // plain + TestData{common::tileToPoint(2, 0), false}, // brick + TestData{common::tileToPoint(3, 0), true}, // life up + TestData{common::tileToPoint(4, 0), true}, // shield up + TestData{common::tileToPoint(0, 1), true}, // plain + TestData{common::tileToPoint(1, 1), false}, // brick + TestData{common::tileToPoint(2, 1), true}, // speed up + TestData{common::tileToPoint(3, 1), true}, // tier up + TestData{common::tileToPoint(4, 1), false}, // brick + TestData{common::tileToPoint(0, 2), true}, // enemy + TestData{common::tileToPoint(1, 2), false}, // base + TestData{common::tileToPoint(2, 2), true}, // plain + TestData{common::tileToPoint(3, 2), false}, // brick + TestData{common::tileToPoint(4, 2), true}, // plain + TestData{common::tileToPoint(0, 3), true}, // plain + TestData{common::tileToPoint(1, 3), true}, // water + TestData{common::tileToPoint(2, 3), false}, // brick + TestData{common::tileToPoint(3, 3), true}, // plant + TestData{common::tileToPoint(4, 3), true}, // player + TestData{common::tileToPoint(0, 4), false}, // steel + TestData{common::tileToPoint(1, 4), true}, // plain + TestData{common::tileToPoint(2, 4), true}, // ice + TestData{common::tileToPoint(3, 4), true}, // plain + TestData{common::tileToPoint(4, 4), false}); // brick map.loadMap(stream); REQUIRE(map.canFly(point) == expectedCanFly); } @@ -147,70 +124,70 @@ TEST_CASE("Check driving and flying", "[map]") TEST_CASE("Check hitting", "[map]") { - Map map(tileCount); - std::stringstream stream(getTestMap()); + Map map(common::tileCount); + std::stringstream stream(common::getTestMap()); SECTION("check tiles reaction for hit") { using TestData = std::pair; const auto [point, expectedCanDriveAndFly] = - GENERATE(TestData{tileToPoint(4, 1), true}, // brick - TestData{tileToPoint(1, 2), true}, // base - TestData{tileToPoint(0, 4), false}); // steel + GENERATE(TestData{common::tileToPoint(4, 1), true}, // brick + TestData{common::tileToPoint(1, 2), true}, // base + TestData{common::tileToPoint(0, 4), false}); // steel map.loadMap(stream); - map.hit(point, testHitStrength); + map.hit(point, common::testHitStrength); REQUIRE(map.canDrive(point) == expectedCanDriveAndFly); REQUIRE(map.canFly(point) == expectedCanDriveAndFly); } SECTION("check water reaction for hit") { - const Point point{tileToPoint(1, 3)}; + const Point point{common::tileToPoint(1, 3)}; map.loadMap(stream); - map.hit(point, testHitStrength); + map.hit(point, common::testHitStrength); REQUIRE(map.canDrive(point) == false); REQUIRE(map.canFly(point) == true); } SECTION("check base status after hit") { - const Point point{tileToPoint(1, 2)}; + const Point point{common::tileToPoint(1, 2)}; map.loadMap(stream); REQUIRE(map.isBaseDestroyed() == false); - map.hit(point, testHitStrength); + map.hit(point, common::testHitStrength); REQUIRE(map.isBaseDestroyed() == true); } SECTION("hitting tile multiple times") { - const Point point{tileToPoint(1, 2)}; + const Point point{common::tileToPoint(1, 2)}; map.loadMap(stream); REQUIRE(map.isBaseDestroyed() == false); - map.hit(point, utils::getMidpoint(testHitStrength)); + map.hit(point, utils::getMidpoint(common::testHitStrength)); REQUIRE(map.isBaseDestroyed() == true); } } TEST_CASE("Power ups", "[map]") { - Map map(tileCount); - std::stringstream stream(getTestMap()); + Map map(common::tileCount); + std::stringstream stream(common::getTestMap()); SECTION("taking power up") { using TestData = std::pair; const auto [point, expectedPowerup] = - GENERATE(TestData{tileToPoint(2, 0), false}, // brick - TestData{tileToPoint(3, 0), true}, // life up - TestData{tileToPoint(4, 0), true}, // shield up - TestData{tileToPoint(0, 1), false}, // plain - TestData{tileToPoint(2, 1), true}, // speed up - TestData{tileToPoint(3, 1), true}, // tier up - TestData{tileToPoint(1, 2), false}, // base - TestData{tileToPoint(3, 3), false}, // plant - TestData{tileToPoint(0, 4), false}, // ice - TestData{tileToPoint(2, 4), false}); // brick + GENERATE(TestData{common::tileToPoint(2, 0), false}, // brick + TestData{common::tileToPoint(3, 0), true}, // life up + TestData{common::tileToPoint(4, 0), true}, // shield up + TestData{common::tileToPoint(0, 1), false}, // plain + TestData{common::tileToPoint(2, 1), true}, // speed up + TestData{common::tileToPoint(3, 1), true}, // tier up + TestData{common::tileToPoint(1, 2), false}, // base + TestData{common::tileToPoint(3, 3), false}, // plant + TestData{common::tileToPoint(0, 4), false}, // ice + TestData{common::tileToPoint(2, 4), false}); // brick map.loadMap(stream); auto [currentPowerup, _]{map.takePowerUp(point)}; @@ -219,10 +196,11 @@ TEST_CASE("Power ups", "[map]") SECTION("retaking power up") { - const Point point = GENERATE(Point{tileToPoint(3, 0)}, // life up - Point{tileToPoint(4, 0)}, // shield up - Point{tileToPoint(2, 1)}, // speed up - Point{tileToPoint(3, 1)}); // tier up + const Point point = + GENERATE(Point{common::tileToPoint(3, 0)}, // life up + Point{common::tileToPoint(4, 0)}, // shield up + Point{common::tileToPoint(2, 1)}, // speed up + Point{common::tileToPoint(3, 1)}); // tier up map.loadMap(stream); auto [currentPowerup, _]{map.takePowerUp(point)}; @@ -234,11 +212,15 @@ TEST_CASE("Power ups", "[map]") SECTION("recognizing power ups") { using TestData = std::pair; - auto [point, expectedResourceType] = GENERATE( - TestData{tileToPoint(3, 0), ResourceType::LIFE_UP}, // life up - TestData{tileToPoint(4, 0), ResourceType::SHIELD_UP}, // shield up - TestData{tileToPoint(2, 1), ResourceType::SPEED_UP}, // speed up - TestData{tileToPoint(3, 1), ResourceType::TIER_UP}); // tier up + auto [point, expectedResourceType] = + GENERATE(TestData{common::tileToPoint(3, 0), + ResourceType::LIFE_UP}, // life up + TestData{common::tileToPoint(4, 0), + ResourceType::SHIELD_UP}, // shield up + TestData{common::tileToPoint(2, 1), + ResourceType::SPEED_UP}, // speed up + TestData{common::tileToPoint(3, 1), + ResourceType::TIER_UP}); // tier up map.loadMap(stream); auto [_, currentResourceType] = map.takePowerUp(point); @@ -311,8 +293,8 @@ TEST_CASE("changed area", "[map]") { const int tileSize{Config::getInstance().getTileSize()}; - Map map(tileCount); - std::stringstream stream(getTestMap()); + Map map(common::tileCount); + std::stringstream stream(common::getTestMap()); map.loadMap(stream); FakeDisplay display;