/* * 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" #include "../../lib/CStack.h" using namespace testing; static const int32_t UNIT_HEALTH = 123; static const int32_t UNIT_AMOUNT = 300; class HealthTest : public ::testing::Test { public: UnitHealthInfoMock mock; HealthTest() : health(&mock){ } void setDefaultExpectations() { EXPECT_CALL(mock, unitMaxHealth()).WillRepeatedly(Return(UNIT_HEALTH)); EXPECT_CALL(mock, unitBaseAmount()).WillRepeatedly(Return(UNIT_AMOUNT)); } CHealth health; }; static void checkTotal(const CHealth & health, const UnitHealthInfoMock & mock) { EXPECT_EQ(health.total(), mock.unitMaxHealth() * mock.unitBaseAmount()); } static void checkEmptyHealth(const CHealth & health, const UnitHealthInfoMock & mock) { checkTotal(health, mock); EXPECT_EQ(health.getCount(), 0); EXPECT_EQ(health.getFirstHPleft(), 0); EXPECT_EQ(health.getResurrected(), 0); EXPECT_EQ(health.available(), 0); } static void checkFullHealth(const CHealth & health, const UnitHealthInfoMock & mock) { 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()); } static void checkDamage(CHealth & health, const int32_t initialDamage, const int32_t expectedDamage) { int32_t damage = initialDamage; health.damage(damage); EXPECT_EQ(damage, expectedDamage); } 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); } TEST_F(HealthTest, empty) { setDefaultExpectations(); checkEmptyHealth(health, mock); health.init(); checkFullHealth(health, mock); health.reset(); checkEmptyHealth(health, mock); } TEST_F(HealthTest, damage) { setDefaultExpectations(); health.init(); checkNormalDamage(health, 0); checkFullHealth(health, mock); checkNormalDamage(health, mock.unitMaxHealth() - 1); EXPECT_EQ(health.getCount(), UNIT_AMOUNT); EXPECT_EQ(health.getFirstHPleft(), 1); EXPECT_EQ(health.getResurrected(), 0); checkNormalDamage(health, 1); EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH); EXPECT_EQ(health.getResurrected(), 0); checkNormalDamage(health, UNIT_HEALTH * (UNIT_AMOUNT - 1)); checkEmptyHealth(health, mock); checkNoDamage(health, 1337); checkEmptyHealth(health, mock); } TEST_F(HealthTest, heal) { setDefaultExpectations(); health.init(); checkNormalDamage(health, 99); EXPECT_EQ(health.getCount(), UNIT_AMOUNT); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH-99); EXPECT_EQ(health.getResurrected(), 0); 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); 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); checkHeal(health, EHealLevel::OVERHEAL, EHealPower::PERMANENT, 50, 50); checkFullHealth(health, mock); } TEST_F(HealthTest, resurrectOneBattle) { setDefaultExpectations(); health.init(); checkNormalDamage(health, UNIT_HEALTH); EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH); EXPECT_EQ(health.getResurrected(), 0); 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); checkNormalDamage(health, UNIT_HEALTH); EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH); EXPECT_EQ(health.getResurrected(), 0); health.init(); checkNormalDamage(health, UNIT_HEALTH); EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH); EXPECT_EQ(health.getResurrected(), 0); health.takeResurrected(); EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH); EXPECT_EQ(health.getResurrected(), 0); health.init(); checkNormalDamage(health, UNIT_HEALTH * UNIT_AMOUNT); checkEmptyHealth(health, mock); 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); health.takeResurrected(); checkEmptyHealth(health, mock); } TEST_F(HealthTest, resurrectPermanent) { setDefaultExpectations(); health.init(); checkNormalDamage(health, UNIT_HEALTH); EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH); EXPECT_EQ(health.getResurrected(), 0); 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); checkNormalDamage(health, UNIT_HEALTH); EXPECT_EQ(health.getCount(), UNIT_AMOUNT - 1); EXPECT_EQ(health.getFirstHPleft(), UNIT_HEALTH); EXPECT_EQ(health.getResurrected(), 0); health.init(); checkNormalDamage(health, UNIT_HEALTH * UNIT_AMOUNT); checkEmptyHealth(health, mock); checkHeal(health, EHealLevel::RESURRECT, EHealPower::PERMANENT, UNIT_HEALTH * UNIT_AMOUNT, UNIT_HEALTH * UNIT_AMOUNT); checkFullHealth(health, mock); health.takeResurrected(); checkFullHealth(health, mock); } TEST_F(HealthTest, singleUnitStack) { //related to issue 2612 //one Titan EXPECT_CALL(mock, unitMaxHealth()).WillRepeatedly(Return(1)); EXPECT_CALL(mock, unitBaseAmount()).WillRepeatedly(Return(300)); health.init(); checkDamage(health, 1000, 300); checkEmptyHealth(health, mock); checkHeal(health, EHealLevel::RESURRECT, EHealPower::PERMANENT, 300, 300); checkFullHealth(health, mock); }