1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-26 08:41:13 +02:00
vcmi/test/erm/ExamplesTest.cpp
AlexVinS ecaa9f5d0b Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2021-02-14 19:05:43 +03:00

130 lines
2.9 KiB
C++

/*
* 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));
}
}
}