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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import XCTest
import ArgumentParser
import ArgumentParserTestHelpers
final class MathExampleTests: XCTestCase {
func testMath_Simple() throws {
try AssertExecuteCommand(command: "math 1 2 3 4 5", expected: "15")
try AssertExecuteCommand(command: "math multiply 1 2 3 4 5", expected: "120")
}
func testMath_Help() throws {
let helpText = """
OVERVIEW: A utility for performing maths.
USAGE: math <subcommand>
OPTIONS:
--version Show the version.
-h, --help Show help information.
SUBCOMMANDS:
add (default) Print the sum of the values.
multiply Print the product of the values.
stats Calculate descriptive statistics.
See 'math help <subcommand>' for detailed help.
"""
try AssertExecuteCommand(command: "math -h", expected: helpText)
try AssertExecuteCommand(command: "math --help", expected: helpText)
try AssertExecuteCommand(command: "math help", expected: helpText)
}
func testMath_AddHelp() throws {
let helpText = """
OVERVIEW: Print the sum of the values.
USAGE: math add [--hex-output] [<values> ...]
ARGUMENTS:
<values> A group of integers to operate on.
OPTIONS:
-x, --hex-output Use hexadecimal notation for the result.
--version Show the version.
-h, --help Show help information.
"""
try AssertExecuteCommand(command: "math add -h", expected: helpText)
try AssertExecuteCommand(command: "math add --help", expected: helpText)
try AssertExecuteCommand(command: "math help add", expected: helpText)
// Verify that extra help flags are ignored.
try AssertExecuteCommand(command: "math help add -h", expected: helpText)
try AssertExecuteCommand(command: "math help add -help", expected: helpText)
try AssertExecuteCommand(command: "math help add --help", expected: helpText)
}
func testMath_StatsMeanHelp() throws {
let helpText = """
OVERVIEW: Print the average of the values.
USAGE: math stats average [--kind <kind>] [<values> ...]
ARGUMENTS:
<values> A group of floating-point values to operate on.
OPTIONS:
--kind <kind> The kind of average to provide. (default: mean)
--version Show the version.
-h, --help Show help information.
"""
try AssertExecuteCommand(command: "math stats average -h", expected: helpText)
try AssertExecuteCommand(command: "math stats average --help", expected: helpText)
try AssertExecuteCommand(command: "math help stats average", expected: helpText)
}
func testMath_StatsQuantilesHelp() throws {
let helpText = """
OVERVIEW: Print the quantiles of the values (TBD).
USAGE: math stats quantiles [<one-of-four>] [<custom-arg>] [<values> ...] [--file <file>] [--directory <directory>] [--shell <shell>] [--custom <custom>]
ARGUMENTS:
<one-of-four>
<custom-arg>
<values> A group of floating-point values to operate on.
OPTIONS:
--file <file>
--directory <directory>
--shell <shell>
--custom <custom>
--version Show the version.
-h, --help Show help information.
"""
// The "quantiles" subcommand's run() method is unimplemented, so it
// just generates the help text.
try AssertExecuteCommand(command: "math stats quantiles", expected: helpText)
try AssertExecuteCommand(command: "math stats quantiles -h", expected: helpText)
try AssertExecuteCommand(command: "math stats quantiles --help", expected: helpText)
try AssertExecuteCommand(command: "math help stats quantiles", expected: helpText)
}
func testMath_CustomValidation() throws {
try AssertExecuteCommand(
command: "math stats average --kind mode",
expected: """
Error: Please provide at least one value to calculate the mode.
Usage: math stats average [--kind <kind>] [<values> ...]
See 'math stats average --help' for more information.
""",
exitCode: .validationFailure)
}
func testMath_Versions() throws {
try AssertExecuteCommand(
command: "math --version",
expected: "1.0.0")
try AssertExecuteCommand(
command: "math stats --version",
expected: "1.0.0")
try AssertExecuteCommand(
command: "math stats average --version",
expected: "1.5.0-alpha")
}
func testMath_ExitCodes() throws {
try AssertExecuteCommand(
command: "math stats quantiles --test-success-exit-code",
expected: "",
exitCode: .success)
try AssertExecuteCommand(
command: "math stats quantiles --test-failure-exit-code",
expected: "",
exitCode: .failure)
try AssertExecuteCommand(
command: "math stats quantiles --test-validation-exit-code",
expected: "",
exitCode: .validationFailure)
try AssertExecuteCommand(
command: "math stats quantiles --test-custom-exit-code 42",
expected: "",
exitCode: ExitCode(42))
}
func testMath_Fail() throws {
try AssertExecuteCommand(
command: "math --foo",
expected: """
Error: Unknown option '--foo'
Usage: math add [--hex-output] [<values> ...]
See 'math add --help' for more information.
""",
exitCode: .validationFailure)
try AssertExecuteCommand(
command: "math ZZZ",
expected: """
Error: The value 'ZZZ' is invalid for '<values>'
Help: <values> A group of integers to operate on.
Usage: math add [--hex-output] [<values> ...]
See 'math add --help' for more information.
""",
exitCode: .validationFailure)
}
}
// MARK: - Completion Script
extension MathExampleTests {
func testMath_CompletionScript() throws {
try AssertExecuteCommand(
command: "math --generate-completion-script=bash",
expected: bashCompletionScriptText)
try AssertExecuteCommand(
command: "math --generate-completion-script bash",
expected: bashCompletionScriptText)
try AssertExecuteCommand(
command: "math --generate-completion-script=zsh",
expected: zshCompletionScriptText)
try AssertExecuteCommand(
command: "math --generate-completion-script zsh",
expected: zshCompletionScriptText)
try AssertExecuteCommand(
command: "math --generate-completion-script=fish",
expected: fishCompletionScriptText)
try AssertExecuteCommand(
command: "math --generate-completion-script fish",
expected: fishCompletionScriptText)
}
func testMath_CustomCompletion() throws {
try AssertExecuteCommand(
command: "math ---completion stats quantiles -- --custom",
expected: """
hello
helicopter
heliotrope
""")
try AssertExecuteCommand(
command: "math ---completion stats quantiles -- --custom h",
expected: """
hello
helicopter
heliotrope
""")
try AssertExecuteCommand(
command: "math ---completion stats quantiles -- --custom a",
expected: """
aardvark
aaaaalbert
""")
}
}
private let bashCompletionScriptText = """
#!/bin/bash
_math() {
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=()
opts="--version -h --help add multiply stats help"
if [[ $COMP_CWORD == "1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
case ${COMP_WORDS[1]} in
(add)
_math_add 2
return
;;
(multiply)
_math_multiply 2
return
;;
(stats)
_math_stats 2
return
;;
(help)
_math_help 2
return
;;
esac
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_add() {
opts="--hex-output -x --version -h --help"
if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_multiply() {
opts="--hex-output -x --version -h --help"
if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_stats() {
opts="--version -h --help average stdev quantiles"
if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
case ${COMP_WORDS[$1]} in
(average)
_math_stats_average $(($1+1))
return
;;
(stdev)
_math_stats_stdev $(($1+1))
return
;;
(quantiles)
_math_stats_quantiles $(($1+1))
return
;;
esac
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_stats_average() {
opts="--kind --version -h --help"
if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
case $prev in
--kind)
COMPREPLY=( $(compgen -W "mean median mode" -- "$cur") )
return
;;
esac
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_stats_stdev() {
opts="--version -h --help"
if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_stats_quantiles() {
opts="--file --directory --shell --custom --version -h --help"
opts="$opts alphabet alligator branch braggart"
opts="$opts $("${COMP_WORDS[0]}" ---completion stats quantiles -- customArg "${COMP_WORDS[@]}")"
if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
case $prev in
--file)
COMPREPLY=( $(compgen -f -- "$cur") )
return
;;
--directory)
COMPREPLY=( $(compgen -d -- "$cur") )
return
;;
--shell)
COMPREPLY=( $(head -100 /usr/share/dict/words | tail -50) )
return
;;
--custom)
COMPREPLY=( $(compgen -W "$("${COMP_WORDS[0]}" ---completion stats quantiles -- --custom "${COMP_WORDS[@]}")" -- "$cur") )
return
;;
esac
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_math_help() {
opts="--version"
if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
complete -F _math math
"""
private let zshCompletionScriptText = """
#compdef math
local context state state_descr line
_math_commandname=$words[1]
typeset -A opt_args
_math() {
integer ret=1
local -a args
args+=(
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
'(-): :->command'
'(-)*:: :->arg'
)
_arguments -w -s -S $args[@] && ret=0
case $state in
(command)
local subcommands
subcommands=(
'add:Print the sum of the values.'
'multiply:Print the product of the values.'
'stats:Calculate descriptive statistics.'
'help:Show subcommand help information.'
)
_describe "subcommand" subcommands
;;
(arg)
case ${words[1]} in
(add)
_math_add
;;
(multiply)
_math_multiply
;;
(stats)
_math_stats
;;
(help)
_math_help
;;
esac
;;
esac
return ret
}
_math_add() {
integer ret=1
local -a args
args+=(
'(--hex-output -x)'{--hex-output,-x}'[Use hexadecimal notation for the result.]'
':values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_math_multiply() {
integer ret=1
local -a args
args+=(
'(--hex-output -x)'{--hex-output,-x}'[Use hexadecimal notation for the result.]'
':values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_math_stats() {
integer ret=1
local -a args
args+=(
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
'(-): :->command'
'(-)*:: :->arg'
)
_arguments -w -s -S $args[@] && ret=0
case $state in
(command)
local subcommands
subcommands=(
'average:Print the average of the values.'
'stdev:Print the standard deviation of the values.'
'quantiles:Print the quantiles of the values (TBD).'
)
_describe "subcommand" subcommands
;;
(arg)
case ${words[1]} in
(average)
_math_stats_average
;;
(stdev)
_math_stats_stdev
;;
(quantiles)
_math_stats_quantiles
;;
esac
;;
esac
return ret
}
_math_stats_average() {
integer ret=1
local -a args
args+=(
'--kind[The kind of average to provide.]:kind:(mean median mode)'
':values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_math_stats_stdev() {
integer ret=1
local -a args
args+=(
':values:'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_math_stats_quantiles() {
integer ret=1
local -a args
args+=(
':one-of-four:(alphabet alligator branch braggart)'
':custom-arg:{_custom_completion $_math_commandname ---completion stats quantiles -- customArg $words}'
':values:'
'--file:file:_files -g '"'"'*.txt *.md'"'"''
'--directory:directory:_files -/'
'--shell:shell:{local -a list; list=(${(f)"$(head -100 /usr/share/dict/words | tail -50)"}); _describe '''' list}'
'--custom:custom:{_custom_completion $_math_commandname ---completion stats quantiles -- --custom $words}'
'--version[Show the version.]'
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_math_help() {
integer ret=1
local -a args
args+=(
':subcommands:'
'--version[Show the version.]'
)
_arguments -w -s -S $args[@] && ret=0
return ret
}
_custom_completion() {
local completions=("${(@f)$($*)}")
_describe '' completions
}
_math
"""
private let fishCompletionScriptText = """
# A function which filters options which starts with \"-\" from $argv.
function _swift_math_preprocessor
set -l results
for i in (seq (count $argv))
switch (echo $argv[$i] | string sub -l 1)
case '-'
case '*'
echo $argv[$i]
end
end
end
function _swift_math_using_command
set -l currentCommands (_swift_math_preprocessor (commandline -opc))
set -l expectedCommands (string split \" \" $argv[1])
set -l subcommands (string split \" \" $argv[2])
if [ (count $currentCommands) -ge (count $expectedCommands) ]
for i in (seq (count $expectedCommands))
if [ $currentCommands[$i] != $expectedCommands[$i] ]
return 1
end
end
if [ (count $currentCommands) -eq (count $expectedCommands) ]
return 0
end
if [ (count $subcommands) -gt 1 ]
for i in (seq (count $subcommands))
if [ $currentCommands[(math (count $expectedCommands) + 1)] = $subcommands[$i] ]
return 1
end
end
end
return 0
end
return 1
end
complete -c math -n \'_swift_math_using_command \"math add\"\' -l hex-output -s x -d \'Use hexadecimal notation for the result.\'
complete -c math -n \'_swift_math_using_command \"math add\"\' -s h -l help -d \'Show help information.\'
complete -c math -n \'_swift_math_using_command \"math multiply\"\' -l hex-output -s x -d \'Use hexadecimal notation for the result.\'
complete -c math -n \'_swift_math_using_command \"math multiply\"\' -s h -l help -d \'Show help information.\'
complete -c math -n \'_swift_math_using_command \"math stats average\"\' -l kind -d \'The kind of average to provide.\' -r -f -k -a \'mean median mode\'
complete -c math -n \'_swift_math_using_command \"math stats average\"\' -l version -d \'Show the version.\'
complete -c math -n \'_swift_math_using_command \"math stats average\"\' -s h -l help -d \'Show help information.\'
complete -c math -n \'_swift_math_using_command \"math stats stdev\"\' -s h -l help -d \'Show help information.\'
complete -c math -n \'_swift_math_using_command \"math stats quantiles\"\' -l file -r -f -a \'(for i in *.{txt,md}; echo $i;end)\'
complete -c math -n \'_swift_math_using_command \"math stats quantiles\"\' -l directory -r -f -a \'(__fish_complete_directories)\'
complete -c math -n \'_swift_math_using_command \"math stats quantiles\"\' -l shell -r -f -a \'(head -100 /usr/share/dict/words | tail -50)\'
complete -c math -n \'_swift_math_using_command \"math stats quantiles\"\' -l custom -r -f -a \'(command math ---completion stats quantiles -- --custom (commandline -opc)[1..-1])\'
complete -c math -n \'_swift_math_using_command \"math stats quantiles\"\' -s h -l help -d \'Show help information.\'
complete -c math -n \'_swift_math_using_command \"math stats\" \"average stdev quantiles help\"\' -s h -l help -d \'Show help information.\'
complete -c math -n \'_swift_math_using_command \"math stats\" \"average stdev quantiles help\"\' -f -a \'average\' -d \'Print the average of the values.\'
complete -c math -n \'_swift_math_using_command \"math stats\" \"average stdev quantiles help\"\' -f -a \'stdev\' -d \'Print the standard deviation of the values.\'
complete -c math -n \'_swift_math_using_command \"math stats\" \"average stdev quantiles help\"\' -f -a \'quantiles\' -d \'Print the quantiles of the values (TBD).\'
complete -c math -n \'_swift_math_using_command \"math stats\" \"average stdev quantiles help\"\' -f -a \'help\' -d \'Show subcommand help information.\'
complete -c math -n \'_swift_math_using_command \"math\" \"add multiply stats help\"\' -l version -d \'Show the version.\'
complete -c math -n \'_swift_math_using_command \"math\" \"add multiply stats help\"\' -s h -l help -d \'Show help information.\'
complete -c math -n \'_swift_math_using_command \"math\" \"add multiply stats help\"\' -f -a \'add\' -d \'Print the sum of the values.\'
complete -c math -n \'_swift_math_using_command \"math\" \"add multiply stats help\"\' -f -a \'multiply\' -d \'Print the product of the values.\'
complete -c math -n \'_swift_math_using_command \"math\" \"add multiply stats help\"\' -f -a \'stats\' -d \'Calculate descriptive statistics.\'
complete -c math -n \'_swift_math_using_command \"math\" \"add multiply stats help\"\' -f -a \'help\' -d \'Show subcommand help information.\'
"""
|