From 37afe7b05e29e9029ea1392411ee38c4a81fca6f Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 21 Oct 2023 11:30:20 +0200 Subject: [PATCH] Add test for async custom instance methods in roblox builtin --- src/lune/builtins/roblox/mod.rs | 5 ----- tests/roblox/instance/custom/async.luau | 30 +++++++++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/lune/builtins/roblox/mod.rs b/src/lune/builtins/roblox/mod.rs index 00e69c2..1e54355 100644 --- a/src/lune/builtins/roblox/mod.rs +++ b/src/lune/builtins/roblox/mod.rs @@ -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) .into_lua_err()?; InstanceRegistry::insert_property_setter(lua, &class_name, &property_name, property_setter) @@ -143,7 +139,6 @@ fn implement_method( lua: &Lua, (class_name, method_name, method): (String, String, LuaFunction), ) -> 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()?; Ok(()) } diff --git a/tests/roblox/instance/custom/async.luau b/tests/roblox/instance/custom/async.luau index 284567f..8df4d52 100644 --- a/tests/roblox/instance/custom/async.luau +++ b/tests/roblox/instance/custom/async.luau @@ -1,12 +1,28 @@ +local net = require("@lune/net") local roblox = require("@lune/roblox") -local game = roblox.Instance.new("DataModel") -local http = game:GetService("HttpService") :: any - -roblox.implementMethod("HttpService", "GetAsync", function() - -- TODO: Fill in method body +roblox.implementMethod("HttpService", "GetAsync", function(_, url: string) + local response = net.request({ + method = "GET", + url = url, + }) + return response.body 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")