From 72e8b0d949058b375749646f1ff5e726b13d0f54 Mon Sep 17 00:00:00 2001 From: kalrnlo Date: Tue, 20 Aug 2024 16:04:50 -0400 Subject: [PATCH] Update mock roblox class implementations --- scripts/mock/roblox/impl.luau | 37 +++++++++++++------ scripts/mock/roblox/instances/.gitkeep | 0 scripts/mock/roblox/instances/ds_getopts.luau | 8 ++++ .../roblox/instances/ds_increment_opts.luau | 20 ++++++++++ scripts/mock/roblox/instances/player.luau | 8 ++++ scripts/mock/roblox/services/http.luau | 28 ++++++++++++++ 6 files changed, 89 insertions(+), 12 deletions(-) delete mode 100644 scripts/mock/roblox/instances/.gitkeep create mode 100644 scripts/mock/roblox/instances/ds_getopts.luau create mode 100644 scripts/mock/roblox/instances/ds_increment_opts.luau create mode 100644 scripts/mock/roblox/instances/player.luau diff --git a/scripts/mock/roblox/impl.luau b/scripts/mock/roblox/impl.luau index 009125a..8f7c149 100644 --- a/scripts/mock/roblox/impl.luau +++ b/scripts/mock/roblox/impl.luau @@ -5,7 +5,7 @@ local roblox = require("@lune/roblox") -type GenericInstance = roblox.Instance & I +export type GenericInstance = roblox.Instance & I type PropertySetter = (instance: GenericInstance, new_value: unknown) -> P @@ -23,7 +23,7 @@ export type Impl = typeof(setmetatable({} :: { }, {} :: ImplPrototype)) 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 } local function rwprop( impl: Impl, @@ -127,24 +127,37 @@ local function method( 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( + class: string, + instance: GenericInstance, + prop: string, + new_value: any +) + IMPLS[class].propstores[prop][instance] = new_value +end -local function create(name: string): Impl - if CLASSES_IMPLEMENTED[name] then +local function create(self: S, name: string): Impl + 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 }) +)) diff --git a/scripts/mock/roblox/instances/.gitkeep b/scripts/mock/roblox/instances/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/scripts/mock/roblox/instances/ds_getopts.luau b/scripts/mock/roblox/instances/ds_getopts.luau new file mode 100644 index 0000000..132d15f --- /dev/null +++ b/scripts/mock/roblox/instances/ds_getopts.luau @@ -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) \ No newline at end of file diff --git a/scripts/mock/roblox/instances/ds_increment_opts.luau b/scripts/mock/roblox/instances/ds_increment_opts.luau new file mode 100644 index 0000000..47aa077 --- /dev/null +++ b/scripts/mock/roblox/instances/ds_increment_opts.luau @@ -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) diff --git a/scripts/mock/roblox/instances/player.luau b/scripts/mock/roblox/instances/player.luau new file mode 100644 index 0000000..a2a39cf --- /dev/null +++ b/scripts/mock/roblox/instances/player.luau @@ -0,0 +1,8 @@ + +-- player +-- implements a mock player instance for lune + +local impl = require("../impl") + +return impl("Player") + :rprop("UserId", 0) \ No newline at end of file diff --git a/scripts/mock/roblox/services/http.luau b/scripts/mock/roblox/services/http.luau index 25ce4ca..dbcd9a1 100644 --- a/scripts/mock/roblox/services/http.luau +++ b/scripts/mock/roblox/services/http.luau @@ -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)