1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/test/battle/CUnitStateMagicTest.cpp

221 lines
4.9 KiB
C++
Raw Normal View History

Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
/*
* CUnitStateMagicTest.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_BonusBearer.h"
#include "mock/mock_spells_Spell.h"
#include "mock/mock_UnitInfo.h"
#include "mock/mock_UnitEnvironment.h"
#include "../../lib/battle/CUnitState.h"
#include "../../lib/CCreatureHandler.h"
class UnitStateMagicTest : public ::testing::Test
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
public:
UnitInfoMock infoMock;
UnitEnvironmentMock envMock;
BonusBearerMock bonusMock;
::testing::StrictMock<spells::SpellMock> spellMock;
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
battle::CUnitStateDetached subject;
const int32_t DEFAULT_AMOUNT = 100;
const int32_t DEFAULT_SPELL_INDEX = 42000000; //could be anything but should be far out of spellhandler bounds
const int32_t DEFAULT_SCHOOL_LEVEL = 2;
UnitStateMagicTest()
:infoMock(),
envMock(),
bonusMock(),
spellMock(),
subject(&infoMock, &bonusMock)
{
}
void setDefaultExpectations()
{
using namespace testing;
EXPECT_CALL(infoMock, unitBaseAmount()).WillRepeatedly(Return(DEFAULT_AMOUNT));
EXPECT_CALL(spellMock, getIndex()).WillRepeatedly(Return(DEFAULT_SPELL_INDEX));
}
void initUnit()
{
subject.localInit(&envMock);
}
void makeNormalCaster()
{
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::SPELLCASTER, Bonus::CREATURE_ABILITY, DEFAULT_SCHOOL_LEVEL, 0, DEFAULT_SPELL_INDEX));
}
};
TEST_F(UnitStateMagicTest, initialNormal)
{
setDefaultExpectations();
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::CASTS, Bonus::CREATURE_ABILITY, 567, 0));
initUnit();
EXPECT_TRUE(subject.canCast());
EXPECT_TRUE(subject.isCaster());
EXPECT_EQ(subject.casts.total(), 567);
EXPECT_EQ(subject.casts.available(), 567);
}
TEST_F(UnitStateMagicTest, schoolLevelByDefault)
{
setDefaultExpectations();
initUnit();
EXPECT_EQ(subject.getSpellSchoolLevel(&spellMock, nullptr), 0);
}
TEST_F(UnitStateMagicTest, schoolLevelForNormalCaster)
{
setDefaultExpectations();
initUnit();
makeNormalCaster();
EXPECT_EQ(subject.getSpellSchoolLevel(&spellMock, nullptr), DEFAULT_SCHOOL_LEVEL);
}
TEST_F(UnitStateMagicTest, effectLevelForNormalCaster)
{
setDefaultExpectations();
initUnit();
makeNormalCaster();
EXPECT_EQ(subject.getEffectLevel(&spellMock), DEFAULT_SCHOOL_LEVEL);
}
TEST_F(UnitStateMagicTest, spellBonus)
{
setDefaultExpectations();
initUnit();
makeNormalCaster();
EXPECT_EQ(subject.getSpellBonus(&spellMock, 12345, &subject), 12345);
}
TEST_F(UnitStateMagicTest, specificSpellBonus)
{
setDefaultExpectations();
initUnit();
makeNormalCaster();
EXPECT_EQ(subject.getSpecificSpellBonus(&spellMock, 12345), 12345);
}
TEST_F(UnitStateMagicTest, effectPower)
{
setDefaultExpectations();
initUnit();
const int32_t EFFECT_POWER = 12 * 100;
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::CREATURE_SPELL_POWER, Bonus::CREATURE_ABILITY, EFFECT_POWER, 0));
makeNormalCaster();
EXPECT_EQ(subject.getEffectPower(&spellMock), 12 * DEFAULT_AMOUNT);
}
TEST_F(UnitStateMagicTest, enchantPowerByDefault)
{
setDefaultExpectations();
initUnit();
makeNormalCaster();
EXPECT_EQ(subject.getEnchantPower(&spellMock), 3);
}
TEST_F(UnitStateMagicTest, enchantPower)
{
setDefaultExpectations();
initUnit();
const int32_t ENCHANT_POWER = 42;
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::CREATURE_ENCHANT_POWER, Bonus::CREATURE_ABILITY, ENCHANT_POWER, 0));
makeNormalCaster();
EXPECT_EQ(subject.getEnchantPower(&spellMock), ENCHANT_POWER);
}
TEST_F(UnitStateMagicTest, effectValueByDefault)
{
setDefaultExpectations();
initUnit();
makeNormalCaster();
EXPECT_EQ(subject.getEffectValue(&spellMock), 0);
}
TEST_F(UnitStateMagicTest, effectValue)
{
setDefaultExpectations();
initUnit();
const int32_t EFFECT_VALUE = 456;
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::SPECIFIC_SPELL_POWER, Bonus::CREATURE_ABILITY, EFFECT_VALUE, 0, DEFAULT_SPELL_INDEX));
makeNormalCaster();
EXPECT_EQ(subject.getEffectValue(&spellMock), EFFECT_VALUE * DEFAULT_AMOUNT);
}
TEST_F(UnitStateMagicTest, getOwner)
{
using namespace testing;
setDefaultExpectations();
initUnit();
PlayerColor player(123);
PlayerColor otherPlayer(124);
EXPECT_CALL(infoMock, unitOwner()).WillRepeatedly(Return(player));
ON_CALL(envMock, unitEffectiveOwner(Eq(&subject))).WillByDefault(Return(otherPlayer));
EXPECT_CALL(envMock, unitEffectiveOwner(_));
EXPECT_EQ(subject.getCasterOwner(), otherPlayer);
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
}
TEST_F(UnitStateMagicTest, spendMana)
{
setDefaultExpectations();
bonusMock.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::CASTS, Bonus::CREATURE_ABILITY, 1, 0));
initUnit();
EXPECT_TRUE(subject.canCast());
subject.spendMana(nullptr, 1);
EXPECT_FALSE(subject.canCast());
}
//TODO:getCasterName
//TODO:getCastDescription
//TODO:spendMana