mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-14 12:10:30 +00:00
Add a couple of tests for file autocompletion
This commit is contained in:
parent
4b0988768d
commit
1acb8441ec
@ -22,13 +22,43 @@ directory exist) and one without it (meaning this entry is an immediate
|
|||||||
completion candidate). `include_dirs` can be set to @{false} to only include
|
completion candidate). `include_dirs` can be set to @{false} to only include
|
||||||
those with a trailing slash.
|
those with a trailing slash.
|
||||||
|
|
||||||
@tparam string path The path to complete.
|
@tparam[1] string path The path to complete.
|
||||||
@tparam string location The location where paths are resolved from.
|
@tparam[1] string location The location where paths are resolved from.
|
||||||
@tparam[opt] boolean include_files When @{false}, only directories will be
|
@tparam[1,opt=true] boolean include_files When @{false}, only directories will
|
||||||
included in the returned list.
|
be included in the returned list.
|
||||||
@tparam[opt] boolean include_dirs When @{false}, "raw" directories will not be
|
@tparam[1,opt=true] boolean include_dirs When @{false}, "raw" directories will
|
||||||
included in the returned list.
|
not be included in the returned list.
|
||||||
|
|
||||||
|
@tparam[2] string path The path to complete.
|
||||||
|
@tparam[2] string location The location where paths are resolved from.
|
||||||
|
@tparam[2] {
|
||||||
|
include_dirs? = boolean, include_files? = boolean,
|
||||||
|
include_hidden? = boolean
|
||||||
|
} options
|
||||||
|
This table form is an expanded version of the previous syntax. The
|
||||||
|
`include_files` and `include_dirs` arguments from above are passed in as fields.
|
||||||
|
|
||||||
|
This table also accepts the following options:
|
||||||
|
- `include_hidden`: Whether to include hidden files (those starting with `.`)
|
||||||
|
by default. They will still be shown when typing a `.`.
|
||||||
|
|
||||||
@treturn { string... } A list of possible completion candidates.
|
@treturn { string... } A list of possible completion candidates.
|
||||||
@since 1.74
|
@since 1.74
|
||||||
|
@changed 1.101.0
|
||||||
|
@usage Complete files in the root directory.
|
||||||
|
|
||||||
|
read(nil, nil, function(str)
|
||||||
|
return fs.complete(str, "", true, false)
|
||||||
|
end)
|
||||||
|
|
||||||
|
@usage Complete files in the root directory, hiding hidden files by default.
|
||||||
|
|
||||||
|
read(nil, nil, function(str)
|
||||||
|
return fs.complete(str, "", {
|
||||||
|
include_files = true,
|
||||||
|
include_dirs = false,
|
||||||
|
included_hidden = false,
|
||||||
|
})
|
||||||
|
end)
|
||||||
]]
|
]]
|
||||||
function complete(path, location, include_files, include_dirs) end
|
function complete(path, location, include_files, include_dirs) end
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
-- Ideally we'd use require, but that is part of the shell, and so is not
|
-- Ideally we'd use require, but that is part of the shell, and so is not
|
||||||
-- available to the BIOS or any APIs. All APIs load this using dofile, but that
|
-- available to the BIOS or any APIs. All APIs load this using dofile, but that
|
||||||
-- has not been defined at this point.
|
-- has not been defined at this point.
|
||||||
local expect
|
local expect, field
|
||||||
|
|
||||||
do
|
do
|
||||||
local h = fs.open("rom/modules/main/cc/expect.lua", "r")
|
local h = fs.open("rom/modules/main/cc/expect.lua", "r")
|
||||||
@ -11,7 +11,8 @@ do
|
|||||||
h.close()
|
h.close()
|
||||||
|
|
||||||
if not f then error(err) end
|
if not f then error(err) end
|
||||||
expect = f().expect
|
local res = f()
|
||||||
|
expect, field = res.expect, res.field
|
||||||
end
|
end
|
||||||
|
|
||||||
if _VERSION == "Lua 5.1" then
|
if _VERSION == "Lua 5.1" then
|
||||||
@ -718,9 +719,9 @@ function fs.complete(sPath, sLocation, bIncludeFiles, bIncludeDirs)
|
|||||||
expect(2, sLocation, "string")
|
expect(2, sLocation, "string")
|
||||||
local bIncludeHidden = nil
|
local bIncludeHidden = nil
|
||||||
if type(bIncludeFiles) == "table" then
|
if type(bIncludeFiles) == "table" then
|
||||||
bIncludeDirs = bIncludeFiles.include_dirs
|
bIncludeDirs = field(bIncludeFiles, "include_dirs", "boolean", "nil")
|
||||||
bIncludeHidden = bIncludeFiles.include_hidden
|
bIncludeHidden = field(bIncludeFiles, "include_hidden", "boolean", "nil")
|
||||||
bIncludeFiles = bIncludeFiles.include_files
|
bIncludeFiles = field(bIncludeFiles, "include_files", "boolean", "nil")
|
||||||
else
|
else
|
||||||
expect(3, bIncludeFiles, "boolean", "nil")
|
expect(3, bIncludeFiles, "boolean", "nil")
|
||||||
expect(4, bIncludeDirs, "boolean", "nil")
|
expect(4, bIncludeDirs, "boolean", "nil")
|
||||||
|
@ -10,6 +10,46 @@ describe("The fs library", function()
|
|||||||
expect.error(fs.complete, "", "", 1):eq("bad argument #3 (expected boolean, got number)")
|
expect.error(fs.complete, "", "", 1):eq("bad argument #3 (expected boolean, got number)")
|
||||||
expect.error(fs.complete, "", "", true, 1):eq("bad argument #4 (expected boolean, got number)")
|
expect.error(fs.complete, "", "", true, 1):eq("bad argument #4 (expected boolean, got number)")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("include_hidden", function()
|
||||||
|
local dir = "tmp/hidden"
|
||||||
|
local function setup_tree()
|
||||||
|
fs.delete(dir)
|
||||||
|
fs.makeDir(dir)
|
||||||
|
fs.open(dir .. "/file.txt", "w").close()
|
||||||
|
fs.open(dir .. "/.hidden.txt", "w").close()
|
||||||
|
end
|
||||||
|
|
||||||
|
it("hides hidden files", function()
|
||||||
|
setup_tree()
|
||||||
|
local opts = { include_files = true, include_dirs = false, include_hidden = false }
|
||||||
|
|
||||||
|
expect(fs.complete("", dir, opts)):same { "../", "file.txt" }
|
||||||
|
expect(fs.complete(dir .. "/", "", opts)):same { "file.txt" }
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("shows hidden files when typing a dot", function()
|
||||||
|
setup_tree()
|
||||||
|
local opts = { include_files = true, include_dirs = false, include_hidden = false }
|
||||||
|
|
||||||
|
expect(fs.complete(".", dir, opts)):same { "./", "hidden.txt" }
|
||||||
|
expect(fs.complete(dir .. "/.", "", opts)):same { "hidden.txt" }
|
||||||
|
|
||||||
|
-- Also test
|
||||||
|
expect(fs.complete(dir .. "/file", "", opts)):same { ".txt" }
|
||||||
|
expect(fs.complete(dir .. "/file.", "", opts)):same { "txt" }
|
||||||
|
expect(fs.complete("file", dir, opts)):same { ".txt" }
|
||||||
|
expect(fs.complete("file.", dir, opts)):same { "txt" }
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("shows hidden files when include_hidden is true", function()
|
||||||
|
setup_tree()
|
||||||
|
local opts = { include_files = true, include_dirs = false, include_hidden = true }
|
||||||
|
|
||||||
|
expect(fs.complete("", dir, opts)):same { "../", ".hidden.txt", "file.txt" }
|
||||||
|
expect(fs.complete(dir .. "/", "", opts)):same { ".hidden.txt", "file.txt" }
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("fs.isDriveRoot", function()
|
describe("fs.isDriveRoot", function()
|
||||||
|
Loading…
Reference in New Issue
Block a user