1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-30 23:18:08 +02:00
vcmi/test/scripting/PoolTest.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

117 lines
2.8 KiB
C++

/*
* PoolTest.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 "../../lib/ScriptHandler.h"
#include "../mock/mock_Environment.h"
#include "../mock/mock_ServerCallback.h"
#include "../mock/mock_scripting_Context.h"
#include "../mock/mock_scripting_Script.h"
#include "../JsonComparer.h"
namespace scripting
{
namespace test
{
using namespace ::testing;
class PoolTest : public Test
{
public:
const std::string SCRIPT_NAME = "TEST SCRIPT";
NiceMock<EnvironmentMock> env;
StrictMock<ScriptMock> script;
std::shared_ptr<PoolImpl> subject;
StrictMock<ServerCallbackMock> server;
void setDefaultExpectations()
{
EXPECT_CALL(script, getName()).WillRepeatedly(ReturnRef(SCRIPT_NAME));
}
protected:
void SetUp() override
{
subject = std::make_shared<PoolImpl>(&env);
}
};
TEST_F(PoolTest, CreatesContext)
{
setDefaultExpectations();
auto context = std::make_shared<NiceMock<ContextMock>>();
EXPECT_CALL(script, createContext(Eq(&env))).WillOnce(Return(context));
EXPECT_EQ(context, subject->getContext(&script));
}
TEST_F(PoolTest, ReturnsCachedContext)
{
setDefaultExpectations();
auto context = std::make_shared<NiceMock<ContextMock>>();
EXPECT_CALL(script, createContext(Eq(&env))).WillOnce(Return(context));
auto first = subject->getContext(&script);
EXPECT_EQ(first, subject->getContext(&script));
}
TEST_F(PoolTest, CreatesNewContextWithEmptyState)
{
setDefaultExpectations();
auto context = std::make_shared<StrictMock<ContextMock>>();
EXPECT_CALL(script, createContext(Eq(&env))).WillOnce(Return(context));
EXPECT_CALL(*context, run(Eq(JsonNode()))).Times(1);
subject->getContext(&script);//return value ignored
}
TEST_F(PoolTest, SavesScriptState)
{
setDefaultExpectations();
auto context = std::make_shared<StrictMock<ContextMock>>();
EXPECT_CALL(script, createContext(Eq(&env))).WillOnce(Return(context));
EXPECT_CALL(*context, run(Eq(JsonNode()))).Times(1);
subject->getContext(&script);
JsonNode expectedState;
expectedState[SCRIPT_NAME]["foo"].String() = "bar";
EXPECT_CALL(*context, saveState()).WillOnce(Return(expectedState[SCRIPT_NAME]));
JsonNode actualState;
subject->serializeState(true, actualState);
JsonComparer c(false);
c.compare("state", actualState, expectedState);
}
TEST_F(PoolTest, LoadsScriptState)
{
setDefaultExpectations();
auto context = std::make_shared<StrictMock<ContextMock>>();
EXPECT_CALL(script, createContext(Eq(&env))).WillOnce(Return(context));
JsonNode expectedState;
expectedState[SCRIPT_NAME]["foo"].String() = "bar";
subject->serializeState(false, expectedState);
EXPECT_CALL(*context, run(Eq(expectedState[SCRIPT_NAME]))).Times(1);
subject->getContext(&script);
}
}
}