49 lines
1.3 KiB
C++
49 lines
1.3 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"
|
||
|
|
||
|
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
|