2019-06-14 12:59:38 +00:00
|
|
|
|
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "../src/Script.hpp"
|
|
|
|
#include "../../src/UserData/UserData.hpp"
|
|
|
|
#include "../../src/UserData/UserDataStorage.hpp"
|
|
|
|
#include "../../src/UserData/UserDataValue.hpp"
|
2019-06-21 15:03:13 +00:00
|
|
|
#include "../../src/UserData/UserDataFunction.hpp"
|
|
|
|
#include "../../src/UserData/UserDataFunctionType.hpp"
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
using namespace Porygon;
|
|
|
|
using namespace Porygon::UserData;
|
|
|
|
using namespace Porygon::Utilities;
|
2019-06-14 12:59:38 +00:00
|
|
|
|
|
|
|
class UserDataTestObject{
|
|
|
|
public:
|
|
|
|
int foo = 10;
|
2019-06-21 15:03:13 +00:00
|
|
|
int getFoo(){
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Addition(int a, int b){
|
|
|
|
return a + b;
|
|
|
|
}
|
2019-06-14 12:59:38 +00:00
|
|
|
|
2019-06-21 15:03:13 +00:00
|
|
|
private:
|
2019-06-14 12:59:38 +00:00
|
|
|
static EvalValue* GetFoo(void* obj){
|
|
|
|
return new IntegerEvalValue(((UserDataTestObject*)obj)->foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetFoo(void* obj, EvalValue* val){
|
|
|
|
((UserDataTestObject*)obj)->foo = val->EvaluateInteger();
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:03:13 +00:00
|
|
|
static EvalValue* CallFooFunction(void* obj, EvalValue* parameters[], int parameterCount){
|
|
|
|
return new IntegerEvalValue(((UserDataTestObject*)obj)->getFoo());
|
|
|
|
}
|
|
|
|
|
|
|
|
static EvalValue* GetFooFunction(void* obj){
|
|
|
|
return new UserDataFunction(CallFooFunction, obj,
|
|
|
|
make_shared<UserDataFunctionType>(make_shared<NumericScriptType>(true, false), vector<shared_ptr<ScriptType>>(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static EvalValue* CallAddition(void* obj, EvalValue* parameters[], int parameterCount){
|
|
|
|
return new IntegerEvalValue(((UserDataTestObject*)obj)->Addition(
|
|
|
|
parameters[0] -> EvaluateInteger(),
|
|
|
|
parameters[1] -> EvaluateInteger()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
static GenericFunctionScriptType* AdditionFunctionType;
|
|
|
|
|
|
|
|
static EvalValue* GetAdditionFunction(void* obj){
|
|
|
|
return new UserDataFunction(CallAddition, obj, shared_ptr<GenericFunctionScriptType>(AdditionFunctionType));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-06-17 16:35:12 +00:00
|
|
|
static Porygon::UserData::UserData* CreateData(){
|
|
|
|
return new Porygon::UserData::UserData({
|
2019-06-14 12:59:38 +00:00
|
|
|
{
|
|
|
|
HashedString::ConstHash("foo"),
|
|
|
|
new UserDataField(new NumericScriptType(true, false), GetFoo, SetFoo)
|
2019-06-21 15:03:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
HashedString::ConstHash("getFoo"),
|
|
|
|
new UserDataField(new UserDataFunctionType(make_shared<NumericScriptType>(true, false), {}), GetFooFunction, nullptr)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HashedString::ConstHash("Addition"),
|
|
|
|
new UserDataField(AdditionFunctionType, GetAdditionFunction, nullptr)
|
2019-06-14 12:59:38 +00:00
|
|
|
}
|
2019-06-21 15:03:13 +00:00
|
|
|
});
|
2019-06-14 12:59:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-21 15:03:13 +00:00
|
|
|
GenericFunctionScriptType* UserDataTestObject::AdditionFunctionType =
|
|
|
|
new UserDataFunctionType(make_shared<NumericScriptType>(true, false),
|
|
|
|
vector<shared_ptr<ScriptType>>{
|
|
|
|
make_shared<NumericScriptType>(true, false),
|
|
|
|
make_shared<NumericScriptType>(true, false)
|
|
|
|
});
|
|
|
|
|
2019-06-14 12:59:38 +00:00
|
|
|
TEST_CASE( "Gets UserData value", "[integration]" ) {
|
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::CreateData());
|
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
function testFunc(testObject obj)
|
|
|
|
return obj["foo"]
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), new UserDataTestObject());
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->CallFunction(u"testFunc", {parameter});
|
2019-06-14 12:59:38 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
REQUIRE(variable->EvaluateInteger() == 10);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:12:27 +00:00
|
|
|
TEST_CASE( "Sets UserData value", "[integration]" ) {
|
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::CreateData());
|
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
function testFunc(testObject obj)
|
|
|
|
obj["foo"] = 5000
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
auto obj = new UserDataTestObject();
|
|
|
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
|
2019-06-15 15:20:27 +00:00
|
|
|
script->CallFunction(u"testFunc", {parameter});
|
2019-06-14 15:12:27 +00:00
|
|
|
delete script;
|
2019-06-14 15:35:05 +00:00
|
|
|
REQUIRE(obj->foo == 5000);
|
|
|
|
delete obj;
|
|
|
|
delete parameter;
|
2019-06-14 15:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:03:13 +00:00
|
|
|
TEST_CASE( "Calls UserData function", "[integration]" ) {
|
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::CreateData());
|
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
function testFunc(testObject obj)
|
|
|
|
return obj.getFoo()
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
auto obj = new UserDataTestObject();
|
|
|
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
|
|
|
|
auto result = script->CallFunction(u"testFunc", {parameter});
|
|
|
|
REQUIRE(result -> EvaluateInteger() == 10);
|
|
|
|
delete script;
|
|
|
|
delete obj;
|
|
|
|
delete parameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Calls UserData function with parameters", "[integration]" ) {
|
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::CreateData());
|
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
function testFunc(testObject obj)
|
|
|
|
return obj.Addition(5046, 8432)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
auto obj = new UserDataTestObject();
|
|
|
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
|
|
|
|
auto result = script->CallFunction(u"testFunc", {parameter});
|
|
|
|
REQUIRE(result -> EvaluateInteger() == 13478);
|
|
|
|
delete script;
|
|
|
|
delete obj;
|
|
|
|
delete parameter;
|
|
|
|
}
|
2019-06-14 15:12:27 +00:00
|
|
|
|
2019-06-14 12:59:38 +00:00
|
|
|
|
|
|
|
#endif
|