PorygonLang/tests/integration/UserData.cpp

71 lines
2.1 KiB
C++

#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"
using namespace Porygon;
using namespace Porygon::UserData;
using namespace Porygon::Utilities;
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 Porygon::UserData::UserData* CreateData(){
return new Porygon::UserData::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});
REQUIRE(variable != nullptr);
REQUIRE(variable->EvaluateInteger() == 10);
delete script;
}
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});
delete script;
REQUIRE(obj->foo == 5000);
delete obj;
delete parameter;
}
#endif