From afdde26a184fa79a6b7e397ad37f4fbdc910f462 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 22 Mar 2023 13:02:05 +0100 Subject: [PATCH] Add tests for reading & writing roblox place & model files --- packages/lib-roblox/src/document/mod.rs | 4 ++-- packages/lib-roblox/src/instance/mod.rs | 15 ++++++--------- tests/roblox/files/readModelFile.luau | 20 ++++++++++++++++++++ tests/roblox/files/readPlaceFile.luau | 18 ++++++++++++++++++ tests/roblox/files/writeModelFile.luau | 10 ++++++++++ tests/roblox/files/writePlaceFile.luau | 15 +++++++++++++++ 6 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 tests/roblox/files/readModelFile.luau create mode 100644 tests/roblox/files/readPlaceFile.luau create mode 100644 tests/roblox/files/writeModelFile.luau create mode 100644 tests/roblox/files/writePlaceFile.luau diff --git a/packages/lib-roblox/src/document/mod.rs b/packages/lib-roblox/src/document/mod.rs index 31857a2..945f871 100644 --- a/packages/lib-roblox/src/document/mod.rs +++ b/packages/lib-roblox/src/document/mod.rs @@ -241,7 +241,7 @@ impl Document { let mut dom = WeakDom::new(DomInstanceBuilder::new("ROOT")); for data_model_child in i.get_children() { - data_model_child.into_external_dom(&mut dom); + data_model_child.clone_into_external_dom(&mut dom); } Ok(Self { @@ -266,7 +266,7 @@ impl Document { let mut dom = WeakDom::new(DomInstanceBuilder::new("ROOT")); for instance in v { - instance.into_external_dom(&mut dom); + instance.clone_into_external_dom(&mut dom); } Ok(Self { diff --git a/packages/lib-roblox/src/instance/mod.rs b/packages/lib-roblox/src/instance/mod.rs index dd30519..b05867a 100644 --- a/packages/lib-roblox/src/instance/mod.rs +++ b/packages/lib-roblox/src/instance/mod.rs @@ -99,17 +99,19 @@ impl Instance { } /** - Transfers an instance to an external weak dom. + Clones an instance to an external weak dom. This will place the instance as a child of the root of the weak dom, and return its referent. */ - pub fn into_external_dom(self, external_dom: &mut WeakDom) -> DomRef { + pub fn clone_into_external_dom(self, external_dom: &mut WeakDom) -> DomRef { + let cloned = self.clone_instance(); + let mut dom = INTERNAL_DOM .try_write() .expect("Failed to get write access to document"); - let internal_dom_ref = self.dom_ref; + let internal_dom_ref = cloned.dom_ref; let external_root_ref = external_dom.root_ref(); dom.transfer(internal_dom_ref, external_dom, external_root_ref); @@ -589,12 +591,7 @@ impl Instance { datatype_table.set( "new", lua.create_function(|lua, class_name: String| { - if class_name == data_model::CLASS_NAME { - Err(LuaError::RuntimeError(format!( - "Failed to create Instance - '{}' class is restricted", - class_name - ))) - } else if class_exists(&class_name) { + if class_exists(&class_name) { Instance::new_orphaned(class_name).to_lua(lua) } else { Err(LuaError::RuntimeError(format!( diff --git a/tests/roblox/files/readModelFile.luau b/tests/roblox/files/readModelFile.luau new file mode 100644 index 0000000..128059a --- /dev/null +++ b/tests/roblox/files/readModelFile.luau @@ -0,0 +1,20 @@ +local fs = require("@lune/fs") :: any +local roblox = require("@lune/roblox") :: any + +local modelDirs = {} +for _, dirName in fs.readDir("tests/roblox/rbx-test-files/places") do + table.insert(modelDirs, "tests/roblox/rbx-test-files/places/" .. dirName) +end + +for _, modelDir in modelDirs do + local modelBinary = roblox.readModelFile(modelDir .. "/binary.rbxl") + local modelXml = roblox.readModelFile(modelDir .. "/xml.rbxlx") + + for _, modelInstance in modelBinary do + assert(modelInstance:IsA("Instance")) + end + + for _, modelInstance in modelXml do + assert(modelInstance:IsA("Instance")) + end +end diff --git a/tests/roblox/files/readPlaceFile.luau b/tests/roblox/files/readPlaceFile.luau new file mode 100644 index 0000000..7f70c4c --- /dev/null +++ b/tests/roblox/files/readPlaceFile.luau @@ -0,0 +1,18 @@ +local fs = require("@lune/fs") :: any +local roblox = require("@lune/roblox") :: any + +local placeDirs = {} +for _, dirName in fs.readDir("tests/roblox/rbx-test-files/places") do + table.insert(placeDirs, "tests/roblox/rbx-test-files/places/" .. dirName) +end + +for _, placeDir in placeDirs do + local placeBinary = roblox.readPlaceFile(placeDir .. "/binary.rbxl") + local placeXml = roblox.readPlaceFile(placeDir .. "/xml.rbxlx") + + assert(placeBinary.ClassName == "DataModel") + assert(placeXml.ClassName == "DataModel") + + assert(placeBinary:IsA("ServiceProvider")) + assert(placeXml:IsA("ServiceProvider")) +end diff --git a/tests/roblox/files/writeModelFile.luau b/tests/roblox/files/writeModelFile.luau new file mode 100644 index 0000000..2e35bfe --- /dev/null +++ b/tests/roblox/files/writeModelFile.luau @@ -0,0 +1,10 @@ +local roblox = require("@lune/roblox") :: any +local Instance = roblox.Instance + +local instances = { + Instance.new("Model"), + Instance.new("Part"), +} + +roblox.writeModelFile("bin/temp-model.rbxm", instances) +roblox.writeModelFile("bin/temp-model.rbxmx", instances) diff --git a/tests/roblox/files/writePlaceFile.luau b/tests/roblox/files/writePlaceFile.luau new file mode 100644 index 0000000..6e3d207 --- /dev/null +++ b/tests/roblox/files/writePlaceFile.luau @@ -0,0 +1,15 @@ +local roblox = require("@lune/roblox") :: any +local Instance = roblox.Instance + +local game = Instance.new("DataModel") + +local workspace = game:GetService("Workspace") + +local model = Instance.new("Model") +local part = Instance.new("Part") + +part.Parent = model +model.Parent = workspace + +roblox.writePlaceFile("bin/temp-place.rbxl", game) +roblox.writePlaceFile("bin/temp-place.rbxlx", game)