Implements basics for UserData

This commit is contained in:
2019-06-14 14:59:38 +02:00
parent 831dbe6917
commit 996b5be496
14 changed files with 232 additions and 1 deletions

View File

@@ -0,0 +1,48 @@
#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"
class UserDataTestObject{
public:
int foo = 10;
static EvalValue* GetFoo(void* obj){
return new IntegerEvalValue(((UserDataTestObject*)obj)->foo);
}
static void SetFoo(void* obj, EvalValue* val){
((UserDataTestObject*)obj)->foo = val->EvaluateInteger();
}
static UserData* CreateData(){
return new UserData({
{
HashedString::ConstHash("foo"),
new UserDataField(new NumericScriptType(true, false), GetFoo, SetFoo)
}
});
}
};
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());
auto variable = script->CallFunction("testFunc", {parameter});
REQUIRE(variable != nullptr);
REQUIRE(variable->EvaluateInteger() == 10);
delete script;
}
#endif