LuaSpellEffectTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * LuaSpellEffectTest.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 "../spells/effects/EffectFixture.h"
  12. #include "../JsonComparer.h"
  13. #include "../mock/mock_scripting_Context.h"
  14. #include "../mock/mock_scripting_Script.h"
  15. #include "../mock/mock_scripting_Service.h"
  16. #include "../mock/mock_spells_effects_Registry.h"
  17. #include "../mock/mock_ServerCallback.h"
  18. #include "../../../lib/VCMI_Lib.h"
  19. #include "../../lib/json/JsonUtils.h"
  20. #include "../../../lib/ScriptHandler.h"
  21. #include "../../../lib/CScriptingModule.h"
  22. namespace test
  23. {
  24. using namespace ::spells;
  25. using namespace ::spells::effects;
  26. using namespace ::scripting;
  27. using namespace ::testing;
  28. class LuaSpellEffectTest : public Test, public EffectFixture
  29. {
  30. public:
  31. const std::string SCRIPT_NAME = "testScript";
  32. const int32_t EFFECT_LEVEL = 1456;
  33. const int32_t RANGE_LEVEL = 4561;
  34. const int32_t EFFECT_POWER = 4789;
  35. const int32_t EFFECT_DURATION = 42;
  36. const int32_t EFFECT_VALUE = 42000;//TODO: this should be 64 bit
  37. ServiceMock serviceMock;
  38. ScriptMock scriptMock;
  39. std::shared_ptr<ContextMock> contextMock;
  40. EffectFactoryMock factoryMock;
  41. RegistryMock registryMock;
  42. RegistryMock::FactoryPtr factory;
  43. StrictMock<ServerCallbackMock> serverMock;
  44. JsonNode request;
  45. LuaSpellEffectTest()
  46. : EffectFixture("testScript")
  47. {
  48. contextMock = std::make_shared<ContextMock>();
  49. }
  50. void expectSettingContextVariables()
  51. {
  52. EXPECT_CALL(mechanicsMock, getEffectLevel()).WillRepeatedly(Return(EFFECT_LEVEL));
  53. EXPECT_CALL(*contextMock, setGlobal(Eq("effectLevel"), Matcher<int>(Eq(EFFECT_LEVEL))));
  54. EXPECT_CALL(mechanicsMock, getRangeLevel()).WillRepeatedly(Return(RANGE_LEVEL));
  55. EXPECT_CALL(*contextMock, setGlobal(Eq("effectRangeLevel"), Matcher<int>(Eq(RANGE_LEVEL))));
  56. EXPECT_CALL(mechanicsMock, getEffectPower()).WillRepeatedly(Return(EFFECT_POWER));
  57. EXPECT_CALL(*contextMock, setGlobal(Eq("effectPower"), Matcher<int>(Eq(EFFECT_POWER))));
  58. EXPECT_CALL(mechanicsMock, getEffectDuration()).WillRepeatedly(Return(EFFECT_DURATION));
  59. EXPECT_CALL(*contextMock, setGlobal(Eq("effectDuration"), Matcher<int>(Eq(EFFECT_DURATION))));
  60. EXPECT_CALL(mechanicsMock, getEffectValue()).WillRepeatedly(Return(EFFECT_VALUE));
  61. EXPECT_CALL(*contextMock, setGlobal(Eq("effectValue"), Matcher<int>(Eq(EFFECT_VALUE))));
  62. }
  63. void setDefaultExpectations()
  64. {
  65. EXPECT_CALL(mechanicsMock, scripts()).WillRepeatedly(Return(&serviceMock));
  66. EXPECT_CALL(*pool, getContext(Eq(&scriptMock))).WillOnce(Return(contextMock));
  67. expectSettingContextVariables();
  68. // JsonNode options(JsonNode::JsonType::DATA_STRUCT);
  69. // //TODO: test passing configuration
  70. // EffectFixture::setupEffect(options);
  71. }
  72. JsonNode saveRequest(const std::string &, const JsonNode & parameters)
  73. {
  74. JsonNode response(true);
  75. request = parameters;
  76. return response;
  77. }
  78. JsonNode saveRequest2(ServerCallback *, const std::string &, const JsonNode & parameters)
  79. {
  80. JsonNode response(true);
  81. request = parameters;
  82. return response;
  83. }
  84. protected:
  85. void SetUp() override
  86. {
  87. EXPECT_CALL(registryMock, add(Eq(SCRIPT_NAME), _)).WillOnce(SaveArg<1>(&factory));
  88. EXPECT_CALL(scriptMock, getName()).WillRepeatedly(ReturnRef(SCRIPT_NAME));
  89. VLC->scriptHandler->lua->registerSpellEffect(&registryMock, &scriptMock);
  90. GTEST_ASSERT_NE(factory, nullptr);
  91. subject.reset(factory->create());
  92. EffectFixture::setUp();
  93. }
  94. };
  95. TEST_F(LuaSpellEffectTest, ApplicableRedirected)
  96. {
  97. setDefaultExpectations();
  98. JsonNode response(true);
  99. EXPECT_CALL(*contextMock, callGlobal(Eq("applicable"),_)).WillOnce(Return(response));//TODO: check call parameter
  100. EXPECT_TRUE(subject->applicable(problemMock, &mechanicsMock));
  101. }
  102. TEST_F(LuaSpellEffectTest, ApplicableTargetRedirected)
  103. {
  104. setDefaultExpectations();
  105. EXPECT_CALL(*contextMock, callGlobal(Eq("applicableTarget"),_)).WillOnce(Invoke(this, &LuaSpellEffectTest::saveRequest));
  106. auto & unit1 = unitsFake.add(BattleSide::ATTACKER);
  107. EffectTarget target;
  108. BattleHex hex1(6,7);
  109. BattleHex hex2(7,8);
  110. int32_t id1 = 42;
  111. EXPECT_CALL(unit1, unitId()).WillOnce(Return(id1));
  112. target.emplace_back(&unit1, hex1);
  113. target.emplace_back(hex2);
  114. EXPECT_TRUE(subject->applicable(problemMock, &mechanicsMock, target));
  115. JsonNode first;
  116. first.Vector().push_back(JsonNode(hex1.hex));
  117. first.Vector().push_back(JsonNode(id1));
  118. JsonNode second;
  119. second.Vector().push_back(JsonNode(hex2.hex));
  120. second.Vector().push_back(JsonNode(-1));
  121. JsonNode targets;
  122. targets.Vector().push_back(first);
  123. targets.Vector().push_back(second);
  124. JsonNode expected;
  125. expected.Vector().push_back(targets);
  126. JsonComparer c(false);
  127. c.compare("applicableTarget request", request, expected);
  128. }
  129. TEST_F(LuaSpellEffectTest, ApplyRedirected)
  130. {
  131. setDefaultExpectations();
  132. EXPECT_CALL(*contextMock, callGlobal(Eq(&serverMock), Eq("apply"),_)).WillOnce(Invoke(this, &LuaSpellEffectTest::saveRequest2));
  133. auto & unit1 = unitsFake.add(BattleSide::ATTACKER);
  134. EffectTarget target;
  135. BattleHex hex1(6,7);
  136. int32_t id1 = 42;
  137. EXPECT_CALL(unit1, unitId()).WillOnce(Return(id1));
  138. target.emplace_back(&unit1, hex1);
  139. subject->apply(&serverMock, &mechanicsMock, target);
  140. JsonNode first;
  141. first.Vector().push_back(JsonNode(hex1.hex));
  142. first.Vector().push_back(JsonNode(id1));
  143. JsonNode targets;
  144. targets.Vector().push_back(first);
  145. JsonNode expected;
  146. expected.Vector().push_back(targets);
  147. JsonComparer c(false);
  148. c.compare("apply request", request, expected);
  149. }
  150. }