Update completion tests

This commit is contained in:
Paul Ouellette
2019-07-20 09:20:32 -04:00
parent 8d258186c6
commit e6506bd408
2 changed files with 220 additions and 242 deletions

View File

@@ -1,288 +1,224 @@
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()
local parser = Parser "foo"
assert.equal([=[ assert.equal([=[
_foo() { _comptest() {
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"))
return 0
;;
--direction)
COMPREPLY=($(compgen -W "north south east west" -- "$cur"))
return 0
;;
esac
for arg in ${COMP_WORDS[@]:1}; do
case "$arg" in
completion)
cmd="completion"
break
;;
install|i)
cmd="install"
break
;;
admin)
cmd="admin"
break
;;
esac
done
case "$cmd" in
comptest)
COMPREPLY=($(compgen -W "help completion install i admin" -- "$cur"))
;;
completion)
opts="$opts -h --help"
;;
install)
case "$prev" in
--deps-mode)
COMPREPLY=($(compgen -W "all one order none" -- "$cur"))
return 0
;;
--pair)
COMPREPLY=($(compgen -f "$cur")) COMPREPLY=($(compgen -f "$cur"))
return 0 return 0
;; ;;
esac esac
opts="$opts -h --help --deps-mode --no-doc --pair"
;;
admin)
opts="$opts -h --help"
;;
esac
if [[ "$cur" = -* ]]; then if [[ "$cur" = -* ]]; then
COMPREPLY=($(compgen -W "$opts" -- "$cur")) COMPREPLY=($(compgen -W "$opts" -- "$cur"))
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) end)
it("generates correct completions for options with argument choices", function() it("generates correct zsh completion script", function()
local parser = Parser "foo"
:add_help(false)
parser:option "--format"
:choices {"short", "medium", "full"}
assert.equal([=[ assert.equal([=[
_foo() { compdef _comptest comptest
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 _comptest() {
--format) local context state state_descr line
COMPREPLY=($(compgen -W "short medium full" -- "$cur")) typeset -A opt_args
return 0
_arguments -s -S \
{-h,--help}"[Show this help message and exit]" \
{-f,--files}"[A description with illegal \' characters]:*: :_files" \
"--direction[The direction to go in]: :(north south east west)" \
": :_comptest_cmds" \
"*:: :->args" \
&& return 0
case $words[1] in
help)
_arguments -s -S \
{-h,--help}"[Show this help message and exit]" \
": :(help completion install i admin)" \
&& return 0
;; ;;
completion)
_arguments -s -S \
{-h,--help}"[Show this help message and exit]" \
": :(bash zsh fish)" \
&& return 0
;;
install|i)
_arguments -s -S \
{-h,--help}"[Show this help message and exit]" \
"--deps-mode: :(all one order none)" \
"--no-doc[Install without documentation]" \
"*--pair[A pair of files]: :_files" \
&& return 0
;;
admin)
_arguments -s -S \
{-h,--help}"[Show this help message and exit]" \
": :_comptest_admin_cmds" \
"*:: :->args" \
&& return 0
case $words[1] in
help)
_arguments -s -S \
{-h,--help}"[Show this help message and exit]" \
": :(help add remove)" \
&& return 0
;;
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 esac
if [[ "$cur" = -* ]]; then return 1
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi
} }
complete -F _foo -o bashdefault -o default foo _comptest_cmds() {
]=], parser:get_bash_complete()) 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) end)
it("generates correct completions for commands", function() it("generates correct fish completion script", function()
local parser = Parser "foo"
:add_help(false)
parser:command "install"
:add_help(false)
assert.equal([=[ 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 complete -c comptest -n '__fish_use_subcommand' -xa 'help' -d 'Show help for commands'
case "$arg" in complete -c comptest -n '__fish_use_subcommand' -xa 'completion' -d 'Output a shell completion script'
install) complete -c comptest -n '__fish_use_subcommand' -xa 'install' -d 'Install a rock'
cmd="install" complete -c comptest -n '__fish_use_subcommand' -xa 'i' -d 'Install a rock'
break complete -c comptest -n '__fish_use_subcommand' -xa 'admin'
;; complete -c comptest -s h -l help -d 'Show this help message and exit'
esac complete -c comptest -s f -l files -r -d 'A description with illegal \\\' characters'
done complete -c comptest -l direction -xa 'north south east west' -d 'The direction to go in'
case "$cmd" in complete -c comptest -n '__fish_seen_subcommand_from help' -xa 'help completion install i admin'
foo) complete -c comptest -n '__fish_seen_subcommand_from help' -s h -l help -d 'Show this help message and exit'
COMPREPLY=($(compgen -W "install" -- "$cur"))
;;
esac
if [[ "$cur" = -* ]]; then complete -c comptest -n '__fish_seen_subcommand_from completion' -s h -l help -d 'Show this help message and exit'
COMPREPLY=($(compgen -W "$opts" -- "$cur"))
fi
}
complete -F _foo -o bashdefault -o default foo complete -c comptest -n '__fish_seen_subcommand_from install i' -s h -l help -d 'Show this help message and exit'
]=], parser:get_bash_complete()) complete -c comptest -n '__fish_seen_subcommand_from install i' -l deps-mode -xa 'all one order none'
end) 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'
it("generates correct completions for command options", function() complete -c comptest -n '__fish_use_subcommand' -xa 'help' -d 'Show help for commands'
local parser = Parser "foo" complete -c comptest -n '__fish_use_subcommand' -xa 'add' -d 'Add a rock to a server'
:add_help(false) complete -c comptest -n '__fish_use_subcommand' -xa 'remove' -d 'Remove a rock from a server'
local install = parser:command "install" complete -c comptest -n '__fish_seen_subcommand_from admin' -s h -l help -d 'Show this help message and exit'
: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 complete -c comptest -n '__fish_seen_subcommand_from help' -xa 'help add remove'
case "$arg" in complete -c comptest -n '__fish_seen_subcommand_from help' -s h -l help -d 'Show this help message and exit'
install)
cmd="install"
break
;;
esac
done
case "$cmd" in complete -c comptest -n '__fish_seen_subcommand_from add' -s h -l help -d 'Show this help message and exit'
foo)
COMPREPLY=($(compgen -W "install" -- "$cur"))
;;
install)
opts="$opts -v --verbose"
;;
esac
if [[ "$cur" = -* ]]; then complete -c comptest -n '__fish_seen_subcommand_from remove' -s h -l help -d 'Show this help message and exit'
COMPREPLY=($(compgen -W "$opts" -- "$cur")) ]=], get_output("completion fish"))
fi
}
complete -F _foo -o bashdefault -o default foo
]=], parser:get_bash_complete())
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)
describe("fish completion scripts", function()
it("generates correct completions for help flag", function()
local parser = Parser "foo"
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()
local parser = Parser "foo"
:add_help(false)
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()
local parser = Parser "foo"
:add_help(false)
parser:option "--format"
:choices {"short", "medium", "full"}
assert.equal([[
complete -c foo -l format -xa 'short medium full'
]], parser:get_fish_complete())
end)
it("generates correct completions for commands", function()
local parser = Parser "foo"
:add_help(false)
parser:command "install"
:add_help(false)
:description "Install a rock."
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()
local parser = Parser "foo"
:add_help(false)
local install = parser:command "install"
:add_help(false)
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()
local parser = Parser "foo"
:add_help(false)
:add_help_command {add_help = false}
parser:command "install"
:add_help(false)
assert.equal([[
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()
local parser = Parser "foo"
:add_help(false)
parser:option "--bar"
:description "A description with a .period. Another sentence."
assert.equal([[
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()
local parser = Parser "foo"
:add_help(false)
parser:option "--bar"
:description "A description with illegal \\' characters."
assert.equal([[
complete -c foo -l bar -r -d 'A description with illegal \\\' characters'
]], parser:get_fish_complete())
end)
end) end)
end) end)

42
spec/comptest Executable file
View 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()