1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/test/battle/CHealthTest.cpp

243 lines
6.5 KiB
C++
Raw Normal View History

2017-07-09 21:15:06 +02:00
/*
* CHealthTest.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "mock/mock_UnitHealthInfo.h"
2017-07-09 21:15:06 +02:00
#include "../../lib/CStack.h"
using namespace testing;
2017-07-09 21:31:00 +02:00
static const int32_t UNIT_HEALTH = 123;
static const int32_t UNIT_AMOUNT = 300;
class HealthTest : public ::testing::Test
2017-07-09 21:15:06 +02:00
{
public:
UnitHealthInfoMock mock;
HealthTest() : health(&mock)
{}
2017-07-09 21:15:06 +02:00
void setDefaultExpectations()
2017-07-09 21:15:06 +02:00
{
EXPECT_CALL(mock, unitMaxHealth()).WillRepeatedly(Return(UNIT_HEALTH));
EXPECT_CALL(mock, unitBaseAmount()).WillRepeatedly(Return(UNIT_AMOUNT));
}
CHealth health;
2017-07-09 21:15:06 +02:00
};
static void checkTotal(const CHealth & health, const UnitHealthInfoMock & mock)
2017-07-09 21:15:06 +02:00
{
EXPECT_EQ(health.total(), mock.unitMaxHealth() * mock.unitBaseAmount());
2017-07-09 21:15:06 +02:00
}
static void checkEmptyHealth(const CHealth & health, const UnitHealthInfoMock & mock)
2017-07-09 21:15:06 +02:00
{
checkTotal(health, mock);
EXPECT_EQ(health.getCount(), 0);
EXPECT_EQ(health.getFirstHPleft(), 0);
EXPECT_EQ(health.getResurrected(), 0);
EXPECT_EQ(health.available(), 0);
2017-07-09 21:15:06 +02:00
}
static void checkFullHealth(const CHealth & health, const UnitHealthInfoMock & mock)
2017-07-09 21:15:06 +02:00
{
checkTotal(health, mock);
EXPECT_EQ(health.getCount(), mock.unitBaseAmount());
EXPECT_EQ(health.getFirstHPleft(), mock.unitMaxHealth());
EXPECT_EQ(health.getResurrected(), 0);
EXPECT_EQ(health.available(), mock.unitMaxHealth() * mock.unitBaseAmount());
2017-07-09 21:15:06 +02:00
}
static void checkDamage(CHealth & health, const int32_t initialDamage, const int32_t expectedDamage)
{
int32_t damage = initialDamage;
health.damage(damage);
EXPECT_EQ(damage, expectedDamage);
2017-07-09 21:15:06 +02:00
}
static void checkNormalDamage(CHealth & health, const int32_t initialDamage)
{
checkDamage(health, initialDamage, initialDamage);
}
static void checkNoDamage(CHealth & health, const int32_t initialDamage)
{
checkDamage(health, initialDamage, 0);
}
static void checkHeal(CHealth & health, EHealLevel level, EHealPower power, const int32_t initialHeal, const int32_t expectedHeal)
{
int32_t heal = initialHeal;
health.heal(heal, level, power);
EXPECT_EQ(heal, expectedHeal);
2017-07-09 21:15:06 +02:00
}
TEST_F(HealthTest, empty)
2017-07-09 21:15:06 +02:00
{
setDefaultExpectations();
checkEmptyHealth(health, mock);
2017-07-09 21:15:06 +02:00
health.init();
checkFullHealth(health, mock);
2017-07-09 21:15:06 +02:00
health.reset();
checkEmptyHealth(health, mock);
2017-07-09 21:15:06 +02:00
}
TEST_F(HealthTest, damage)
2017-07-09 21:15:06 +02:00
{
setDefaultExpectations();
health.init();
2017-07-09 21:15:06 +02:00
checkNormalDamage(health, 0);
checkFullHealth(health, mock);
2017-07-09 21:15:06 +02:00
checkNormalDamage(health, mock.unitMaxHealth() - 1);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT);
EXPECT_EQ(health.getFirstHPleft(), 1);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:15:06 +02:00
checkNormalDamage(health, 1);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:15:06 +02:00
2017-07-09 21:31:00 +02:00
checkNormalDamage(health, UNIT_HEALTH * (UNIT_AMOUNT - 1));
checkEmptyHealth(health, mock);
2017-07-09 21:15:06 +02:00
checkNoDamage(health, 1337);
checkEmptyHealth(health, mock);
2017-07-09 21:15:06 +02:00
}
TEST_F(HealthTest, heal)
2017-07-09 21:15:06 +02:00
{
setDefaultExpectations();
health.init();
2017-07-09 21:15:06 +02:00
checkNormalDamage(health, 99);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH-99);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:15:06 +02:00
checkHeal(health, EHealLevel::HEAL, EHealPower::PERMANENT, 9, 9);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH-90);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:15:06 +02:00
checkHeal(health, EHealLevel::RESURRECT, EHealPower::ONE_BATTLE, 40, 40);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH-50);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:15:06 +02:00
checkHeal(health, EHealLevel::OVERHEAL, EHealPower::PERMANENT, 50, 50);
checkFullHealth(health, mock);
2017-07-09 21:15:06 +02:00
}
TEST_F(HealthTest, resurrectOneBattle)
2017-07-09 21:31:00 +02:00
{
setDefaultExpectations();
health.init();
2017-07-09 21:31:00 +02:00
checkNormalDamage(health, UNIT_HEALTH);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:31:00 +02:00
checkHeal(health, EHealLevel::RESURRECT, EHealPower::ONE_BATTLE, UNIT_HEALTH, UNIT_HEALTH);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 1);
2017-07-09 21:31:00 +02:00
checkNormalDamage(health, UNIT_HEALTH);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:31:00 +02:00
health.init();
2017-07-14 22:43:03 +02:00
checkNormalDamage(health, UNIT_HEALTH);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-14 22:43:03 +02:00
health.takeResurrected();
EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-14 22:43:03 +02:00
health.init();
2017-07-09 21:31:00 +02:00
checkNormalDamage(health, UNIT_HEALTH * UNIT_AMOUNT);
checkEmptyHealth(health, mock);
2017-07-09 21:31:00 +02:00
checkHeal(health, EHealLevel::RESURRECT, EHealPower::ONE_BATTLE, UNIT_HEALTH * UNIT_AMOUNT, UNIT_HEALTH * UNIT_AMOUNT);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), UNIT_AMOUNT);
2017-07-09 21:31:00 +02:00
health.takeResurrected();
checkEmptyHealth(health, mock);
2017-07-09 21:31:00 +02:00
}
TEST_F(HealthTest, resurrectPermanent)
2017-07-09 21:31:00 +02:00
{
setDefaultExpectations();
health.init();
2017-07-09 21:31:00 +02:00
checkNormalDamage(health, UNIT_HEALTH);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:31:00 +02:00
checkHeal(health, EHealLevel::RESURRECT, EHealPower::PERMANENT, UNIT_HEALTH, UNIT_HEALTH);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:31:00 +02:00
checkNormalDamage(health, UNIT_HEALTH);
EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1);
EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH);
EXPECT_EQ(health.getResurrected(), 0);
2017-07-09 21:31:00 +02:00
health.init();
checkNormalDamage(health, UNIT_HEALTH * UNIT_AMOUNT);
checkEmptyHealth(health, mock);
2017-07-09 21:31:00 +02:00
checkHeal(health, EHealLevel::RESURRECT, EHealPower::PERMANENT, UNIT_HEALTH * UNIT_AMOUNT, UNIT_HEALTH * UNIT_AMOUNT);
checkFullHealth(health, mock);
2017-07-09 21:31:00 +02:00
health.takeResurrected();
checkFullHealth(health, mock);
2017-07-09 21:31:00 +02:00
}
TEST_F(HealthTest, singleUnitStack)
2017-07-17 01:50:38 +02:00
{
//related to issue 2612
//one Titan
EXPECT_CALL(mock, unitMaxHealth()).WillRepeatedly(Return(300));
EXPECT_CALL(mock, unitBaseAmount()).WillRepeatedly(Return(1));
2017-07-17 01:50:38 +02:00
health.init();
checkDamage(health, 1000, 300);
checkEmptyHealth(health, mock);
2017-07-17 01:50:38 +02:00
checkHeal(health, EHealLevel::RESURRECT, EHealPower::PERMANENT, 300, 300);
checkFullHealth(health, mock);
2017-07-17 01:50:38 +02:00
}