-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in impl for easily mocking roblox classes in lune
- Loading branch information
1 parent
5d70ace
commit c687d77
Showing
9 changed files
with
196 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
|
||
-- impl | ||
-- a wrapper for easily implementing properties and methods | ||
-- in lune to mock roblox stuffs | ||
|
||
local roblox = require("@lune/roblox") | ||
|
||
type GenericInstance<I> = roblox.Instance & I | ||
|
||
type PropertySetter<I, P> = (instance: GenericInstance<I>, new_value: unknown) -> P | ||
|
||
type ImplPrototype<I> = { | ||
rwprop: (<P>(self: Impl<I>, name: string, setter: PropertySetter<I, P>, default: P) -> Impl<I>) & | ||
(<P>(self: Impl<I>, name: string, default: P) -> Impl<I>), | ||
rprop: <P>(self: Impl<I>, name: string, default: P) -> Impl<I>, | ||
method: <A..., R...>(self: Impl<I>, name: string, f: (instance: GenericInstance<I>, A...) -> R...) -> Impl<I>, | ||
__index: ImplPrototype<I>, | ||
} | ||
|
||
export type Impl<I> = typeof(setmetatable({} :: { | ||
propstores: { [string]: { [GenericInstance<I>]: any } }, | ||
name: string, | ||
}, {} :: 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 function rwprop<I, P, D>( | ||
impl: Impl<I>, | ||
propname: string, | ||
default_or_setter: P | PropertySetter<I, P>, | ||
default: P | ||
): Impl<I> | ||
local propstores = impl.propstores | ||
local classname = impl.name | ||
local propstore = {} | ||
|
||
propstores[propname] = propstore | ||
|
||
if type(default_or_setter) == "function" and default then | ||
roblox.implementProperty( | ||
classname, | ||
propname, | ||
function(instance: any) | ||
local value = propstore[instance] | ||
|
||
if value then | ||
return value | ||
else | ||
propstore[instance] = default | ||
return default | ||
end | ||
end, | ||
function(instance: any, new_value) | ||
local new_value = default_or_setter(instance, new_value) | ||
propstore[instance] = new_value | ||
end | ||
) | ||
else | ||
local type = typeof(default_or_setter) | ||
local cannot_set_err = string.format( | ||
CANNOT_SET_ERR_FORMAT, propname, | ||
classname, type | ||
) | ||
|
||
roblox.implementProperty( | ||
classname, | ||
propname, | ||
function(instance: any) | ||
local value = propstore[instance] | ||
|
||
if value then | ||
return value | ||
else | ||
propstore[instance] = default_or_setter | ||
return default_or_setter | ||
end | ||
end, | ||
function(instance: any, new_value) | ||
if typeof(new_value) == type then | ||
propstore[instance] = new_value | ||
elseif new_value == nil then | ||
propstore[instance] = nil | ||
else | ||
error(cannot_set_err) | ||
end | ||
end | ||
) | ||
end | ||
return impl | ||
end | ||
|
||
local function rprop<I, P, D>( | ||
impl: Impl<I>, | ||
propname: string, | ||
default: P | ||
): Impl<I> | ||
local propstores = impl.propstores | ||
local classname = impl.name | ||
local propstore = {} | ||
|
||
propstores[propname] = propstore | ||
|
||
roblox.implementProperty( | ||
classname, | ||
propname, | ||
function(instance: any) | ||
local value = propstore[instance] | ||
|
||
if value then | ||
return value | ||
else | ||
propstore[instance] = default | ||
return default | ||
end | ||
end | ||
) | ||
return impl | ||
end | ||
|
||
local function method<I, P, A..., R...>( | ||
impl: Impl<I>, | ||
method_name: string, | ||
f: (instance: GenericInstance<I>, A...) -> R... | ||
): Impl<I> | ||
roblox.implementMethod(impl.name, method_name, f :: any) | ||
return impl | ||
end | ||
|
||
local impl = { | ||
method = method, | ||
rwprop = rwprop, | ||
rprop = rprop, | ||
} | ||
impl.__index = impl | ||
table.freeze(impl) | ||
|
||
local function create<I>(name: string): Impl<I> | ||
if CLASSES_IMPLEMENTED[name] then | ||
error(`[IMPL] class "{name}" has already been implemented`) | ||
end | ||
CLASSES_IMPLEMENTED[name] = true | ||
|
||
return table.freeze(setmetatable({ | ||
propstores = {}, | ||
name = name, | ||
}, impl :: any)) | ||
end | ||
|
||
return create |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
-- analytics | ||
-- implements a mock analytics service for lune | ||
|
||
local impl = require("../impl") | ||
|
||
return impl("AnalyticsService") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
-- collection | ||
-- implements a mock collection service for lune | ||
|
||
local impl = require("../impl") | ||
|
||
return impl("CollectionService") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
-- datastore | ||
-- implements a mock datastore service for lune | ||
|
||
local impl = require("../impl") | ||
|
||
return impl("DatastoreService") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
-- players | ||
-- implements a mock players service for lune | ||
|
||
local impl = require("../impl") | ||
|
||
return impl("Players") | ||
:method("GetPlayers", function(players) | ||
return players:GetChildren() | ||
end) |
Empty file.