Skip to content

Commit

Permalink
Add test for checking victory condition when base is destroyed.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Oct 7, 2024
1 parent c0a2bb1 commit e337bd9
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 120 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(${PROJECT_TEST}_SOURCES
MenuTest.cpp
StatusTest.cpp
GameTest.cpp
Common.h
)

add_executable(${PROJECT_TEST} ${${PROJECT_TEST}_SOURCES})
Expand Down
29 changes: 29 additions & 0 deletions test/Common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <string>

#include <src/Config.h>
#include <src/Point.h>

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
25 changes: 18 additions & 7 deletions test/GameTest.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
#include <algorithm>
#include <sstream>

#include <catch2/catch_test_macros.hpp>

#include <src/Config.h>
#include <src/Game.h>
#include <src/Map.h>
#include <src/TankType.h>

#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<Tank> tanks;
Point point{0, 0};
Map map{3};
Map map(common::tileCount);
std::stringstream stream(common::getTestMap());
std::list<Tank> 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));
}

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));
}
}
Loading

0 comments on commit e337bd9

Please sign in to comment.