1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00
vcmi/test/battle/CBattleInfoCallbackTest.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

420 lines
11 KiB
C++

/*
* CBattleInfoCallbackTest.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 "../../lib/battle/CBattleInfoCallback.h"
#include <vstd/RNG.h>
#include "../../lib/NetPacksBase.h"
#include "mock/mock_BonusBearer.h"
#include "mock/mock_battle_IBattleState.h"
#include "mock/mock_battle_Unit.h"
using namespace battle;
using namespace testing;
class UnitFake : public UnitMock
{
public:
void addNewBonus(const std::shared_ptr<Bonus> & b)
{
bonusFake.addNewBonus(b);
}
void makeAlive()
{
EXPECT_CALL(*this, alive()).WillRepeatedly(Return(true));
}
void makeWarMachine()
{
addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::SIEGE_WEAPON, Bonus::CREATURE_ABILITY, 1, 0));
}
void redirectBonusesToFake()
{
ON_CALL(*this, getAllBonuses(_, _, _, _)).WillByDefault(Invoke(&bonusFake, &BonusBearerMock::getAllBonuses));
ON_CALL(*this, getTreeVersion()).WillByDefault(Invoke(&bonusFake, &BonusBearerMock::getTreeVersion));
}
void expectAnyBonusSystemCall()
{
EXPECT_CALL(*this, getAllBonuses(_, _, _, _)).Times(AtLeast(0));
EXPECT_CALL(*this, getTreeVersion()).Times(AtLeast(0));
}
void setDefaultExpectations()
{
EXPECT_CALL(*this, unitSlot()).WillRepeatedly(Return(SlotID(0)));
EXPECT_CALL(*this, creatureIndex()).WillRepeatedly(Return(0));
}
private:
BonusBearerMock bonusFake;
};
class UnitsFake
{
public:
std::vector<std::shared_ptr<UnitFake>> allUnits;
UnitFake & add(ui8 side)
{
UnitFake * unit = new UnitFake();
EXPECT_CALL(*unit, unitSide()).WillRepeatedly(Return(side));
unit->setDefaultExpectations();
allUnits.emplace_back(unit);
return *allUnits.back().get();
}
Units getUnitsIf(UnitFilter predicate) const
{
Units ret;
for(auto & unit : allUnits)
{
if(predicate(unit.get()))
ret.push_back(unit.get());
}
return ret;
}
void setDefaultBonusExpectations()
{
for(auto & unit : allUnits)
{
unit->redirectBonusesToFake();
unit->expectAnyBonusSystemCall();
}
}
};
class CBattleInfoCallbackTest : public Test
{
public:
class TestSubject : public CBattleInfoCallback
{
public:
TestSubject()
: CBattleInfoCallback()
{
}
void setBattle(const IBattleInfo * battleInfo)
{
CBattleInfoCallback::setBattle(battleInfo);
}
};
TestSubject subject;
BattleStateMock battleMock;
UnitsFake unitsFake;
void startBattle()
{
subject.setBattle(&battleMock);
}
void redirectUnitsToFake()
{
ON_CALL(battleMock, getUnitsIf(_)).WillByDefault(Invoke(&unitsFake, &UnitsFake::getUnitsIf));
}
};
class BattleFinishedTest : public CBattleInfoCallbackTest
{
public:
void expectBattleNotFinished()
{
auto ret = subject.battleIsFinished();
EXPECT_FALSE(ret);
}
void expectBattleDraw()
{
auto ret = subject.battleIsFinished();
EXPECT_TRUE(ret);
EXPECT_EQ(*ret, 2);
}
void expectBattleWinner(ui8 side)
{
auto ret = subject.battleIsFinished();
EXPECT_TRUE(ret);
EXPECT_EQ(*ret, side);
}
void expectBattleLooser(ui8 side)
{
auto ret = subject.battleIsFinished();
EXPECT_TRUE(ret);
EXPECT_NE(*ret, (int)side);
}
void setDefaultExpectations()
{
redirectUnitsToFake();
unitsFake.setDefaultBonusExpectations();
EXPECT_CALL(battleMock, getUnitsIf(_)).Times(1);
}
};
TEST_F(BattleFinishedTest, NoBattleIsDraw)
{
expectBattleDraw();
}
TEST_F(BattleFinishedTest, EmptyBattleIsDraw)
{
setDefaultExpectations();
startBattle();
expectBattleDraw();
}
TEST_F(BattleFinishedTest, LastAliveUnitWins)
{
UnitFake & unit = unitsFake.add(1);
unit.makeAlive();
setDefaultExpectations();
startBattle();
expectBattleWinner(1);
}
TEST_F(BattleFinishedTest, TwoUnitsContinueFight)
{
UnitFake & unit1 = unitsFake.add(0);
unit1.makeAlive();
UnitFake & unit2 = unitsFake.add(1);
unit2.makeAlive();
setDefaultExpectations();
startBattle();
expectBattleNotFinished();
}
TEST_F(BattleFinishedTest, LastWarMachineNotWins)
{
UnitFake & unit = unitsFake.add(0);
unit.makeAlive();
unit.makeWarMachine();
setDefaultExpectations();
startBattle();
expectBattleLooser(0);
}
TEST_F(BattleFinishedTest, LastWarMachineLoose)
{
UnitFake & unit1 = unitsFake.add(0);
unit1.makeAlive();
UnitFake & unit2 = unitsFake.add(1);
unit2.makeAlive();
unit2.makeWarMachine();
setDefaultExpectations();
startBattle();
expectBattleWinner(0);
}
class BattleMatchOwnerTest : public CBattleInfoCallbackTest
{
public:
void setDefaultExpectations()
{
redirectUnitsToFake();
unitsFake.setDefaultBonusExpectations();
EXPECT_CALL(battleMock, getSidePlayer(Eq(BattleSide::ATTACKER))).WillRepeatedly(Return(PlayerColor(0)));
EXPECT_CALL(battleMock, getSidePlayer(Eq(BattleSide::DEFENDER))).WillRepeatedly(Return(PlayerColor(1)));
}
};
TEST_F(BattleMatchOwnerTest, normalToSelf)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
setDefaultExpectations();
startBattle();
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit1, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit1, boost::logic::indeterminate));
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit1, false));
}
TEST_F(BattleMatchOwnerTest, hypnotizedToSelf)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
unit1.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
setDefaultExpectations();
startBattle();
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit1, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit1, boost::logic::indeterminate));
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit1, false));
}
TEST_F(BattleMatchOwnerTest, normalToNormalAlly)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
UnitFake & unit2 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
setDefaultExpectations();
startBattle();
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, false));
}
TEST_F(BattleMatchOwnerTest, hypnotizedToNormalAlly)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
unit1.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
UnitFake & unit2 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
setDefaultExpectations();
startBattle();
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, false));
}
TEST_F(BattleMatchOwnerTest, normalToHypnotizedAlly)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
UnitFake & unit2 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
unit2.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
setDefaultExpectations();
startBattle();
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, false));
}
TEST_F(BattleMatchOwnerTest, hypnotizedToHypnotizedAlly)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
unit1.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
UnitFake & unit2 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
unit2.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
setDefaultExpectations();
startBattle();
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, false));
}
TEST_F(BattleMatchOwnerTest, normalToNormalEnemy)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
UnitFake & unit2 = unitsFake.add(BattleSide::DEFENDER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
setDefaultExpectations();
startBattle();
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, false));
}
TEST_F(BattleMatchOwnerTest, hypnotizedToNormalEnemy)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
unit1.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
UnitFake & unit2 = unitsFake.add(BattleSide::DEFENDER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
setDefaultExpectations();
startBattle();
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, false));
}
TEST_F(BattleMatchOwnerTest, normalToHypnotizedEnemy)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
UnitFake & unit2 = unitsFake.add(BattleSide::DEFENDER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
unit2.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
setDefaultExpectations();
startBattle();
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, false));
}
TEST_F(BattleMatchOwnerTest, hypnotizedToHypnotizedEnemy)
{
UnitFake & unit1 = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit1, unitId()).WillRepeatedly(Return(42));
unit1.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
UnitFake & unit2 = unitsFake.add(BattleSide::DEFENDER);
EXPECT_CALL(unit2, unitId()).WillRepeatedly(Return(4242));
unit2.addNewBonus(std::make_shared<Bonus>(Bonus::PERMANENT, Bonus::HYPNOTIZED, Bonus::CREATURE_ABILITY, 0, 0));
setDefaultExpectations();
startBattle();
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, true));
EXPECT_TRUE(subject.battleMatchOwner(&unit1, &unit2, boost::logic::indeterminate));
EXPECT_FALSE(subject.battleMatchOwner(&unit1, &unit2, false));
}