1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/test/spells/effects/DispelTest.cpp
AlexVinS 0b70baa95e 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
2018-02-08 11:37:21 +03:00

214 lines
6.3 KiB
C++

/*
* DispelTest.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 "EffectFixture.h"
bool operator==(const Bonus & b1, const Bonus & b2)
{
return b1.duration == b2.duration
&& b1.type == b2.type
&& b1.subtype == b2.subtype
&& b1.source == b2.source
&& b1.val == b2.val
&& b1.sid == b2.sid
&& b1.valType == b2.valType
&& b1.additionalInfo == b2.additionalInfo
&& b1.effectRange == b2.effectRange
&& b1.description == b2.description;
}
namespace test
{
using namespace ::spells;
using namespace ::spells::effects;
using namespace ::testing;
class DispelTest : public Test, public EffectFixture
{
public:
DispelTest()
: EffectFixture("core:dispel")
{
}
protected:
void SetUp() override
{
EffectFixture::setUp();
}
};
TEST_F(DispelTest, ApplicableToAliveUnitWithTimedEffect)
{
{
JsonNode config(JsonNode::JsonType::DATA_STRUCT);
config["dispelNegative"].Bool() = true;
EffectFixture::setupEffect(config);
}
auto & unit = unitsFake.add(BattleSide::ATTACKER);
unit.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::SPELL_EFFECT, 1, SpellID::CURSE));
EXPECT_CALL(unit, isValidTarget(Eq(false))).WillOnce(Return(true));
EXPECT_CALL(mechanicsMock, isSmart()).WillOnce(Return(false));
EXPECT_CALL(mechanicsMock, ownerMatches(Eq(&unit))).WillOnce(Return(true));
EXPECT_CALL(mechanicsMock, getSpellIndex()).Times(AtLeast(1)).WillRepeatedly(Return(424242));
unitsFake.setDefaultBonusExpectations();
EffectTarget target;
target.emplace_back(&unit, BattleHex());
EXPECT_TRUE(subject->applicable(problemMock, &mechanicsMock, target));
}
TEST_F(DispelTest, IgnoresOwnEffects)
{
{
JsonNode config(JsonNode::JsonType::DATA_STRUCT);
config["dispelPositive"].Bool() = true;
config["dispelNegative"].Bool() = true;
config["dispelNeutral"].Bool() = true;
EffectFixture::setupEffect(config);
}
auto & unit = unitsFake.add(BattleSide::ATTACKER);
unit.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::SPELL_EFFECT, 1, SpellID::ANTI_MAGIC));
EXPECT_CALL(unit, isValidTarget(Eq(false))).Times(AtMost(1)).WillRepeatedly(Return(true));
EXPECT_CALL(mechanicsMock, isSmart()).Times(AtMost(1)).WillRepeatedly(Return(false));
EXPECT_CALL(mechanicsMock, ownerMatches(Eq(&unit))).Times(AtMost(1)).WillRepeatedly(Return(true));
EXPECT_CALL(mechanicsMock, getSpellIndex()).Times(AtLeast(1)).WillRepeatedly(Return(SpellID::ANTI_MAGIC));
unitsFake.setDefaultBonusExpectations();
EffectTarget target;
target.emplace_back(&unit, BattleHex());
EXPECT_FALSE(subject->applicable(problemMock, &mechanicsMock, target));
}
TEST_F(DispelTest, NotApplicableToUnitWithNoTimedEffect)
{
auto & unit = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit, isValidTarget(Eq(false))).Times(AtMost(1)).WillRepeatedly(Return(true));
EXPECT_CALL(mechanicsMock, isSmart()).Times(AtMost(1)).WillRepeatedly(Return(false));
EXPECT_CALL(mechanicsMock, ownerMatches(Eq(&unit))).Times(AtMost(1)).WillRepeatedly(Return(true));
unitsFake.setDefaultBonusExpectations();
EffectTarget target;
target.emplace_back(&unit, BattleHex());
EXPECT_FALSE(subject->applicable(problemMock, &mechanicsMock, target));
}
TEST_F(DispelTest, NotApplicableToDeadUnit)
{
auto & unit = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit, isValidTarget(Eq(false))).Times(AtMost(1)).WillRepeatedly(Return(false));
EXPECT_CALL(mechanicsMock, isSmart()).Times(AtMost(1)).WillRepeatedly(Return(false));
EXPECT_CALL(mechanicsMock, ownerMatches(Eq(&unit))).Times(AtMost(1)).WillRepeatedly(Return(true));
unitsFake.setDefaultBonusExpectations();
EffectTarget target;
target.emplace_back(&unit, BattleHex());
EXPECT_FALSE(subject->applicable(problemMock, &mechanicsMock, target));
}
class DispelApplyTest : public Test, public EffectFixture
{
public:
DispelApplyTest()
: EffectFixture("core:dispel")
{
}
std::array<std::vector<Bonus>, 2> expectedBonus;
std::array<std::vector<Bonus>, 2> actualBonus;
protected:
void SetUp() override
{
EffectFixture::setUp();
}
};
TEST_F(DispelApplyTest, RemovesEffects)
{
{
JsonNode config(JsonNode::JsonType::DATA_STRUCT);
config["dispelPositive"].Bool() = true;
config["dispelNegative"].Bool() = true;
config["dispelNeutral"].Bool() = true;
EffectFixture::setupEffect(config);
}
const std::array<uint32_t, 2> unitIds = {567, 765};
auto & unit0 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit0, unitId()).Times(AtLeast(1)).WillRepeatedly(Return(unitIds[0]));
auto & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).Times(AtLeast(1)).WillRepeatedly(Return(unitIds[1]));
{
auto bonus = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::SPELL_EFFECT, 1, SpellID::CURSE);
expectedBonus[0].emplace_back(*bonus);
unit0.addNewBonus(bonus);
bonus = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::SPELL_EFFECT, 1, SpellID::CURSE);
expectedBonus[0].emplace_back(*bonus);
unit0.addNewBonus(bonus);
bonus = std::make_shared<Bonus>(Bonus::N_TURNS, Bonus::GENERAL_ATTACK_REDUCTION, Bonus::SPELL_EFFECT, 3, SpellID::CURSE);
expectedBonus[0].emplace_back(*bonus);
unit0.addNewBonus(bonus);
bonus = std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::SPELL_EFFECT, 5, SpellID::ANTI_MAGIC);
expectedBonus[1].emplace_back(*bonus);
unit1.addNewBonus(bonus);
bonus = std::make_shared<Bonus>(Bonus::N_TURNS, Bonus::GENERAL_ATTACK_REDUCTION, Bonus::SPELL_EFFECT, 3, SpellID::ANTI_MAGIC);
expectedBonus[1].emplace_back(*bonus);
unit1.addNewBonus(bonus);
}
EXPECT_CALL(*battleFake, removeUnitBonus(Eq(unitIds[0]),_)).WillOnce(SaveArg<1>(&actualBonus[0]));
EXPECT_CALL(*battleFake, removeUnitBonus(Eq(unitIds[1]),_)).WillOnce(SaveArg<1>(&actualBonus[1]));
EXPECT_CALL(mechanicsMock, getSpellIndex()).Times(AtLeast(1)).WillRepeatedly(Return(424242));
unitsFake.setDefaultBonusExpectations();
EffectTarget target;
target.emplace_back(&unit0, BattleHex());
target.emplace_back(&unit1, BattleHex());
subject->apply(battleProxy.get(), rngMock, &mechanicsMock, target);
EXPECT_THAT(actualBonus[0], UnorderedElementsAreArray(expectedBonus[0]));
EXPECT_THAT(actualBonus[1], UnorderedElementsAreArray(expectedBonus[1]));
}
}