mirror of
https://github.com/apple/swift-argument-parser.git
synced 2026-06-06 20:18:23 +00:00
Adopts the common swift-mmio and swift-argument-parser format and enables CI checking. Fixes: #702
247 lines
7.8 KiB
Swift
247 lines
7.8 KiB
Swift
//===----------------------------------------------------------*- 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 ArgumentParser
|
|
import ArgumentParserTestHelpers
|
|
import XCTest
|
|
|
|
final class MathExampleTests: XCTestCase {
|
|
override func setUp() {
|
|
#if !os(Windows) && !os(WASI)
|
|
unsetenv("COLUMNS")
|
|
#endif
|
|
}
|
|
|
|
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, mul 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. (values: mean,
|
|
median, mode; 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
|
|
|
|
// swift-format-ignore: AlwaysUseLowerCamelCase
|
|
// https://github.com/apple/swift-argument-parser/issues/710
|
|
extension MathExampleTests {
|
|
func testMathBashCompletionScript() throws {
|
|
let script = try AssertExecuteCommand(
|
|
command: "math --generate-completion-script bash")
|
|
try assertSnapshot(actual: script, extension: "bash")
|
|
}
|
|
|
|
func testMathZshCompletionScript() throws {
|
|
let script = try AssertExecuteCommand(
|
|
command: "math --generate-completion-script zsh")
|
|
try assertSnapshot(actual: script, extension: "zsh")
|
|
}
|
|
|
|
func testMathFishCompletionScript() throws {
|
|
let script = try AssertExecuteCommand(
|
|
command: "math --generate-completion-script fish")
|
|
try assertSnapshot(actual: script, extension: "fish")
|
|
}
|
|
|
|
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
|
|
""")
|
|
}
|
|
}
|