Add tests for reading & writing roblox place & model files

This commit is contained in:
Filip Tibell 2023-03-22 13:02:05 +01:00
parent 34e417e57b
commit afdde26a18
No known key found for this signature in database
6 changed files with 71 additions and 11 deletions

View file

@ -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 {

View file

@ -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!(

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)