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
|
//===----------------------------------------------------------*- 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
@testable import ArgumentParser
final class UsageGenerationTests: XCTestCase {
}
func _testSynopsis<T: ParsableArguments>(
_ type: T.Type,
visibility: ArgumentVisibility = .default,
expected: String,
file: StaticString = #file,
line: UInt = #line
) {
let help = UsageGenerator(toolName: "example", parsable: T(), visibility: visibility, parent: nil)
XCTAssertEqual(help.synopsis, expected, file: file, line: line)
}
// MARK: -
extension UsageGenerationTests {
func testNameSynopsis() {
XCTAssertEqual(Name.long("foo").synopsisString, "--foo")
XCTAssertEqual(Name.short("f").synopsisString, "-f")
XCTAssertEqual(Name.longWithSingleDash("foo").synopsisString, "-foo")
}
}
extension UsageGenerationTests {
struct A: ParsableArguments {
@Option() var firstName: String
@Option() var title: String
}
func testSynopsis() {
_testSynopsis(A.self, expected: "example --first-name <first-name> --title <title>")
}
struct B: ParsableArguments {
@Option() var firstName: String?
@Option() var title: String?
}
func testSynopsisWithOptional() {
_testSynopsis(B.self, expected: "example [--first-name <first-name>] [--title <title>]")
}
struct C: ParsableArguments {
@Flag var log: Bool = false
@Flag() var verbose: Int
}
func testFlagSynopsis() {
_testSynopsis(C.self, expected: "example [--log] [--verbose ...]")
}
struct D: ParsableArguments {
@Argument() var firstName: String
@Argument() var title: String?
}
func testPositionalSynopsis() {
_testSynopsis(D.self, expected: "example <first-name> [<title>]")
}
struct E: ParsableArguments {
@Option
var name: String = "no-name"
@Option
var count: Int = 0
@Argument
var arg: String = "no-arg"
}
func testSynopsisWithDefaults() {
_testSynopsis(E.self, expected: "example [--name <name>] [--count <count>] [<arg>]")
}
struct F: ParsableArguments {
@Option() var name: [String] = []
@Argument() var nameCounts: [Int] = []
}
func testSynopsisWithRepeats() {
_testSynopsis(F.self, expected: "example [--name <name> ...] [<name-counts> ...]")
}
struct G: ParsableArguments {
@Option(help: ArgumentHelp(valueName: "path"))
var filePath: String?
@Argument(help: ArgumentHelp(valueName: "user-home-path"))
var homePath: String
}
func testSynopsisWithCustomization() {
_testSynopsis(G.self, expected: "example [--file-path <path>] <user-home-path>")
}
struct H: ParsableArguments {
@Option(help: .hidden) var firstName: String?
@Argument(help: .hidden) var title: String?
}
func testSynopsisWithHidden() {
_testSynopsis(H.self, expected: "example")
_testSynopsis(H.self, visibility: .hidden, expected: "example [--first-name <first-name>] [<title>]")
}
struct I: ParsableArguments {
enum Color {
case red, blue
static func transform(_ string: String) throws -> Color {
switch string {
case "red":
return .red
case "blue":
return .blue
default:
throw ValidationError("Not a valid string for 'Color'")
}
}
}
@Option(transform: Color.transform)
var color: Color = .red
}
func testSynopsisWithDefaultValueAndTransform() {
_testSynopsis(I.self, expected: "example [--color <color>]")
}
struct J: ParsableArguments {
struct Foo {}
@Option(transform: { _ in Foo() }) var req: Foo
@Option(transform: { _ in Foo() }) var opt: Foo?
}
func testSynopsisWithTransform() {
_testSynopsis(J.self, expected: "example --req <req> [--opt <opt>]")
}
struct K: ParsableArguments {
@Option(
name: [.short, .customLong("remote"), .customLong("when"), .customLong("there")],
help: "Help Message")
var time: String?
}
func testSynopsisWithMultipleCustomNames() {
_testSynopsis(K.self, expected: "example [--remote <remote>]")
}
struct L: ParsableArguments {
@Option(
name: [.short, .short, .customLong("remote", withSingleDash: true), .short, .customLong("remote", withSingleDash: true)],
help: "Help Message")
var time: String?
}
func testSynopsisWithSingleDashLongNameFirst() {
_testSynopsis(L.self, expected: "example [-remote <remote>]")
}
struct M: ParsableArguments {
enum Color: String, EnumerableFlag {
case green, blue, yellow
}
@Flag var a: Bool = false
@Flag var b: Bool = false
@Flag var c: Bool = false
@Flag var d: Bool = false
@Flag var e: Bool = false
@Flag var f: Bool = false
@Flag var g: Bool = false
@Flag var h: Bool = false
@Flag var i: Bool = false
@Flag var j: Bool = false
@Flag var k: Bool = false
@Flag var l: Bool = false
@Flag(inversion: .prefixedEnableDisable)
var optionalBool: Bool?
@Flag var optionalColor: Color?
@Option var option: Bool
@Argument var input: String
@Argument var output: String?
}
func testSynopsisWithTooManyOptions() {
_testSynopsis(M.self, expected: "example [<options>] --option <option> <input> [<output>]")
}
struct N: ParsableArguments {
@Flag var a: Bool = false
@Flag var b: Bool = false
var title = "defaulted value"
var decode = false
}
func testNonwrappedValues() {
_testSynopsis(N.self, expected: "example [--a] [--b]")
_testSynopsis(N.self, visibility: .hidden, expected: "example [--a] [--b]")
}
struct O: ParsableArguments {
@Argument var a: String
@Argument(parsing: .postTerminator) var b: [String] = []
}
func testSynopsisWithPostTerminatorParsingStrategy() {
_testSynopsis(O.self, expected: "example <a> -- [<b> ...]")
}
}
|