PorygonLang/tests/integration/UserData.cpp

68 lines
1.9 KiB
C++
Raw Normal View History

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"
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(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);
script->CallFunction(u"testFunc", {parameter});
2019-06-14 15:12:27 +00:00
delete script;
REQUIRE(obj->foo == 5000);
delete obj;
delete parameter;
2019-06-14 15:12:27 +00:00
}
2019-06-14 12:59:38 +00:00
#endif