From a886f6a34c000b98d1954a48ce6e1d7d90a0c54a Mon Sep 17 00:00:00 2001 From: maelstrom Date: Mon, 15 Dec 2025 22:37:55 +0100 Subject: [PATCH] test(datamodel): accessing and using properties with lua --- core/src/datatypes/vector.h | 2 +- core/src/partassembly.cpp | 4 +-- tests/CMakeLists.txt | 2 ++ tests/src/lua/luainst.cpp | 64 +++++++++++++++++++++++++++++++++++++ tests/src/testutil.h | 3 +- 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 tests/src/lua/luainst.cpp diff --git a/core/src/datatypes/vector.h b/core/src/datatypes/vector.h index 9dae1a6..06226b5 100644 --- a/core/src/datatypes/vector.h +++ b/core/src/datatypes/vector.h @@ -13,7 +13,7 @@ class DEF_DATA Vector3 { public: DEF_DATA_CTOR Vector3(); 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&); virtual ~Vector3(); diff --git a/core/src/partassembly.cpp b/core/src/partassembly.cpp index 6292eaa..7d6465a 100644 --- a/core/src/partassembly.cpp +++ b/core/src/partassembly.cpp @@ -68,7 +68,7 @@ void PartAssembly::SetCollisionsEnabled(bool enabled) { void PartAssembly::SetOrigin(CFrame newOrigin) { for (auto part : parts) { part->cframe = newOrigin * (_assemblyOrigin.Inverse() * part->cframe); - part->velocity = 0; // Reset velocity + part->velocity = Vector3(0); // Reset velocity part->UpdateProperty("CFrame"); part->UpdateProperty("Velocity"); // sendPropertyUpdatedSignal(part, "CFrame", Variant(part->cframe)); @@ -80,7 +80,7 @@ void PartAssembly::SetOrigin(CFrame newOrigin) { void PartAssembly::TransformBy(CFrame transform) { for (auto part : parts) { part->cframe = transform * part->cframe; - part->velocity = 0; // Reset velocity + part->velocity = Vector3(0); // Reset velocity part->UpdateProperty("CFrame"); part->UpdateProperty("Position"); part->UpdateProperty("Rotation"); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3847337..7f70083 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,7 +6,9 @@ add_executable(obtest src/lua/luasched.cpp src/lua/luasignal.cpp src/lua/luageneric.cpp + src/lua/luainst.cpp src/objectmodel/basic.cpp + # src/objectmodel/datamodel.cpp ) target_link_libraries(obtest PRIVATE openblocks Catch2::Catch2WithMain) target_include_directories(obtest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) diff --git a/tests/src/lua/luainst.cpp b/tests/src/lua/luainst.cpp new file mode 100644 index 0000000..bb07703 --- /dev/null +++ b/tests/src/lua/luainst.cpp @@ -0,0 +1,64 @@ +#include + +#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(); + +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)); + } +} \ No newline at end of file diff --git a/tests/src/testutil.h b/tests/src/testutil.h index d26f52f..890f7f2 100644 --- a/tests/src/testutil.h +++ b/tests/src/testutil.h @@ -9,9 +9,8 @@ #define TT_ADVANCETIME(secs) tu_set_override(tu_clock_micros() + (secs) * 1'000'000); inline std::string luaEvalOut(std::shared_ptr m, std::string source) { - testLogOutput.seekp(0, std::ios::end); size_t offset = testLogOutput.tellp(); - testLogOutput.seekp(0); + testLogOutput.seekp(0, std::ios::end); auto ss = m->GetService(); auto s = Script::New();