1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 19:50:31 +00:00

Add a couple of tests for file autocompletion

This commit is contained in:
Jonathan Coates 2022-11-01 19:22:07 +00:00
parent 4b0988768d
commit 1acb8441ec
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 82 additions and 11 deletions

View File

@ -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
those with a trailing slash.
@tparam string path The path to complete.
@tparam string location The location where paths are resolved from.
@tparam[opt] boolean include_files When @{false}, only directories will be
included in the returned list.
@tparam[opt] boolean include_dirs When @{false}, "raw" directories will not be
included in the returned list.
@tparam[1] string path The path to complete.
@tparam[1] string location The location where paths are resolved from.
@tparam[1,opt=true] boolean include_files When @{false}, only directories will
be included in the returned list.
@tparam[1,opt=true] boolean include_dirs When @{false}, "raw" directories will
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.
@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

View File

@ -3,7 +3,7 @@
-- 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
-- has not been defined at this point.
local expect
local expect, field
do
local h = fs.open("rom/modules/main/cc/expect.lua", "r")
@ -11,7 +11,8 @@ do
h.close()
if not f then error(err) end
expect = f().expect
local res = f()
expect, field = res.expect, res.field
end
if _VERSION == "Lua 5.1" then
@ -718,9 +719,9 @@ function fs.complete(sPath, sLocation, bIncludeFiles, bIncludeDirs)
expect(2, sLocation, "string")
local bIncludeHidden = nil
if type(bIncludeFiles) == "table" then
bIncludeDirs = bIncludeFiles.include_dirs
bIncludeHidden = bIncludeFiles.include_hidden
bIncludeFiles = bIncludeFiles.include_files
bIncludeDirs = field(bIncludeFiles, "include_dirs", "boolean", "nil")
bIncludeHidden = field(bIncludeFiles, "include_hidden", "boolean", "nil")
bIncludeFiles = field(bIncludeFiles, "include_files", "boolean", "nil")
else
expect(3, bIncludeFiles, "boolean", "nil")
expect(4, bIncludeDirs, "boolean", "nil")

View File

@ -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, "", "", true, 1):eq("bad argument #4 (expected boolean, got number)")
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)
describe("fs.isDriveRoot", function()