-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f85a7c
commit 3c88ae9
Showing
6 changed files
with
93 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
local Path = require("worldeditadditions_core.utils.path") | ||
|
||
describe("Path.join", function() | ||
it("should correct bad formatting", function() | ||
local result, err = Path.join("C:\\Users\\me\\", "/Documents//code.lua") | ||
assert.is_nil(err) | ||
assert.are.same( | ||
table.concat({"C:","Users","me","Documents","code.lua"}, Path.sep), | ||
result | ||
) | ||
end) | ||
it("should return an error if not a string", function() | ||
local result, err = Path.join("C://Users/me", 123) | ||
assert.is_false(result) | ||
assert.are.same("string", type(err)) | ||
end) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
local Path = require("worldeditadditions_core.utils.path") | ||
|
||
describe("Path.new", function() | ||
it("should correct bad formatting", function() | ||
local result, err = Path.new("C:\\Users\\me\\".."/Documents//code.lua") | ||
assert.is_nil(err) | ||
assert.are.same( | ||
table.concat({"C:","Users","me","Documents","code.lua"}, Path.sep), | ||
result | ||
) | ||
end) | ||
it("should return an error if not a string", function() | ||
local result, err = Path.new(123) | ||
assert.is_false(result) | ||
assert.are.same("string", type(err)) | ||
end) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- A path manipulation class. | ||
-- @class worldeditadditions_core.Path | ||
local path = {} | ||
|
||
-- Helper functions | ||
local check = function( ... ) | ||
for _, v in ipairs( ... ) do | ||
if type(v) ~= "string" then | ||
return false, v .. " is not a string." | ||
end | ||
end | ||
return true | ||
end | ||
|
||
--- The directory separator on the current host system | ||
-- @value string | ||
path.sep = "/" | ||
if package and package.config then | ||
path.sep = package.config:sub(1,1) | ||
end | ||
|
||
--- Format string into path | ||
-- @param str string The string to process | ||
-- @return string|false, string? The formatted path string or | ||
-- false and an error message. | ||
-- @example Basic usage | ||
-- local path = path.new("C:\\Users\\me\\".."/Documents//code.lua") | ||
path.new = function( str ) | ||
local ok, err = check(str) | ||
if not ok then return false, err end | ||
return ({str:gsub("[/\\]+", path.sep)})[1] | ||
end | ||
|
||
--- Joins the given path segments into a single path with dirsep. | ||
-- @param ... string The path fragments to process and join. | ||
-- @return string|false, string? The joined path or | ||
-- false and an error message. | ||
-- @example Basic usage | ||
-- local path = file_path("C:\\Users", "me", "/Documents/code.lua") | ||
path.join = function( ... ) | ||
local pathlets = { ... } | ||
local ok, err = check(pathlets) | ||
if not ok then return false, err end | ||
return path.new(table.concat(pathlets, path.sep)) | ||
end | ||
|
||
local Path = {} | ||
Path.__name = "Path" -- A hack to allow easier identification | ||
Path.__index = Path | ||
Path.__call = function(self, ...) | ||
return self.join(...) | ||
end | ||
|
||
return setmetatable(path, Path) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.