Add test for async custom instance methods in roblox builtin

This commit is contained in:
Filip Tibell 2023-10-21 11:30:20 +02:00
parent ec6060a6cb
commit 37afe7b05e
No known key found for this signature in database
2 changed files with 23 additions and 12 deletions

View file

@ -128,10 +128,6 @@ fn implement_property(
})? })?
} }
}; };
// TODO: Wrap getter and setter functions in async compat layers,
// the roblox library does not know about the Lune runtime or the
// scheduler and users may want to call async functions, some of
// which are not obvious that they are async such as print, warn, ...
InstanceRegistry::insert_property_getter(lua, &class_name, &property_name, property_getter) InstanceRegistry::insert_property_getter(lua, &class_name, &property_name, property_getter)
.into_lua_err()?; .into_lua_err()?;
InstanceRegistry::insert_property_setter(lua, &class_name, &property_name, property_setter) InstanceRegistry::insert_property_setter(lua, &class_name, &property_name, property_setter)
@ -143,7 +139,6 @@ fn implement_method(
lua: &Lua, lua: &Lua,
(class_name, method_name, method): (String, String, LuaFunction), (class_name, method_name, method): (String, String, LuaFunction),
) -> LuaResult<()> { ) -> LuaResult<()> {
// TODO: Same as above, wrap the provided method in an async compat layer
InstanceRegistry::insert_method(lua, &class_name, &method_name, method).into_lua_err()?; InstanceRegistry::insert_method(lua, &class_name, &method_name, method).into_lua_err()?;
Ok(()) Ok(())
} }

View file

@ -1,12 +1,28 @@
local net = require("@lune/net")
local roblox = require("@lune/roblox") local roblox = require("@lune/roblox")
local game = roblox.Instance.new("DataModel") roblox.implementMethod("HttpService", "GetAsync", function(_, url: string)
local http = game:GetService("HttpService") :: any local response = net.request({
method = "GET",
roblox.implementMethod("HttpService", "GetAsync", function() url = url,
-- TODO: Fill in method body })
return response.body
end) end)
-- TODO: Fill in rest of test cases here roblox.implementMethod("HttpService", "JSONDecode", function(_, value)
return net.jsonDecode(value)
end)
http:GetAsync() -- Reference: https://create.roblox.com/docs/reference/engine/classes/HttpService#GetAsync
local URL_ASTROS = "http://api.open-notify.org/astros.json"
local game = roblox.Instance.new("DataModel")
local HttpService = game:GetService("HttpService") :: any
local response = HttpService:GetAsync(URL_ASTROS)
local data = HttpService:JSONDecode(response)
assert(type(data) == "table", "Returned JSON data should decode to a table")
assert(data.message == "success", "Returned JSON data should have a 'message' with value 'success'")
assert(type(data.people) == "table", "Returned JSON data should have a 'people' table")