Skip to content

Commit

Permalink
Update mock roblox class implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
gaymeowing committed Aug 20, 2024
1 parent c687d77 commit 72e8b0d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
37 changes: 25 additions & 12 deletions scripts/mock/roblox/impl.luau
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

local roblox = require("@lune/roblox")

type GenericInstance<I> = roblox.Instance & I
export type GenericInstance<I> = roblox.Instance & I

type PropertySetter<I, P> = (instance: GenericInstance<I>, new_value: unknown) -> P

Expand All @@ -23,7 +23,7 @@ export type Impl<I> = typeof(setmetatable({} :: {
}, {} :: ImplPrototype<I>))

local CANNOT_SET_ERR_FORMAT = "[IMPL] cannot set property \"%s\" on class \"%s\" to a value that isn't of type \"%s?\""
local CLASSES_IMPLEMENTED = {} :: { [string]: boolean }
local IMPLS = {} :: { [string]: Impl<any> }

local function rwprop<I, P, D>(
impl: Impl<I>,
Expand Down Expand Up @@ -127,24 +127,37 @@ local function method<I, P, A..., R...>(
return impl
end

local impl = {
local impl_mt = {
method = method,
rwprop = rwprop,
rprop = rprop,
}
impl.__index = impl
table.freeze(impl)
impl_mt.__index = impl_mt
table.freeze(impl_mt)

local function set_rprop<I>(
class: string,
instance: GenericInstance<I>,
prop: string,
new_value: any
)
IMPLS[class].propstores[prop][instance] = new_value
end

local function create<I>(name: string): Impl<I>
if CLASSES_IMPLEMENTED[name] then
local function create<S, I>(self: S, name: string): Impl<I>
if IMPLS[name] then
error(`[IMPL] class "{name}" has already been implemented`)
end
CLASSES_IMPLEMENTED[name] = true

return table.freeze(setmetatable({
local impl = table.freeze(setmetatable({
propstores = {},
name = name,
}, impl :: any))
}, impl_mt :: any))

IMPLS[name] = impl
return impl
end

return create
return table.freeze(setmetatable(
{ set_rprop = set_rprop },
table.freeze({ __call = create })
))
Empty file.
8 changes: 8 additions & 0 deletions scripts/mock/roblox/instances/ds_getopts.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

-- ds_getopts
-- implements a mock datastore get options instance for lune

local impl = require("../impl")

return impl("DataStoreGetOptions")
:rwprop("UseCache", true)
20 changes: 20 additions & 0 deletions scripts/mock/roblox/instances/ds_increment_opts.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

-- ds_increment_opts
-- implements a mock datastore increment options instance for lune

local serde = require("@lune/serde")
local impl = require("../impl")

return impl("DataStoreIncrementOptions")
:rprop("Metadata", "")
:method("SetMetadata", function(instance, new_metadata)
impl.set_rprop(
"DataStoreIncrementOptions",
instance,
"Metadata",
serde.encode("json", new_metadata)
)
end)
:method("GetMetadata", function(instance)
return serde.decode("json", instance.Metadata)
end)
8 changes: 8 additions & 0 deletions scripts/mock/roblox/instances/player.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

-- player
-- implements a mock player instance for lune

local impl = require("../impl")

return impl("Player")
:rprop("UserId", 0)
28 changes: 28 additions & 0 deletions scripts/mock/roblox/services/http.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,36 @@
-- httpservice
-- implements a mock http service for lune

local serde = require("@lune/serde")
local net = require("@lune/net")
local impl = require("../impl")

return impl("HttpService")
:rwprop("HttpEnabled", false)
:method("RequestAsync", function(_)

end)
:method("PostAsync", function(_)

end)
:method("GetAsync", function(_)

end)
:method("PatchAsync", function(_)

end)
:method("PatchAsync", function(_)

end)
:method("GenerateGUID", function(_)

end)
:method("GetSecret", function()

end)
:method("JSONEncode", function(_, input: { [any]: any })
return serde.encode("json", input)
end)
:method("JSONDecode", function(_, input: string)
return serde.decode("json", input)
end)

0 comments on commit 72e8b0d

Please sign in to comment.