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-28 15:02:38 +00:00
|
|
|
#include "../../src/UserData/UserDataTemplates.hpp"
|
2019-07-28 17:01:07 +00:00
|
|
|
#include "../../src/UserData/UserDataCollectionType.hpp"
|
|
|
|
#include "../../src/UserData/UserDataCollectionValue.hpp"
|
2019-06-28 16:31:24 +00:00
|
|
|
#include "../../src/Evaluator/EvalValues/EvalValueHelper.hpp"
|
2019-07-28 17:01:07 +00:00
|
|
|
#include "../../src/Utilities/Random.hpp"
|
2019-06-21 15:03:13 +00:00
|
|
|
|
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-07-28 17:01:07 +00:00
|
|
|
vector<int> fooVector = {5,10,15,25};
|
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-28 15:02:38 +00:00
|
|
|
// Declare script properties
|
2019-06-21 15:03:13 +00:00
|
|
|
private:
|
2019-06-28 21:38:47 +00:00
|
|
|
PORYGON_PREPARE_FUNCTION(UserDataTestObject, getFoo, IntegerEvalValue)
|
|
|
|
PORYGON_PREPARE_FUNCTION(UserDataTestObject, Addition, IntegerEvalValue, (par[0] -> EvaluateInteger()), (par[1] -> EvaluateInteger()))
|
2019-07-28 17:01:07 +00:00
|
|
|
void __setFooVector(const EvalValue* key, const EvalValue* value){
|
|
|
|
auto index = key->EvaluateInteger();
|
|
|
|
fooVector[index] = value->EvaluateInteger();
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:03:13 +00:00
|
|
|
public:
|
2019-06-28 21:38:47 +00:00
|
|
|
PORYGON_USERDATA(UserDataTestObject,
|
|
|
|
PORYGON_INTEGER_FIELD(foo)
|
|
|
|
PORYGON_INTEGER_FUNCTION(getFoo)
|
|
|
|
PORYGON_INTEGER_FUNCTION(Addition, PORYGON_INTEGER_TYPE, PORYGON_INTEGER_TYPE)
|
2019-07-28 17:01:07 +00:00
|
|
|
PORYGON_READONLY_VECTOR_FIELD(fooVector, PORYGON_INTEGER_TYPE)
|
2019-06-28 15:53:37 +00:00
|
|
|
)
|
2019-06-14 12:59:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE( "Gets UserData value", "[integration]" ) {
|
2019-06-28 15:02:38 +00:00
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::__createUserData());
|
2019-06-14 12:59:38 +00:00
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
function testFunc(testObject obj)
|
|
|
|
return obj["foo"]
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-24 11:38:41 +00:00
|
|
|
auto par = new UserDataTestObject();
|
|
|
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), par);
|
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);
|
2019-06-24 11:38:41 +00:00
|
|
|
delete par;
|
|
|
|
delete parameter;
|
2019-06-14 12:59:38 +00:00
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
2019-06-24 11:38:41 +00:00
|
|
|
UserDataStorage::ClearTypes();
|
2019-06-14 12:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:12:27 +00:00
|
|
|
TEST_CASE( "Sets UserData value", "[integration]" ) {
|
2019-06-28 15:02:38 +00:00
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::__createUserData());
|
2019-06-14 15:12:27 +00:00
|
|
|
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-24 11:38:41 +00:00
|
|
|
UserDataStorage::ClearTypes();
|
2019-06-14 15:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:03:13 +00:00
|
|
|
TEST_CASE( "Calls UserData function", "[integration]" ) {
|
2019-06-28 15:02:38 +00:00
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::__createUserData());
|
2019-06-21 15:03:13 +00:00
|
|
|
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;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete result;
|
2019-06-24 11:38:41 +00:00
|
|
|
UserDataStorage::ClearTypes();
|
2019-06-21 15:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Calls UserData function with parameters", "[integration]" ) {
|
2019-06-28 15:02:38 +00:00
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::__createUserData());
|
2019-06-21 15:03:13 +00:00
|
|
|
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-07-27 15:59:42 +00:00
|
|
|
delete result;
|
2019-06-24 11:38:41 +00:00
|
|
|
UserDataStorage::ClearTypes();
|
2019-06-21 15:03:13 +00:00
|
|
|
}
|
2019-06-14 15:12:27 +00:00
|
|
|
|
2019-07-28 17:01:07 +00:00
|
|
|
TEST_CASE( "Gets userdata vector value", "[integration]" ) {
|
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::__createUserData());
|
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
function testFunc(testObject obj)
|
|
|
|
return obj.fooVector[1]
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
auto func = (GenericFunctionEvalValue*)script -> GetVariable(u"testFunc");
|
|
|
|
auto funcType = func -> GetType();
|
|
|
|
REQUIRE(funcType->GetFirstOption()->GetReturnType()->GetClass() == TypeClass::Number);
|
|
|
|
auto obj = new UserDataTestObject();
|
|
|
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
|
|
|
|
auto result = script->CallFunction(u"testFunc", {parameter});
|
|
|
|
REQUIRE(result -> EvaluateInteger() == 5);
|
|
|
|
delete obj;
|
|
|
|
delete parameter;
|
|
|
|
delete result;
|
|
|
|
delete script;
|
|
|
|
delete func;
|
|
|
|
UserDataStorage::ClearTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Sets userdata vector value", "[integration]" ) {
|
|
|
|
UserDataStorage::RegisterType(HashedString::ConstHash("testObject"), UserDataTestObject::__createUserData());
|
|
|
|
Script* script = Script::Create(R"(
|
|
|
|
function testFunc(testObject obj)
|
|
|
|
obj.fooVector[3] = 684
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
auto func = (GenericFunctionEvalValue*)script -> GetVariable(u"testFunc");
|
|
|
|
auto funcType = func -> GetType();
|
|
|
|
auto obj = new UserDataTestObject();
|
|
|
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
|
|
|
|
script->CallFunction(u"testFunc", {parameter});
|
|
|
|
REQUIRE(obj->fooVector[2] == 684);
|
|
|
|
delete obj;
|
|
|
|
delete parameter;
|
|
|
|
delete script;
|
|
|
|
delete func;
|
|
|
|
UserDataStorage::ClearTypes();
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:59:38 +00:00
|
|
|
|
|
|
|
#endif
|