-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a new function pair for saving and loading paths
- Loading branch information
Showing
8 changed files
with
150 additions
and
107 deletions.
There are no files selected for viewing
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,44 @@ | ||
--[[ | ||
Gets an instance based off the result of getInstancePath. | ||
The reason we use this over GetFullName is because it uses a dot (.) as the | ||
path separator which makes it difficult to disambiguate instances stories | ||
and test files (Foo.story and Foo.spec, respectively) | ||
Returns `nil` if the instance is outside the DataModel or otherwise cannot | ||
be found. | ||
]] | ||
|
||
local tryGetService = require("@root/RobloxInternal/tryGetService") | ||
|
||
local PATH_SEPERATOR = "/" | ||
|
||
local function getInstanceFromPath(path: string): Instance? | ||
local parts = path:split(PATH_SEPERATOR) | ||
local serviceName = parts[1] | ||
|
||
if serviceName then | ||
-- This function only works for instances in the DataModel. As such, the | ||
-- first part of the path will always be a service, so if we can't find | ||
-- one we exit out and return nil | ||
local current = tryGetService(serviceName) | ||
|
||
if current then | ||
for i = 2, #parts do | ||
local found = current:FindFirstChild(parts[i]) | ||
|
||
if found then | ||
current = found | ||
else | ||
return nil | ||
end | ||
end | ||
|
||
return current | ||
end | ||
end | ||
|
||
return nil | ||
end | ||
|
||
return getInstanceFromPath |
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,18 @@ | ||
local PATH_SEPARATOR = "/" | ||
|
||
local function getInstancePath(instance: Instance, pathSeparator: string?): string | ||
pathSeparator = if pathSeparator then pathSeparator else PATH_SEPARATOR | ||
assert(pathSeparator, "Luau") | ||
|
||
local path = {} | ||
local current = instance | ||
|
||
while current and current.Parent ~= nil do | ||
table.insert(path, 1, current.Name) | ||
current = current.Parent | ||
end | ||
|
||
return table.concat(path, "/") | ||
end | ||
|
||
return getInstancePath |
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,48 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
|
||
local JestGlobals = require("@pkg/JestGlobals") | ||
local newFolder = require("@root/Testing/newFolder") | ||
|
||
local getInstancePath = require("./getInstancePath") | ||
|
||
local expect = JestGlobals.expect | ||
local test = JestGlobals.test | ||
local afterEach = JestGlobals.afterEach | ||
|
||
local folder: Folder | ||
|
||
afterEach(function() | ||
if folder then | ||
folder:Destroy() | ||
end | ||
end) | ||
|
||
test("services are treated as the root", function() | ||
expect(getInstancePath(ReplicatedStorage)).toBe("ReplicatedStorage") | ||
end) | ||
|
||
test("works on nested instances", function() | ||
local module = Instance.new("ModuleScript") | ||
|
||
folder = newFolder({ | ||
foo = newFolder({ | ||
bar = module, | ||
}), | ||
}) | ||
folder.Parent = ReplicatedStorage | ||
|
||
expect(getInstancePath(module)).toBe("ReplicatedStorage/Folder/foo/bar") | ||
end) | ||
|
||
test("works with spec files", function() | ||
local module = Instance.new("ModuleScript") | ||
|
||
folder = newFolder({ | ||
foo = newFolder({ | ||
["bar.spec"] = module, | ||
}), | ||
}) | ||
folder.Parent = ReplicatedStorage | ||
|
||
expect(getInstancePath(module)).toBe("ReplicatedStorage/Folder/foo/bar.spec") | ||
end) |
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
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