File: ExamplesTest.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 (129 lines) | stat: -rw-r--r-- 2,975 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
/*
 * ExamplesTest.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/VCMI_Lib.h"
#include "../../lib/ScriptHandler.h"
#include "../../lib/NetPacks.h"
#include "../../lib/serializer/Cast.h"
#include "../../lib/VCMIDirs.h"
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/filesystem/FileInfo.h"


///All unsorted ERM tests goes here

namespace test
{

using namespace ::testing;
using namespace ::scripting;

class ExamplesTest : public Test, public ScriptFixture
{
public:
	std::vector<std::string> actualMessages;

	ExamplesTest()
		: ScriptFixture()
	{
	}

	void setDefaultExpectaions()
	{
		EXPECT_CALL(infoMock, getLocalPlayer()).WillRepeatedly(Return(PlayerColor(3)));
		EXPECT_CALL(serverMock, apply(Matcher<CPackForClient *>(_))).WillRepeatedly(Invoke(this, &ExamplesTest::onCommit));
	}

	void onCommit(CPackForClient * pack)
	{
		InfoWindow * iw = dynamic_ptr_cast<InfoWindow>(pack);

		if(iw)
		{
			actualMessages.push_back(iw->text.toString());
			EXPECT_EQ(iw->player, PlayerColor(3));
		}
		else
		{
			GTEST_FAIL() << "Invalid NetPack";
		}

	}

	void saveScript(const std::string & name)
	{
#ifdef VCMI_DUMP_TEST_SCRIPTS

	auto path = VCMIDirs::get().userDataPath() / name;

	boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::trunc);

	tmp.write(subject->code.c_str(), subject->code.size());
	tmp.flush();
	tmp.close();

#endif
	}

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

TEST_F(ExamplesTest, ALL)
{
	setDefaultExpectaions();
	auto sources = CResourceHandler::get()->getFilteredFiles([](const ResourceID & ident)
	{
		return ident.getType() == EResType::ERM && boost::algorithm::starts_with(ident.getName(), "SCRIPTS/TEST/ERM/");
	});

	for(const ResourceID & file : sources)
	{
		actualMessages.clear();
		boost::filesystem::path scriptPath(file.getName() + ".VERM");
		boost::filesystem::path baseName = scriptPath.stem();
		boost::filesystem::path basePath = boost::filesystem::path("TEST/ERM/") / scriptPath.stem();

		std::string dataName = basePath.string()+".JSON";
		std::string scriptName = basePath.string()+".VERM";

		ResourceID dataPath(dataName);

		if(!CResourceHandler::get()->existsResource(dataPath))
		{
			GTEST_FAIL() << dataName << " does not exists";
			return;
		}

		const JsonNode expectedState(dataPath);

		loadScriptFromFile(scriptName);

		saveScript(baseName.string()+".lua");

		const JsonNode actualState = runServer();

		SCOPED_TRACE("\n"+actualState.toJson(true));

		JsonComparer c(false);
		c.compare(baseName.string(), actualState["ERM"], expectedState["ERM"]);

		auto expectedMessages = expectedState["messages"].convertTo<std::vector<std::string>>();

		EXPECT_THAT(actualMessages, Eq(expectedMessages));
	}
}

}