File: ERM_UN.cpp

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (128 lines) | stat: -rw-r--r-- 2,777 bytes parent folder | download | duplicates (2)
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
/*
 * ERM_UN.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 "../scripting/ScriptFixture.h"

#include "../../lib/NetPacks.h"
#include "../../lib/serializer/Cast.h"

#include "../mock/mock_CreatureService.h"
#include "../mock/mock_Creature.h"

namespace test
{
namespace scripting
{

using namespace ::testing;

class ERM_UN : public Test, public ScriptFixture
{
public:
	std::vector<EntityChanges> actualChanges;

	void onCommit(CPackForClient * pack)
	{
		EntitiesChanged * ec = dynamic_ptr_cast<EntitiesChanged>(pack);

		if(ec)
			onEntitiesChanged(ec);
		else
			GTEST_FAIL() << "Invalid NetPack";
	}

	void onEntitiesChanged(EntitiesChanged * pack)
	{
		vstd::concatenate(actualChanges, pack->changes);
	}

protected:
	void SetUp() override
	{
		ScriptFixture::setUp();
	}
};

class ERM_UN_G1 : public ERM_UN
{
public:
	CreatureServiceMock creatureService;
	CreatureMock oldCreature;
	const std::string NAME = "A monster";
	const std::string NAMEP = "Many monsters";

protected:
	void SetUp() override
	{
		ERM_UN::SetUp();

		EXPECT_CALL(servicesMock, creatures()).WillRepeatedly(Return(&creatureService));
		EXPECT_CALL(creatureService, getByIndex(Eq(68))).WillRepeatedly(Return(&oldCreature));
	}
};

TEST_F(ERM_UN_G1, get)
{
	EXPECT_CALL(oldCreature, getSingularName()).WillOnce(ReturnRef(NAME));
	EXPECT_CALL(oldCreature, getPluralName()).WillOnce(ReturnRef(NAMEP));
	loadScript(VLC->scriptHandler->erm,
	{
		"VERM",
		"!#UN:G1/68/0/?z1;",
		"!#UN:G1/68/1/?z2;"
	});

	SCOPED_TRACE("\n" + subject->code);
	const JsonNode actualState = runServer();
	EXPECT_EQ(actualState["ERM"]["z"]["1"].String(), NAME);
	EXPECT_EQ(actualState["ERM"]["z"]["2"].String(), NAMEP);
}

TEST_F(ERM_UN_G1, set)
{
	EXPECT_CALL(serverMock, apply(Matcher<CPackForClient *>(_))).Times(AtLeast(1)).WillRepeatedly(Invoke(this, &ERM_UN::onCommit));

	loadScript(VLC->scriptHandler->erm,
	{
		"VERM",
		"!#VRz1:S^A monster^;",
		"!#VRz2:S^Many monsters^;",
		"!#UN:G1/68/0/z1;",
		"!#UN:G1/68/1/2;"
	});

	SCOPED_TRACE("\n" + subject->code);
	const JsonNode actualState = runServer();

	JsonNode merged(JsonNode::JsonType::DATA_STRUCT);

	for(EntityChanges & change : actualChanges)
	{
		EXPECT_EQ(change.metatype, Metatype::CREATURE);
		EXPECT_EQ(change.entityIndex,68);
		JsonUtils::merge(merged, change.data);
	}

	JsonNode expectedMerged(JsonNode::JsonType::DATA_STRUCT);

	JsonNode & config = expectedMerged["config"];
	config["name"]["singular"].String() = NAME;
	config["name"]["plural"].String() = NAMEP;

	{
		JsonComparer c(false);
		c.compare("updateData", merged, expectedMerged);
	}
}


}
}