File: LuaSpellEffectTest.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (210 lines) | stat: -rw-r--r-- 5,586 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * LuaSpellEffectTest.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 "../spells/effects/EffectFixture.h"
#include "../JsonComparer.h"

#include "../mock/mock_scripting_Context.h"
#include "../mock/mock_scripting_Script.h"
#include "../mock/mock_scripting_Service.h"
#include "../mock/mock_spells_effects_Registry.h"
#include "../mock/mock_ServerCallback.h"

#include "../../../lib/VCMI_Lib.h"
#include "../../lib/json/JsonUtils.h"
#include "../../../lib/ScriptHandler.h"
#include "../../../lib/CScriptingModule.h"

namespace test
{
using namespace ::spells;
using namespace ::spells::effects;
using namespace ::scripting;
using namespace ::testing;

class LuaSpellEffectTest : public Test, public EffectFixture
{
public:
	const std::string SCRIPT_NAME = "testScript";
	const int32_t EFFECT_LEVEL = 1456;
	const int32_t RANGE_LEVEL = 4561;
	const int32_t EFFECT_POWER = 4789;
	const int32_t EFFECT_DURATION = 42;

	const int32_t EFFECT_VALUE = 42000;//TODO: this should be 64 bit

	ServiceMock serviceMock;
	ScriptMock scriptMock;
	std::shared_ptr<ContextMock> contextMock;
	EffectFactoryMock factoryMock;

	RegistryMock registryMock;
	RegistryMock::FactoryPtr factory;
	StrictMock<ServerCallbackMock> serverMock;

	JsonNode request;

	LuaSpellEffectTest()
		: EffectFixture("testScript")
	{
		contextMock = std::make_shared<ContextMock>();
	}

	void expectSettingContextVariables()
	{
		EXPECT_CALL(mechanicsMock, getEffectLevel()).WillRepeatedly(Return(EFFECT_LEVEL));
		EXPECT_CALL(*contextMock, setGlobal(Eq("effectLevel"), Matcher<int>(Eq(EFFECT_LEVEL))));

		EXPECT_CALL(mechanicsMock, getRangeLevel()).WillRepeatedly(Return(RANGE_LEVEL));
		EXPECT_CALL(*contextMock, setGlobal(Eq("effectRangeLevel"), Matcher<int>(Eq(RANGE_LEVEL))));

		EXPECT_CALL(mechanicsMock, getEffectPower()).WillRepeatedly(Return(EFFECT_POWER));
		EXPECT_CALL(*contextMock, setGlobal(Eq("effectPower"), Matcher<int>(Eq(EFFECT_POWER))));

		EXPECT_CALL(mechanicsMock, getEffectDuration()).WillRepeatedly(Return(EFFECT_DURATION));
		EXPECT_CALL(*contextMock, setGlobal(Eq("effectDuration"), Matcher<int>(Eq(EFFECT_DURATION))));

		EXPECT_CALL(mechanicsMock, getEffectValue()).WillRepeatedly(Return(EFFECT_VALUE));
		EXPECT_CALL(*contextMock, setGlobal(Eq("effectValue"), Matcher<int>(Eq(EFFECT_VALUE))));
	}

	void setDefaultExpectations()
	{
		EXPECT_CALL(mechanicsMock, scripts()).WillRepeatedly(Return(&serviceMock));

		EXPECT_CALL(*pool, getContext(Eq(&scriptMock))).WillOnce(Return(contextMock));

		expectSettingContextVariables();

//		JsonNode options(JsonNode::JsonType::DATA_STRUCT);
//		//TODO: test passing configuration
//		EffectFixture::setupEffect(options);
	}

	JsonNode saveRequest(const std::string &, const JsonNode & parameters)
	{
		JsonNode response(true);

		request = parameters;
		return response;
	}

	JsonNode saveRequest2(ServerCallback *, const std::string &, const JsonNode & parameters)
	{
		JsonNode response(true);

		request = parameters;
		return response;
	}

protected:
	void SetUp() override
	{
		EXPECT_CALL(registryMock, add(Eq(SCRIPT_NAME), _)).WillOnce(SaveArg<1>(&factory));
		EXPECT_CALL(scriptMock, getName()).WillRepeatedly(ReturnRef(SCRIPT_NAME));
		VLC->scriptHandler->lua->registerSpellEffect(&registryMock, &scriptMock);

		GTEST_ASSERT_NE(factory, nullptr);
		subject.reset(factory->create());

		EffectFixture::setUp();
	}
};

TEST_F(LuaSpellEffectTest, ApplicableRedirected)
{
	setDefaultExpectations();

	JsonNode response(true);

	EXPECT_CALL(*contextMock, callGlobal(Eq("applicable"),_)).WillOnce(Return(response));//TODO: check call parameter

	EXPECT_TRUE(subject->applicable(problemMock, &mechanicsMock));
}

TEST_F(LuaSpellEffectTest, ApplicableTargetRedirected)
{
	setDefaultExpectations();

	EXPECT_CALL(*contextMock, callGlobal(Eq("applicableTarget"),_)).WillOnce(Invoke(this, &LuaSpellEffectTest::saveRequest));

	auto & unit1 = unitsFake.add(BattleSide::ATTACKER);

	EffectTarget target;

	BattleHex hex1(6,7);
	BattleHex hex2(7,8);

	int32_t id1 = 42;

	EXPECT_CALL(unit1, unitId()).WillOnce(Return(id1));

	target.emplace_back(&unit1, hex1);
	target.emplace_back(hex2);

	EXPECT_TRUE(subject->applicable(problemMock, &mechanicsMock, target));


	JsonNode first;
	first.Vector().emplace_back(hex1.toInt());
	first.Vector().emplace_back(id1);

	JsonNode second;
	second.Vector().emplace_back(hex2.toInt());
	second.Vector().emplace_back(-1);

	JsonNode targets;
	targets.Vector().push_back(first);
	targets.Vector().push_back(second);

	JsonNode expected;
	expected.Vector().push_back(targets);

	JsonComparer c(false);
	c.compare("applicableTarget request", request, expected);
}

TEST_F(LuaSpellEffectTest, ApplyRedirected)
{
	setDefaultExpectations();

	EXPECT_CALL(*contextMock, callGlobal(Eq(&serverMock), Eq("apply"),_)).WillOnce(Invoke(this, &LuaSpellEffectTest::saveRequest2));

	auto & unit1 = unitsFake.add(BattleSide::ATTACKER);

	EffectTarget target;

	BattleHex hex1(6,7);

	int32_t id1 = 42;

	EXPECT_CALL(unit1, unitId()).WillOnce(Return(id1));

	target.emplace_back(&unit1, hex1);

	subject->apply(&serverMock, &mechanicsMock, target);

	JsonNode first;
	first.Vector().emplace_back(hex1.toInt());
	first.Vector().emplace_back(id1);

	JsonNode targets;
	targets.Vector().push_back(first);

	JsonNode expected;
	expected.Vector().push_back(targets);

	JsonComparer c(false);
	c.compare("apply request", request, expected);
}

}