Files
swift-argument-parser/Tests/ExampleTests/MathExampleTests.swift
T
2020-02-27 15:45:22 -06:00

119 lines
3.9 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 XCTest
import TestHelpers
final class MathExampleTests: XCTestCase {
func testMath_Simple() throws {
AssertExecuteCommand(command: "math 1 2 3 4 5", expected: "15")
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:
-h, --help Show help information.
SUBCOMMANDS:
add Print the sum of the values.
multiply Print the product of the values.
stats Calculate descriptive statistics.
"""
AssertExecuteCommand(command: "math -h", expected: helpText)
AssertExecuteCommand(command: "math --help", expected: helpText)
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.
-h, --help Show help information.
"""
AssertExecuteCommand(command: "math add -h", expected: helpText)
AssertExecuteCommand(command: "math add --help", expected: helpText)
AssertExecuteCommand(command: "math help add", 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)
-h, --help Show help information.
"""
AssertExecuteCommand(command: "math stats average -h", expected: helpText)
AssertExecuteCommand(command: "math stats average --help", expected: helpText)
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 [<values> ...]
ARGUMENTS:
<values> A group of floating-point values to operate on.
OPTIONS:
-h, --help Show help information.
"""
// The "quantiles" subcommand's run() method is unimplemented, so it
// just generates the help text.
AssertExecuteCommand(command: "math stats quantiles", expected: helpText)
AssertExecuteCommand(command: "math stats quantiles -h", expected: helpText)
AssertExecuteCommand(command: "math stats quantiles --help", expected: helpText)
AssertExecuteCommand(command: "math help stats quantiles", expected: helpText)
}
func testMath_Fail() throws {
AssertExecuteCommand(
command: "math --foo",
expected: """
Error: Unexpected argument '--foo'
Usage: math add [--hex-output] [<values> ...]
""",
shouldError: true)
AssertExecuteCommand(
command: "math ZZZ",
expected: """
Error: The value 'ZZZ' is invalid for '<values>'
Usage: math add [--hex-output] [<values> ...]
""",
shouldError: true)
}
}