test(datamodel): accessing and using properties with lua

This commit is contained in:
maelstrom 2025-12-15 22:37:55 +01:00
parent b61362cc07
commit a886f6a34c
5 changed files with 70 additions and 5 deletions

View file

@ -13,7 +13,7 @@ class DEF_DATA Vector3 {
public: public:
DEF_DATA_CTOR Vector3(); DEF_DATA_CTOR Vector3();
DEF_DATA_CTOR Vector3(float x, float y, float z); DEF_DATA_CTOR Vector3(float x, float y, float z);
inline Vector3(float value) : Vector3(value, value, value) {} explicit inline Vector3(float value) : Vector3(value, value, value) {}
Vector3(const glm::vec3&); Vector3(const glm::vec3&);
virtual ~Vector3(); virtual ~Vector3();

View file

@ -68,7 +68,7 @@ void PartAssembly::SetCollisionsEnabled(bool enabled) {
void PartAssembly::SetOrigin(CFrame newOrigin) { void PartAssembly::SetOrigin(CFrame newOrigin) {
for (auto part : parts) { for (auto part : parts) {
part->cframe = newOrigin * (_assemblyOrigin.Inverse() * part->cframe); part->cframe = newOrigin * (_assemblyOrigin.Inverse() * part->cframe);
part->velocity = 0; // Reset velocity part->velocity = Vector3(0); // Reset velocity
part->UpdateProperty("CFrame"); part->UpdateProperty("CFrame");
part->UpdateProperty("Velocity"); part->UpdateProperty("Velocity");
// sendPropertyUpdatedSignal(part, "CFrame", Variant(part->cframe)); // sendPropertyUpdatedSignal(part, "CFrame", Variant(part->cframe));
@ -80,7 +80,7 @@ void PartAssembly::SetOrigin(CFrame newOrigin) {
void PartAssembly::TransformBy(CFrame transform) { void PartAssembly::TransformBy(CFrame transform) {
for (auto part : parts) { for (auto part : parts) {
part->cframe = transform * part->cframe; part->cframe = transform * part->cframe;
part->velocity = 0; // Reset velocity part->velocity = Vector3(0); // Reset velocity
part->UpdateProperty("CFrame"); part->UpdateProperty("CFrame");
part->UpdateProperty("Position"); part->UpdateProperty("Position");
part->UpdateProperty("Rotation"); part->UpdateProperty("Rotation");

View file

@ -6,7 +6,9 @@ add_executable(obtest
src/lua/luasched.cpp src/lua/luasched.cpp
src/lua/luasignal.cpp src/lua/luasignal.cpp
src/lua/luageneric.cpp src/lua/luageneric.cpp
src/lua/luainst.cpp
src/objectmodel/basic.cpp src/objectmodel/basic.cpp
# src/objectmodel/datamodel.cpp
) )
target_link_libraries(obtest PRIVATE openblocks Catch2::Catch2WithMain) target_link_libraries(obtest PRIVATE openblocks Catch2::Catch2WithMain)
target_include_directories(obtest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) target_include_directories(obtest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)

64
tests/src/lua/luainst.cpp Normal file
View file

@ -0,0 +1,64 @@
#include <catch2/catch_test_macros.hpp>
#include "objects/model.h"
#include "objects/part/part.h"
#include "testcommon.h"
#include "testutil.h"
static auto& m = gTestModel;
static auto& out = testLogOutput;
// static auto& ctx = m->GetService<ScriptContext>();
TEST_CASE("Access instances") {
SECTION("Basic access") {
auto model = Model::New();
m->AddChild(model);
REQUIRE(luaEvalOut(m, "print(game.Model ~= nil)") == "INFO: true\n");
REQUIRE(luaEvalOut(m, "print(workspace.Parent.Model ~= nil)") == "INFO: true\n");
REQUIRE(luaEvalOut(m, "print(game.Model.Parent.Model ~= nil)") == "INFO: true\n");
}
SECTION("Property comes first") {
auto model = Model::New();
model->name = "Parent";
m->AddChild(model);
REQUIRE(luaEvalOut(m, "print(game.Parent == nil)") == "INFO: true\n");
}
SECTION("Reading properties") {
auto part = Part::New();
part->transparency = 2.f;
part->anchored = true;
part->cframe = CFrame() + Vector3(-2, 5, 3);
part->UpdateProperty("CFrame");
m->AddChild(part);
REQUIRE(luaEvalOut(m, "print(game.Part)") == "INFO: Part\n"); // tostring
REQUIRE(luaEvalOut(m, "print(game.Part.Name)") == "INFO: Part\n");
REQUIRE(luaEvalOut(m, "print(game.Part.Transparency)") == "INFO: 2\n");
REQUIRE(luaEvalOut(m, "print(game.Part.Anchored)") == "INFO: true\n");
REQUIRE(luaEvalOut(m, "print(game.Part.Position)") == "INFO: -2, 5, 3\n");
}
SECTION("Writing properties") {
auto part = Part::New();
m->AddChild(part);
std::string out = luaEvalOut(m, R"(
local part = game.Part
part.Name = "Some name"
part.Transparency = 1.0
part.Anchored = true
part.Position = Vector3.new(2, 3, 4)
)");
// No error
REQUIRE(out == "");
REQUIRE(part->name == "Some name");
REQUIRE(part->transparency == 1.0);
REQUIRE(part->anchored == true);
REQUIRE(part->position() == Vector3(2, 3, 4));
}
}

View file

@ -9,9 +9,8 @@
#define TT_ADVANCETIME(secs) tu_set_override(tu_clock_micros() + (secs) * 1'000'000); #define TT_ADVANCETIME(secs) tu_set_override(tu_clock_micros() + (secs) * 1'000'000);
inline std::string luaEvalOut(std::shared_ptr<DataModel> m, std::string source) { inline std::string luaEvalOut(std::shared_ptr<DataModel> m, std::string source) {
testLogOutput.seekp(0, std::ios::end);
size_t offset = testLogOutput.tellp(); size_t offset = testLogOutput.tellp();
testLogOutput.seekp(0); testLogOutput.seekp(0, std::ios::end);
auto ss = m->GetService<ServerScriptService>(); auto ss = m->GetService<ServerScriptService>();
auto s = Script::New(); auto s = Script::New();