1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/test/spells/effects/CloneTest.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

236 lines
6.4 KiB
C++

/*
* CloneTest.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"
#include <vstd/RNG.h>
#include "../../../lib/battle/CUnitState.h"
namespace test
{
using namespace ::spells;
using namespace ::spells::effects;
using namespace ::testing;
class CloneTest : public Test, public EffectFixture
{
public:
CloneTest()
: EffectFixture("core:clone")
{
}
protected:
void SetUp() override
{
EffectFixture::setUp();
}
};
TEST_F(CloneTest, ApplicableToValidTarget)
{
{
JsonNode config(JsonNode::JsonType::DATA_STRUCT);
config["maxTier"].Integer() = 7;
EffectFixture::setupEffect(config);
}
auto & unit = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit, isValidTarget(Eq(false))).Times(AtLeast(1)).WillRepeatedly(Return(true));
EXPECT_CALL(unit, hasClone()).WillRepeatedly(Return(false));
EXPECT_CALL(unit, isClone()).Times(AtLeast(1)).WillRepeatedly(Return(false));
EXPECT_CALL(mechanicsMock, ownerMatches(Eq(&unit))).Times(AtLeast(1)).WillRepeatedly(Return(true));
EXPECT_CALL(mechanicsMock, isSmart()).Times(AtLeast(1)).WillRepeatedly(Return(true));
EXPECT_CALL(unit, getPosition()).WillOnce(Return(BattleHex(5, 5)));
EffectTarget target;
target.emplace_back(&unit);
EXPECT_TRUE(subject->applicable(problemMock, &mechanicsMock, target));
}
TEST_F(CloneTest, CloneIsNotClonable)
{
auto & unit = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit, hasClone()).WillRepeatedly(Return(false));
EXPECT_CALL(unit, isClone()).Times(AtLeast(1)).WillRepeatedly(Return(true));
EXPECT_CALL(unit, getPosition()).WillOnce(Return(BattleHex(5, 5)));
EffectTarget target;
target.emplace_back(&unit);
EXPECT_FALSE(subject->applicable(problemMock, &mechanicsMock, target));
}
TEST_F(CloneTest, SecondCloneRejected)
{
auto & unit = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(unit, hasClone()).Times(AtLeast(1)).WillRepeatedly(Return(true));
EXPECT_CALL(unit, isClone()).WillRepeatedly(Return(false));
EXPECT_CALL(unit, getPosition()).WillOnce(Return(BattleHex(5, 5)));
EffectTarget target;
target.emplace_back(&unit);
EXPECT_FALSE(subject->applicable(problemMock, &mechanicsMock, target));
}
class CloneApplyTest : public Test, public EffectFixture
{
public:
std::shared_ptr<::battle::UnitInfo> cloneAddInfo;
std::shared_ptr<::battle::CUnitState> cloneState;
std::shared_ptr<::battle::CUnitState> originalState;
const uint32_t originalId = 40;
const uint32_t cloneId = 42;
const int32_t expectedAmount = 10;
const int32_t effectDuration = 6;
const BattleHex originalPosition = BattleHex(5,5);
EffectTarget target;
CloneApplyTest()
: EffectFixture("core:clone")
{
}
void onUnitAdded(uint32_t id, const JsonNode & data)
{
using namespace ::battle;
UnitFake & clone = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(clone, unitId()).WillRepeatedly(Return(id));
cloneState = std::make_shared<CUnitStateDetached>(&clone, &clone);
EXPECT_CALL(clone, acquireState()).WillOnce(Return(cloneState));
cloneAddInfo->load(id, data);
}
void checkCloneLifetimeMarker(uint32_t id, const std::vector<Bonus> & bonus)
{
EXPECT_EQ(id, cloneId);
GTEST_ASSERT_EQ(bonus.size(), 1);
const Bonus & marker = bonus.front();
EXPECT_EQ(marker.type, Bonus::NONE);
EXPECT_EQ(marker.duration, Bonus::N_TURNS);
EXPECT_EQ(marker.turnsRemain, effectDuration);
EXPECT_EQ(marker.source, Bonus::SPELL_EFFECT);
EXPECT_EQ(marker.sid, SpellID::CLONE);
}
void setDefaultExpectations()
{
using namespace ::battle;
EXPECT_CALL(mechanicsMock, getEffectDuration()).WillOnce(Return(effectDuration));
EXPECT_CALL(*battleFake, getUnitsIf(_)).Times(AtLeast(1));
EXPECT_CALL(*battleFake, nextUnitId()).WillOnce(Return(cloneId));
EXPECT_CALL(*battleFake, addUnit(_, _)).WillOnce(Invoke(this, &CloneApplyTest::onUnitAdded));
EXPECT_CALL(*battleFake, setUnitState(Eq(originalId), _, Eq(0))).Times(AtLeast(1));
EXPECT_CALL(*battleFake, setUnitState(Eq(cloneId), _, Eq(0))).Times(AtLeast(1));
auto & original = unitsFake.add(BattleSide::ATTACKER);
EXPECT_CALL(original, isValidTarget(Eq(false))).Times(AtLeast(1)).WillRepeatedly(Return(true));
EXPECT_CALL(original, getCount()).WillRepeatedly(Return(expectedAmount));
EXPECT_CALL(original, unitId()).WillRepeatedly(Return(originalId));
EXPECT_CALL(original, creatureId()).WillRepeatedly(Return(CreatureID(0)));
EXPECT_CALL(original, creatureIndex()).WillRepeatedly(Return(0));
EXPECT_CALL(original, doubleWide()).WillRepeatedly(Return(false));
EXPECT_CALL(original, getPosition()).WillRepeatedly(Return(originalPosition));
EXPECT_CALL(original, unitSide()).Times(AnyNumber());
originalState = std::make_shared<CUnitStateDetached>(&original, &original);
EXPECT_CALL(original, acquireState()).WillOnce(Return(originalState));
target.emplace_back(&original);
}
protected:
void SetUp() override
{
EffectFixture::setUp();
cloneAddInfo = std::make_shared<::battle::UnitInfo>();
}
};
TEST_F(CloneApplyTest, AddsNewUnit)
{
setupEmptyBattlefield();
setDefaultExpectations();
EXPECT_CALL(*battleFake, addUnitBonus(_,_)).Times(AtLeast(1));
subject->apply(battleProxy.get(), rngMock, &mechanicsMock, target);
EXPECT_EQ(cloneAddInfo->id, cloneId);
EXPECT_EQ(cloneAddInfo->count, expectedAmount);
EXPECT_EQ(cloneAddInfo->type, CreatureID(0));
EXPECT_EQ(cloneAddInfo->side, BattleSide::ATTACKER);
EXPECT_TRUE(cloneAddInfo->position.isValid());
EXPECT_NE(cloneAddInfo->position, originalPosition);
EXPECT_TRUE(cloneAddInfo->summoned);
}
TEST_F(CloneApplyTest, SetsClonedFlag)
{
setupEmptyBattlefield();
setDefaultExpectations();
EXPECT_CALL(*battleFake, addUnitBonus(_,_)).Times(AtLeast(1));
subject->apply(battleProxy.get(), rngMock, &mechanicsMock, target);
EXPECT_TRUE(cloneState->cloned);
}
TEST_F(CloneApplyTest, SetsCloneLink)
{
setupEmptyBattlefield();
setDefaultExpectations();
EXPECT_CALL(*battleFake, addUnitBonus(_,_)).Times(AtLeast(1));
subject->apply(battleProxy.get(), rngMock, &mechanicsMock, target);
EXPECT_EQ(originalState->cloneID, cloneId);
}
TEST_F(CloneApplyTest, SetsLifetimeMarker)
{
setupEmptyBattlefield();
setDefaultExpectations();
EXPECT_CALL(*battleFake, addUnitBonus(_,_)).WillOnce(Invoke(this, &CloneApplyTest::checkCloneLifetimeMarker));
subject->apply(battleProxy.get(), rngMock, &mechanicsMock, target);
}
}