BattleFake.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * BattleFake.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "BattleFake.h"
  12. namespace test
  13. {
  14. namespace battle
  15. {
  16. void UnitFake::addNewBonus(const std::shared_ptr<Bonus> & b)
  17. {
  18. bonusFake.addNewBonus(b);
  19. }
  20. void UnitFake::makeAlive()
  21. {
  22. EXPECT_CALL(*this, alive()).Times(AtLeast(1)).WillRepeatedly(Return(true));
  23. }
  24. void UnitFake::makeDead()
  25. {
  26. EXPECT_CALL(*this, alive()).Times(AtLeast(1)).WillRepeatedly(Return(false));
  27. EXPECT_CALL(*this, isGhost()).Times(AtLeast(1)).WillRepeatedly(Return(false));
  28. }
  29. void UnitFake::redirectBonusesToFake()
  30. {
  31. ON_CALL(*this, getAllBonuses(_, _)).WillByDefault(Invoke(&bonusFake, &BonusBearerMock::getAllBonuses));
  32. ON_CALL(*this, getTreeVersion()).WillByDefault(Invoke(&bonusFake, &BonusBearerMock::getTreeVersion));
  33. }
  34. void UnitFake::expectAnyBonusSystemCall()
  35. {
  36. EXPECT_CALL(*this, getAllBonuses(_, _)).Times(AtLeast(0));
  37. EXPECT_CALL(*this, getTreeVersion()).Times(AtLeast(0));
  38. }
  39. UnitFake & UnitsFake::add(BattleSide side)
  40. {
  41. auto * unit = new UnitFake();
  42. ON_CALL(*unit, unitSide()).WillByDefault(Return(side));
  43. unit->redirectBonusesToFake();
  44. allUnits.emplace_back(unit);
  45. return *allUnits.back().get();
  46. }
  47. Units UnitsFake::getUnitsIf(UnitFilter predicate) const
  48. {
  49. Units ret;
  50. for(auto & unit : allUnits)
  51. {
  52. if(predicate(unit.get()))
  53. ret.push_back(unit.get());
  54. }
  55. return ret;
  56. }
  57. void UnitsFake::setDefaultBonusExpectations()
  58. {
  59. for(auto & unit : allUnits)
  60. {
  61. unit->expectAnyBonusSystemCall();
  62. }
  63. }
  64. BattleFake::~BattleFake() = default;
  65. #if SCRIPTING_ENABLED
  66. BattleFake::BattleFake(std::shared_ptr<scripting::PoolMock> pool_):
  67. pool(pool_)
  68. {
  69. }
  70. #else
  71. BattleFake::BattleFake() = default;
  72. #endif
  73. void BattleFake::setUp()
  74. {
  75. }
  76. void BattleFake::setupEmptyBattlefield()
  77. {
  78. EXPECT_CALL(*this, getDefendedTown()).WillRepeatedly(Return(nullptr));
  79. EXPECT_CALL(*this, getAllObstacles()).WillRepeatedly(Return(IBattleInfo::ObstacleCList()));
  80. EXPECT_CALL(*this, getBattlefieldType()).WillRepeatedly(Return(BattleField(0)));
  81. }
  82. #if SCRIPTING_ENABLED
  83. scripting::Pool * BattleFake::getContextPool() const
  84. {
  85. return pool.get();
  86. }
  87. #endif
  88. }
  89. }