mirror of
https://github.com/TangentFoxy/argparse.git
synced 2025-07-28 11:02:20 +00:00
Update completion tests
This commit is contained in:
@@ -1,155 +1,81 @@
|
|||||||
local Parser = require "argparse"
|
local script = "./spec/comptest"
|
||||||
getmetatable(Parser()).error = function(_, msg) error(msg) end
|
local script_cmd = "lua"
|
||||||
|
|
||||||
|
if package.loaded["luacov.runner"] then
|
||||||
|
script_cmd = script_cmd .. " -lluacov"
|
||||||
|
end
|
||||||
|
|
||||||
|
script_cmd = script_cmd .. " " .. script
|
||||||
|
|
||||||
|
local function get_output(args)
|
||||||
|
local handler = io.popen(script_cmd .. " " .. args .. " 2>&1", "r")
|
||||||
|
local output = handler:read("*a")
|
||||||
|
handler:close()
|
||||||
|
return output
|
||||||
|
end
|
||||||
|
|
||||||
describe("tests related to generation of shell completion scripts", function()
|
describe("tests related to generation of shell completion scripts", function()
|
||||||
describe("bash completion scripts", function()
|
it("generates correct bash completion script", function()
|
||||||
it("generates correct completions for help flag", function()
|
assert.equal([=[
|
||||||
local parser = Parser "foo"
|
_comptest() {
|
||||||
assert.equal([=[
|
|
||||||
_foo() {
|
|
||||||
local IFS=$' \t\n'
|
local IFS=$' \t\n'
|
||||||
local cur prev cmd opts arg
|
local cur prev cmd opts arg
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||||
cmd="foo"
|
cmd="comptest"
|
||||||
opts="-h --help"
|
opts="-h --help -f --files --direction"
|
||||||
|
|
||||||
if [[ "$cur" = -* ]]; then
|
|
||||||
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
complete -F _foo -o bashdefault -o default foo
|
|
||||||
]=], parser:get_bash_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for options with required argument", function()
|
|
||||||
local parser = Parser "foo"
|
|
||||||
:add_help(false)
|
|
||||||
parser:option "--bar"
|
|
||||||
assert.equal([=[
|
|
||||||
_foo() {
|
|
||||||
local IFS=$' \t\n'
|
|
||||||
local cur prev cmd opts arg
|
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
||||||
cmd="foo"
|
|
||||||
opts="--bar"
|
|
||||||
|
|
||||||
case "$prev" in
|
case "$prev" in
|
||||||
--bar)
|
-f|--files)
|
||||||
COMPREPLY=($(compgen -f "$cur"))
|
COMPREPLY=($(compgen -f "$cur"))
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
esac
|
--direction)
|
||||||
|
COMPREPLY=($(compgen -W "north south east west" -- "$cur"))
|
||||||
if [[ "$cur" = -* ]]; then
|
|
||||||
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
complete -F _foo -o bashdefault -o default foo
|
|
||||||
]=], parser:get_bash_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for options with argument choices", function()
|
|
||||||
local parser = Parser "foo"
|
|
||||||
:add_help(false)
|
|
||||||
parser:option "--format"
|
|
||||||
:choices {"short", "medium", "full"}
|
|
||||||
assert.equal([=[
|
|
||||||
_foo() {
|
|
||||||
local IFS=$' \t\n'
|
|
||||||
local cur prev cmd opts arg
|
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
||||||
cmd="foo"
|
|
||||||
opts="--format"
|
|
||||||
|
|
||||||
case "$prev" in
|
|
||||||
--format)
|
|
||||||
COMPREPLY=($(compgen -W "short medium full" -- "$cur"))
|
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [[ "$cur" = -* ]]; then
|
|
||||||
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
complete -F _foo -o bashdefault -o default foo
|
|
||||||
]=], parser:get_bash_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for commands", function()
|
|
||||||
local parser = Parser "foo"
|
|
||||||
:add_help(false)
|
|
||||||
parser:command "install"
|
|
||||||
:add_help(false)
|
|
||||||
assert.equal([=[
|
|
||||||
_foo() {
|
|
||||||
local IFS=$' \t\n'
|
|
||||||
local cur prev cmd opts arg
|
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
||||||
cmd="foo"
|
|
||||||
opts=""
|
|
||||||
|
|
||||||
for arg in ${COMP_WORDS[@]:1}; do
|
for arg in ${COMP_WORDS[@]:1}; do
|
||||||
case "$arg" in
|
case "$arg" in
|
||||||
install)
|
completion)
|
||||||
|
cmd="completion"
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
install|i)
|
||||||
cmd="install"
|
cmd="install"
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
|
admin)
|
||||||
|
cmd="admin"
|
||||||
|
break
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
case "$cmd" in
|
case "$cmd" in
|
||||||
foo)
|
comptest)
|
||||||
COMPREPLY=($(compgen -W "install" -- "$cur"))
|
COMPREPLY=($(compgen -W "help completion install i admin" -- "$cur"))
|
||||||
;;
|
;;
|
||||||
esac
|
completion)
|
||||||
|
opts="$opts -h --help"
|
||||||
if [[ "$cur" = -* ]]; then
|
|
||||||
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
complete -F _foo -o bashdefault -o default foo
|
|
||||||
]=], parser:get_bash_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for command options", function()
|
|
||||||
local parser = Parser "foo"
|
|
||||||
:add_help(false)
|
|
||||||
local install = parser:command "install"
|
|
||||||
:add_help(false)
|
|
||||||
install:flag "-v --verbose"
|
|
||||||
assert.equal([=[
|
|
||||||
_foo() {
|
|
||||||
local IFS=$' \t\n'
|
|
||||||
local cur prev cmd opts arg
|
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
||||||
cmd="foo"
|
|
||||||
opts=""
|
|
||||||
|
|
||||||
for arg in ${COMP_WORDS[@]:1}; do
|
|
||||||
case "$arg" in
|
|
||||||
install)
|
|
||||||
cmd="install"
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
case "$cmd" in
|
|
||||||
foo)
|
|
||||||
COMPREPLY=($(compgen -W "install" -- "$cur"))
|
|
||||||
;;
|
;;
|
||||||
install)
|
install)
|
||||||
opts="$opts -v --verbose"
|
case "$prev" in
|
||||||
|
--deps-mode)
|
||||||
|
COMPREPLY=($(compgen -W "all one order none" -- "$cur"))
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
--pair)
|
||||||
|
COMPREPLY=($(compgen -f "$cur"))
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
opts="$opts -h --help --deps-mode --no-doc --pair"
|
||||||
|
;;
|
||||||
|
admin)
|
||||||
|
opts="$opts -h --help"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
@@ -158,131 +84,141 @@ _foo() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
complete -F _foo -o bashdefault -o default foo
|
complete -F _comptest -o bashdefault -o default comptest
|
||||||
]=], parser:get_bash_complete())
|
]=], get_output("completion bash"))
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates completions for help command argument", function()
|
|
||||||
local parser = Parser "foo"
|
|
||||||
:add_help(false)
|
|
||||||
:add_help_command {add_help = false}
|
|
||||||
parser:command "install"
|
|
||||||
:add_help(false)
|
|
||||||
assert.equal([=[
|
|
||||||
_foo() {
|
|
||||||
local IFS=$' \t\n'
|
|
||||||
local cur prev cmd opts arg
|
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
||||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
||||||
cmd="foo"
|
|
||||||
opts=""
|
|
||||||
|
|
||||||
for arg in ${COMP_WORDS[@]:1}; do
|
|
||||||
case "$arg" in
|
|
||||||
install)
|
|
||||||
cmd="install"
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
case "$cmd" in
|
|
||||||
foo)
|
|
||||||
COMPREPLY=($(compgen -W "help install" -- "$cur"))
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
if [[ "$cur" = -* ]]; then
|
|
||||||
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
complete -F _foo -o bashdefault -o default foo
|
|
||||||
]=], parser:get_bash_complete())
|
|
||||||
end)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("fish completion scripts", function()
|
it("generates correct zsh completion script", function()
|
||||||
it("generates correct completions for help flag", function()
|
assert.equal([=[
|
||||||
local parser = Parser "foo"
|
compdef _comptest comptest
|
||||||
assert.equal([[
|
|
||||||
complete -c foo -s h -l help -d 'Show this help message and exit'
|
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for options with required argument", function()
|
_comptest() {
|
||||||
local parser = Parser "foo"
|
local context state state_descr line
|
||||||
:add_help(false)
|
typeset -A opt_args
|
||||||
parser:option "--bar"
|
|
||||||
assert.equal([[
|
|
||||||
complete -c foo -l bar -r
|
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for options with argument choices", function()
|
_arguments -s -S \
|
||||||
local parser = Parser "foo"
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
:add_help(false)
|
{-f,--files}"[A description with illegal \' characters]:*: :_files" \
|
||||||
parser:option "--format"
|
"--direction[The direction to go in]: :(north south east west)" \
|
||||||
:choices {"short", "medium", "full"}
|
": :_comptest_cmds" \
|
||||||
assert.equal([[
|
"*:: :->args" \
|
||||||
complete -c foo -l format -xa 'short medium full'
|
&& return 0
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for commands", function()
|
case $words[1] in
|
||||||
local parser = Parser "foo"
|
help)
|
||||||
:add_help(false)
|
_arguments -s -S \
|
||||||
parser:command "install"
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
:add_help(false)
|
": :(help completion install i admin)" \
|
||||||
:description "Install a rock."
|
&& return 0
|
||||||
assert.equal([[
|
;;
|
||||||
complete -c foo -n '__fish_use_subcommand' -xa 'install' -d 'Install a rock'
|
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates correct completions for command options", function()
|
completion)
|
||||||
local parser = Parser "foo"
|
_arguments -s -S \
|
||||||
:add_help(false)
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
local install = parser:command "install"
|
": :(bash zsh fish)" \
|
||||||
:add_help(false)
|
&& return 0
|
||||||
install:flag "-v --verbose"
|
;;
|
||||||
assert.equal([[
|
|
||||||
complete -c foo -n '__fish_use_subcommand' -xa 'install'
|
|
||||||
complete -c foo -n '__fish_seen_subcommand_from install' -s v -l verbose
|
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("generates completions for help command argument", function()
|
install|i)
|
||||||
local parser = Parser "foo"
|
_arguments -s -S \
|
||||||
:add_help(false)
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
:add_help_command {add_help = false}
|
"--deps-mode: :(all one order none)" \
|
||||||
parser:command "install"
|
"--no-doc[Install without documentation]" \
|
||||||
:add_help(false)
|
"*--pair[A pair of files]: :_files" \
|
||||||
assert.equal([[
|
&& return 0
|
||||||
complete -c foo -n '__fish_use_subcommand' -xa 'help' -d 'Show help for commands'
|
;;
|
||||||
complete -c foo -n '__fish_seen_subcommand_from help' -xa 'help install'
|
|
||||||
complete -c foo -n '__fish_use_subcommand' -xa 'install'
|
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("uses fist sentence of descriptions", function()
|
admin)
|
||||||
local parser = Parser "foo"
|
_arguments -s -S \
|
||||||
:add_help(false)
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
parser:option "--bar"
|
": :_comptest_admin_cmds" \
|
||||||
:description "A description with a .period. Another sentence."
|
"*:: :->args" \
|
||||||
assert.equal([[
|
&& return 0
|
||||||
complete -c foo -l bar -r -d 'A description with a .period'
|
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
|
||||||
|
|
||||||
it("escapes backslashes and single quotes in descriptions", function()
|
case $words[1] in
|
||||||
local parser = Parser "foo"
|
help)
|
||||||
:add_help(false)
|
_arguments -s -S \
|
||||||
parser:option "--bar"
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
:description "A description with illegal \\' characters."
|
": :(help add remove)" \
|
||||||
assert.equal([[
|
&& return 0
|
||||||
complete -c foo -l bar -r -d 'A description with illegal \\\' characters'
|
;;
|
||||||
]], parser:get_fish_complete())
|
|
||||||
end)
|
add)
|
||||||
|
_arguments -s -S \
|
||||||
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
|
": :_files" \
|
||||||
|
&& return 0
|
||||||
|
;;
|
||||||
|
|
||||||
|
remove)
|
||||||
|
_arguments -s -S \
|
||||||
|
{-h,--help}"[Show this help message and exit]" \
|
||||||
|
": :_files" \
|
||||||
|
&& return 0
|
||||||
|
;;
|
||||||
|
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
|
||||||
|
esac
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
_comptest_cmds() {
|
||||||
|
local -a commands=(
|
||||||
|
"help:Show help for commands"
|
||||||
|
"completion:Output a shell completion script"
|
||||||
|
{install,i}":Install a rock"
|
||||||
|
"admin"
|
||||||
|
)
|
||||||
|
_describe "command" commands
|
||||||
|
}
|
||||||
|
|
||||||
|
_comptest_admin_cmds() {
|
||||||
|
local -a commands=(
|
||||||
|
"help:Show help for commands"
|
||||||
|
"add:Add a rock to a server"
|
||||||
|
"remove:Remove a rock from a server"
|
||||||
|
)
|
||||||
|
_describe "command" commands
|
||||||
|
}
|
||||||
|
]=], get_output("completion zsh"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("generates correct fish completion script", function()
|
||||||
|
assert.equal([=[
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'help' -d 'Show help for commands'
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'completion' -d 'Output a shell completion script'
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'install' -d 'Install a rock'
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'i' -d 'Install a rock'
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'admin'
|
||||||
|
complete -c comptest -s h -l help -d 'Show this help message and exit'
|
||||||
|
complete -c comptest -s f -l files -r -d 'A description with illegal \\\' characters'
|
||||||
|
complete -c comptest -l direction -xa 'north south east west' -d 'The direction to go in'
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from help' -xa 'help completion install i admin'
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from help' -s h -l help -d 'Show this help message and exit'
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from completion' -s h -l help -d 'Show this help message and exit'
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from install i' -s h -l help -d 'Show this help message and exit'
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from install i' -l deps-mode -xa 'all one order none'
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from install i' -l no-doc -d 'Install without documentation'
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from install i' -l pair -r -d 'A pair of files'
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'help' -d 'Show help for commands'
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'add' -d 'Add a rock to a server'
|
||||||
|
complete -c comptest -n '__fish_use_subcommand' -xa 'remove' -d 'Remove a rock from a server'
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from admin' -s h -l help -d 'Show this help message and exit'
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from help' -xa 'help add remove'
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from help' -s h -l help -d 'Show this help message and exit'
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from add' -s h -l help -d 'Show this help message and exit'
|
||||||
|
|
||||||
|
complete -c comptest -n '__fish_seen_subcommand_from remove' -s h -l help -d 'Show this help message and exit'
|
||||||
|
]=], get_output("completion fish"))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
42
spec/comptest
Executable file
42
spec/comptest
Executable file
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env lua
|
||||||
|
|
||||||
|
local argparse = require "argparse"
|
||||||
|
|
||||||
|
local parser = argparse "comptest"
|
||||||
|
:add_help_command()
|
||||||
|
:add_complete_command()
|
||||||
|
|
||||||
|
parser:option "-f --files"
|
||||||
|
:description "A description with illegal \\' characters."
|
||||||
|
:args "+"
|
||||||
|
|
||||||
|
parser:option "--direction"
|
||||||
|
:description "The direction to go in."
|
||||||
|
:choices {"north", "south", "east", "west"}
|
||||||
|
|
||||||
|
local install = parser:command "install i"
|
||||||
|
:description "Install a rock."
|
||||||
|
|
||||||
|
install:option "--deps-mode"
|
||||||
|
:choices {"all", "one", "order", "none"}
|
||||||
|
|
||||||
|
install:flag "--no-doc"
|
||||||
|
:description "Install without documentation."
|
||||||
|
|
||||||
|
install:option "--pair"
|
||||||
|
:description "A pair of files."
|
||||||
|
:args "2"
|
||||||
|
:count "*"
|
||||||
|
|
||||||
|
local admin = parser:command "admin"
|
||||||
|
:add_help_command()
|
||||||
|
|
||||||
|
local admin_add = admin:command "add"
|
||||||
|
:description "Add a rock to a server."
|
||||||
|
admin_add:argument "rock"
|
||||||
|
|
||||||
|
local admin_remove = admin:command "remove"
|
||||||
|
:description "Remove a rock from a server."
|
||||||
|
admin_remove:argument "rock"
|
||||||
|
|
||||||
|
parser:parse()
|
Reference in New Issue
Block a user