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"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "mock/mock_battle_Unit.h"
|
|
|
|
#include "mock/mock_BonusBearer.h"
|
|
|
|
#include "../../lib/battle/CUnitState.h"
|
|
|
|
#include "../../lib/NetPacksBase.h"
|
2017-07-09 21:15:06 +02:00
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
using namespace testing;
|
2017-07-20 06:08:49 +02:00
|
|
|
using namespace battle;
|
2017-07-18 21:04:57 +02:00
|
|
|
|
2017-07-09 21:31:00 +02:00
|
|
|
static const int32_t UNIT_HEALTH = 123;
|
|
|
|
static const int32_t UNIT_AMOUNT = 300;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
class HealthTest : public Test
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
|
|
|
public:
|
2017-07-20 06:08:49 +02:00
|
|
|
UnitMock mock;
|
|
|
|
BonusBearerMock bonusMock;
|
2017-07-18 21:18:37 +02:00
|
|
|
HealthTest() : health(&mock)
|
|
|
|
{}
|
2017-07-09 21:15:06 +02:00
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
void setDefaultExpectations()
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
EXPECT_CALL(mock, getAllBonuses(_, _, _, _)).WillRepeatedly(Invoke(&bonusMock, &BonusBearerMock::getAllBonuses));
|
|
|
|
EXPECT_CALL(mock, getTreeVersion()).WillRepeatedly(Return(1));
|
|
|
|
|
|
|
|
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::STACK_HEALTH, Bonus::CREATURE_ABILITY, UNIT_HEALTH, 0));
|
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
EXPECT_CALL(mock, unitBaseAmount()).WillRepeatedly(Return(UNIT_AMOUNT));
|
|
|
|
}
|
|
|
|
CHealth health;
|
2017-07-09 21:15:06 +02:00
|
|
|
};
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static void checkTotal(const CHealth & health, const UnitMock & mock)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
EXPECT_EQ(health.total(), mock.MaxHealth() * mock.unitBaseAmount());
|
2017-07-09 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static void checkEmptyHealth(const CHealth & health, const UnitMock & mock)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
|
|
|
checkTotal(health, mock);
|
2017-07-18 21:04:57 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static void checkFullHealth(const CHealth & health, const UnitMock & mock)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
|
|
|
checkTotal(health, mock);
|
2017-07-18 21:04:57 +02:00
|
|
|
EXPECT_EQ(health.getCount(), mock.unitBaseAmount());
|
2017-07-20 06:08:49 +02:00
|
|
|
EXPECT_EQ(health.getFirstHPleft(), mock.MaxHealth());
|
2017-07-18 21:04:57 +02:00
|
|
|
EXPECT_EQ(health.getResurrected(), 0);
|
2017-07-20 06:08:49 +02:00
|
|
|
EXPECT_EQ(health.available(), mock.MaxHealth() * mock.unitBaseAmount());
|
2017-07-09 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static void checkDamage(CHealth & health, const int64_t initialDamage, const int64_t expectedDamage)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
int64_t damage = initialDamage;
|
2017-07-09 21:15:06 +02:00
|
|
|
health.damage(damage);
|
2017-07-18 21:04:57 +02:00
|
|
|
EXPECT_EQ(damage, expectedDamage);
|
2017-07-09 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static void checkNormalDamage(CHealth & health, const int64_t initialDamage)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
|
|
|
checkDamage(health, initialDamage, initialDamage);
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static void checkNoDamage(CHealth & health, const int64_t initialDamage)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
|
|
|
checkDamage(health, initialDamage, 0);
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
static void checkHeal(CHealth & health, EHealLevel level, EHealPower power, const int64_t initialHeal, const int64_t expectedHeal)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
int64_t heal = initialHeal;
|
2017-07-09 21:15:06 +02:00
|
|
|
health.heal(heal, level, power);
|
2017-07-18 21:04:57 +02:00
|
|
|
EXPECT_EQ(heal, expectedHeal);
|
2017-07-09 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
TEST_F(HealthTest, empty)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
2017-07-18 21:04:57 +02:00
|
|
|
setDefaultExpectations();
|
|
|
|
|
|
|
|
checkEmptyHealth(health, mock);
|
2017-07-09 21:15:06 +02:00
|
|
|
|
|
|
|
health.init();
|
2017-07-18 21:04:57 +02:00
|
|
|
checkFullHealth(health, mock);
|
2017-07-09 21:15:06 +02:00
|
|
|
|
|
|
|
health.reset();
|
2017-07-18 21:04:57 +02:00
|
|
|
checkEmptyHealth(health, mock);
|
2017-07-09 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
|
|
|
|
TEST_F(HealthTest, damage)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
2017-07-18 21:04:57 +02:00
|
|
|
setDefaultExpectations();
|
|
|
|
|
|
|
|
health.init();
|
|
|
|
|
2017-07-09 21:15:06 +02:00
|
|
|
checkNormalDamage(health, 0);
|
2017-07-18 21:04:57 +02:00
|
|
|
checkFullHealth(health, mock);
|
2017-07-09 21:15:06 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
checkNormalDamage(health, mock.MaxHealth() - 1);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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));
|
2017-07-18 21:04:57 +02:00
|
|
|
checkEmptyHealth(health, mock);
|
2017-07-09 21:15:06 +02:00
|
|
|
|
|
|
|
checkNoDamage(health, 1337);
|
2017-07-18 21:04:57 +02:00
|
|
|
checkEmptyHealth(health, mock);
|
2017-07-09 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
TEST_F(HealthTest, heal)
|
2017-07-09 21:15:06 +02:00
|
|
|
{
|
2017-07-18 21:04:57 +02:00
|
|
|
setDefaultExpectations();
|
|
|
|
|
|
|
|
health.init();
|
|
|
|
|
2017-07-09 21:15:06 +02:00
|
|
|
checkNormalDamage(health, 99);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
checkFullHealth(health, mock);
|
2017-07-09 21:15:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
TEST_F(HealthTest, resurrectOneBattle)
|
2017-07-09 21:31:00 +02:00
|
|
|
{
|
2017-07-18 21:04:57 +02:00
|
|
|
setDefaultExpectations();
|
|
|
|
health.init();
|
|
|
|
|
2017-07-09 21:31:00 +02:00
|
|
|
checkNormalDamage(health, UNIT_HEALTH);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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();
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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();
|
2017-07-18 21:04:57 +02:00
|
|
|
checkEmptyHealth(health, mock);
|
2017-07-09 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
TEST_F(HealthTest, resurrectPermanent)
|
2017-07-09 21:31:00 +02:00
|
|
|
{
|
2017-07-18 21:04:57 +02:00
|
|
|
setDefaultExpectations();
|
|
|
|
health.init();
|
|
|
|
|
2017-07-09 21:31:00 +02:00
|
|
|
checkNormalDamage(health, UNIT_HEALTH);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
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);
|
2017-07-18 21:04:57 +02:00
|
|
|
checkFullHealth(health, mock);
|
2017-07-09 21:31:00 +02:00
|
|
|
|
|
|
|
health.takeResurrected();
|
2017-07-18 21:04:57 +02:00
|
|
|
checkFullHealth(health, mock);
|
2017-07-09 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:04:57 +02:00
|
|
|
TEST_F(HealthTest, singleUnitStack)
|
2017-07-17 01:50:38 +02:00
|
|
|
{
|
|
|
|
//related to issue 2612
|
|
|
|
|
|
|
|
//one Titan
|
2017-07-18 21:04:57 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
EXPECT_CALL(mock, getAllBonuses(_, _, _, _)).WillRepeatedly(Invoke(&bonusMock, &BonusBearerMock::getAllBonuses));
|
|
|
|
EXPECT_CALL(mock, getTreeVersion()).WillRepeatedly(Return(1));
|
|
|
|
|
|
|
|
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::STACK_HEALTH, Bonus::CREATURE_ABILITY, 300, 0));
|
|
|
|
|
2017-07-18 21:18:37 +02:00
|
|
|
EXPECT_CALL(mock, unitBaseAmount()).WillRepeatedly(Return(1));
|
2017-07-17 01:50:38 +02:00
|
|
|
|
|
|
|
health.init();
|
|
|
|
|
|
|
|
checkDamage(health, 1000, 300);
|
2017-07-18 21:04:57 +02:00
|
|
|
checkEmptyHealth(health, mock);
|
2017-07-17 01:50:38 +02:00
|
|
|
|
|
|
|
checkHeal(health, EHealLevel::RESURRECT, EHealPower::PERMANENT, 300, 300);
|
2017-07-18 21:04:57 +02:00
|
|
|
checkFullHealth(health, mock);
|
2017-07-17 01:50:38 +02:00
|
|
|
}
|
|
|
|
|