File: completion_spec.lua

package info (click to toggle)
lua-argparse 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: python: 38; makefile: 15
file content (331 lines) | stat: -rw-r--r-- 9,820 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
local script = "./spec/comptest"
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()
   it("generates correct bash completion script", function()
      assert.equal([=[
_comptest() {
    local IFS=$' \t\n'
    local args cur prev cmd opts arg
    args=("${COMP_WORDS[@]}")
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="-h --help --completion -v --verbose -f --files"

    case "$prev" in
        --completion)
            COMPREPLY=($(compgen -W "bash zsh fish" -- "$cur"))
            return 0
            ;;
        -f|--files)
            COMPREPLY=($(compgen -f -- "$cur"))
            return 0
            ;;
    esac

    args=("${args[@]:1}")
    for arg in "${args[@]}"; do
        case "$arg" in
            help)
                cmd="help"
                opts="$opts -h --help"
                break
                ;;
            completion)
                cmd="completion"
                opts="$opts -h --help"
                break
                ;;
            install|i)
                cmd="install"
                opts="$opts -h --help --deps-mode --no-doc"
                break
                ;;
            admin)
                cmd="admin"
                opts="$opts -h --help"
                args=("${args[@]:1}")
                for arg in "${args[@]}"; do
                    case "$arg" in
                        help)
                            cmd="$cmd help"
                            opts="$opts -h --help"
                            break
                            ;;
                        add)
                            cmd="$cmd add"
                            opts="$opts -h --help"
                            break
                            ;;
                        remove)
                            cmd="$cmd remove"
                            opts="$opts -h --help"
                            break
                            ;;
                    esac
                done
                break
                ;;
        esac
    done

    case "$cmd" in
        '')
            COMPREPLY=($(compgen -W "help completion install i admin" -- "$cur"))
            ;;
        'help')
            COMPREPLY=($(compgen -W "help completion install i admin" -- "$cur"))
            ;;
        'install')
            case "$prev" in
                --deps-mode)
                    COMPREPLY=($(compgen -W "all one order none" -- "$cur"))
                    return 0
                    ;;
            esac

            ;;
        'admin')
            COMPREPLY=($(compgen -W "help add remove" -- "$cur"))
            ;;
        'admin help')
            COMPREPLY=($(compgen -W "help add remove" -- "$cur"))
            ;;
    esac

    if [[ "$cur" = -* ]]; then
        COMPREPLY=($(compgen -W "$opts" -- "$cur"))
    fi
}

complete -F _comptest -o bashdefault -o default comptest
]=], get_output("completion bash"))
   end)

   it("generates correct zsh completion script", function()
      assert.equal([=[
#compdef comptest

_comptest() {
  local context state state_descr line
  typeset -A opt_args

  local -a options=(
    {-h,--help}"[Show this help message and exit]"
    "--completion[Output a shell completion script for the specified shell]: :(bash zsh fish)"
    "*"{-v,--verbose}"[Set the verbosity level]"
    {-f,--files}"[A description with illegal \"' characters]:*: :_files"
  )
  _arguments -s -S \
    $options \
    ": :_comptest_cmds" \
    "*:: :->args" \
    && return 0

  case $words[1] in
    help)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :(help completion install i admin)" \
        && return 0
      ;;

    completion)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :(bash zsh fish)" \
        && return 0
      ;;

    install|i)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
        "--deps-mode: :(all one order none)"
        "--no-doc[Install without documentation]"
      )
      _arguments -s -S \
        $options \
        && return 0
      ;;

    admin)
      options=(
        $options
        {-h,--help}"[Show this help message and exit]"
      )
      _arguments -s -S \
        $options \
        ": :_comptest_admin_cmds" \
        "*:: :->args" \
        && return 0

      case $words[1] in
        help)
          options=(
            $options
            {-h,--help}"[Show this help message and exit]"
          )
          _arguments -s -S \
            $options \
            ": :(help add remove)" \
            && return 0
          ;;

        add)
          options=(
            $options
            {-h,--help}"[Show this help message and exit]"
          )
          _arguments -s -S \
            $options \
            ": :_files" \
            && return 0
          ;;

        remove)
          options=(
            $options
            {-h,--help}"[Show this help message and exit]"
          )
          _arguments -s -S \
            $options \
            ": :_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:Rock server administration interface"
  )
  _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
}

_comptest
]=], get_output("completion zsh"))
   end)

   it("generates correct fish completion script", function()
      assert.equal([=[
function __fish_comptest_print_command
    set -l cmdline (commandline -poc)
    set -l cmd
    set -e cmdline[1]
    for arg in $cmdline
        switch $arg
            case help
                set cmd $cmd help
                break
            case completion
                set cmd $cmd completion
                break
            case install i
                set cmd $cmd install
                break
            case admin
                set cmd $cmd admin
                set -e cmdline[1]
                for arg in $cmdline
                    switch $arg
                        case help
                            set cmd $cmd help
                            break
                        case add
                            set cmd $cmd add
                            break
                        case remove
                            set cmd $cmd remove
                            break
                    end
                end
                break
        end
    end
    echo "$cmd"
end

function __fish_comptest_using_command
    test (__fish_comptest_print_command) = "$argv"
    and return 0
    or return 1
end

function __fish_comptest_seen_command
    string match -q "$argv*" (__fish_comptest_print_command)
    and return 0
    or return 1
end

complete -c comptest -n '__fish_comptest_using_command' -xa 'help' -d 'Show help for commands'
complete -c comptest -n '__fish_comptest_using_command' -xa 'completion' -d 'Output a shell completion script'
complete -c comptest -n '__fish_comptest_using_command' -xa 'install i' -d 'Install a rock'
complete -c comptest -n '__fish_comptest_using_command' -xa 'admin' -d 'Rock server administration interface'
complete -c comptest -s h -l help -d 'Show this help message and exit'
complete -c comptest -l completion -xa 'bash zsh fish' -d 'Output a shell completion script for the specified shell'
complete -c comptest -s v -l verbose -d 'Set the verbosity level'
complete -c comptest -s f -l files -r -d 'A description with illegal "\' characters'

complete -c comptest -n '__fish_comptest_using_command help' -xa 'help completion install i admin'
complete -c comptest -n '__fish_comptest_seen_command help' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_seen_command completion' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_seen_command install' -s h -l help -d 'Show this help message and exit'
complete -c comptest -n '__fish_comptest_seen_command install' -l deps-mode -xa 'all one order none'
complete -c comptest -n '__fish_comptest_seen_command install' -l no-doc -d 'Install without documentation'

complete -c comptest -n '__fish_comptest_using_command admin' -xa 'help' -d 'Show help for commands'
complete -c comptest -n '__fish_comptest_using_command admin' -xa 'add' -d 'Add a rock to a server'
complete -c comptest -n '__fish_comptest_using_command admin' -xa 'remove' -d 'Remove a rock from  a server'
complete -c comptest -n '__fish_comptest_seen_command admin' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_using_command admin help' -xa 'help add remove'
complete -c comptest -n '__fish_comptest_seen_command admin help' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_seen_command admin add' -s h -l help -d 'Show this help message and exit'

complete -c comptest -n '__fish_comptest_seen_command admin remove' -s h -l help -d 'Show this help message and exit'
]=], get_output("completion fish"))
   end)
end)