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
|
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2022 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 ArgumentParserTestHelpers
@testable import ArgumentParser
// MARK: - allUnrecognized
fileprivate struct AllUnrecognizedArgs: ParsableCommand {
static var configuration: CommandConfiguration {
.init(version: "1.0")
}
@Flag var verbose: Bool = false
@Flag(name: .customShort("f")) var useFiles: Bool = false
@Flag(name: .customShort("i")) var useStandardInput: Bool = false
@Flag(name: .customShort("h")) var hoopla: Bool = false
@Option var config = "debug"
@Argument(parsing: .allUnrecognized) var names: [String] = []
}
extension RepeatingEndToEndTests {
func testParsing_repeatingAllUnrecognized() throws {
AssertParse(AllUnrecognizedArgs.self, []) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertFalse(cmd.hoopla)
XCTAssertEqual(cmd.names, [])
}
AssertParse(AllUnrecognizedArgs.self, ["foo", "--verbose", "-fi", "bar", "-z", "--other"]) { cmd in
XCTAssertTrue(cmd.verbose)
XCTAssertTrue(cmd.useFiles)
XCTAssertTrue(cmd.useStandardInput)
XCTAssertFalse(cmd.hoopla)
XCTAssertEqual(cmd.names, ["foo", "bar", "-z", "--other"])
}
}
func testParsing_repeatingAllUnrecognized_Builtin() throws {
AssertParse(AllUnrecognizedArgs.self, ["foo", "--verbose", "bar", "-z", "-h"]) { cmd in
XCTAssertTrue(cmd.verbose)
XCTAssertFalse(cmd.useFiles)
XCTAssertFalse(cmd.useStandardInput)
XCTAssertTrue(cmd.hoopla)
XCTAssertEqual(cmd.names, ["foo", "bar", "-z"])
}
AssertParseCommand(AllUnrecognizedArgs.self, HelpCommand.self, ["foo", "--verbose", "bar", "-z", "--help"]) { cmd in
// No need to test HelpCommand properties
}
XCTAssertThrowsError(try AllUnrecognizedArgs.parse(["foo", "--verbose", "--version"]))
}
func testParsing_repeatingAllUnrecognized_Fails() throws {
// Only partially matches the `-fib` argument
XCTAssertThrowsError(try PassthroughArgs.parse(["-fib"]))
}
}
fileprivate struct AllUnrecognizedRoot: ParsableCommand {
static var configuration: CommandConfiguration {
.init(subcommands: [Child.self])
}
@Flag var verbose: Bool = false
struct Child: ParsableCommand {
@Flag var includeExtras: Bool = false
@Option var config = "debug"
@Argument(parsing: .allUnrecognized) var extras: [String] = []
@OptionGroup var root: AllUnrecognizedRoot
}
}
extension RepeatingEndToEndTests {
func testParsing_repeatingAllUnrecognized_Nested() throws {
AssertParseCommand(
AllUnrecognizedRoot.self, AllUnrecognizedRoot.Child.self,
["child"])
{ cmd in
XCTAssertFalse(cmd.root.verbose)
XCTAssertFalse(cmd.includeExtras)
XCTAssertEqual(cmd.config, "debug")
XCTAssertEqual(cmd.extras, [])
}
AssertParseCommand(
AllUnrecognizedRoot.self, AllUnrecognizedRoot.Child.self,
["child", "--verbose", "--other", "one", "two", "--config", "prod"])
{ cmd in
XCTAssertTrue(cmd.root.verbose)
XCTAssertFalse(cmd.includeExtras)
XCTAssertEqual(cmd.config, "prod")
XCTAssertEqual(cmd.extras, ["--other", "one", "two"])
}
}
func testParsing_repeatingAllUnrecognized_Nested_Fails() throws {
// Extra arguments need to make it to the child
XCTAssertThrowsError(try AllUnrecognizedRoot.parse(["--verbose", "--other"]))
}
}
// MARK: - postTerminator
fileprivate struct PostTerminatorArgs: ParsableArguments {
@Flag(name: .customShort("f")) var useFiles: Bool = false
@Flag(name: .customShort("i")) var useStandardInput: Bool = false
@Option var config = "debug"
@Argument var title: String?
@Argument(parsing: .postTerminator)
var names: [String] = []
}
extension RepeatingEndToEndTests {
func testParsing_repeatingPostTerminator() throws {
AssertParse(PostTerminatorArgs.self, []) { cmd in
XCTAssertNil(cmd.title)
XCTAssertEqual(cmd.names, [])
}
AssertParse(PostTerminatorArgs.self, ["--", "-fi"]) { cmd in
XCTAssertNil(cmd.title)
XCTAssertEqual(cmd.names, ["-fi"])
}
AssertParse(PostTerminatorArgs.self, ["-fi", "--", "-fi", "--"]) { cmd in
XCTAssertTrue(cmd.useFiles)
XCTAssertTrue(cmd.useStandardInput)
XCTAssertNil(cmd.title)
XCTAssertEqual(cmd.names, ["-fi", "--"])
}
AssertParse(PostTerminatorArgs.self, ["-fi", "title", "--", "title"]) { cmd in
XCTAssertTrue(cmd.useFiles)
XCTAssertTrue(cmd.useStandardInput)
XCTAssertEqual(cmd.title, "title")
XCTAssertEqual(cmd.names, ["title"])
}
AssertParse(PostTerminatorArgs.self, ["--config", "config", "--", "--config", "post"]) { cmd in
XCTAssertEqual(cmd.config, "config")
XCTAssertNil(cmd.title)
XCTAssertEqual(cmd.names, ["--config", "post"])
}
}
func testParsing_repeatingPostTerminator_Fails() throws {
// Only partially matches the `-fib` argument
XCTAssertThrowsError(try PostTerminatorArgs.parse(["-fib"]))
// The post-terminator input can't provide the option's value
XCTAssertThrowsError(try PostTerminatorArgs.parse(["--config", "--", "config"]))
}
}
// MARK: - captureForPassthrough
fileprivate struct PassthroughArgs: ParsableCommand {
@Flag var verbose: Bool = false
@Flag(name: .customShort("f")) var useFiles: Bool = false
@Flag(name: .customShort("i")) var useStandardInput: Bool = false
@Option var config = "debug"
@Argument(parsing: .captureForPassthrough) var names: [String] = []
}
extension RepeatingEndToEndTests {
func testParsing_repeatingCaptureForPassthrough() throws {
AssertParse(PassthroughArgs.self, []) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertEqual(cmd.names, [])
}
AssertParse(PassthroughArgs.self, ["--other"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertEqual(cmd.names, ["--other"])
}
AssertParse(PassthroughArgs.self, ["--verbose", "one", "two", "three"]) { cmd in
XCTAssertTrue(cmd.verbose)
XCTAssertEqual(cmd.names, ["one", "two", "three"])
}
AssertParse(PassthroughArgs.self, ["one", "two", "three", "--other", "--verbose"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertEqual(cmd.names, ["one", "two", "three", "--other", "--verbose"])
}
AssertParse(PassthroughArgs.self, ["--verbose", "--other", "one", "two", "three"]) { cmd in
XCTAssertTrue(cmd.verbose)
XCTAssertEqual(cmd.names, ["--other", "one", "two", "three"])
}
AssertParse(PassthroughArgs.self, ["--verbose", "--other", "one", "--", "two", "three"]) { cmd in
XCTAssertTrue(cmd.verbose)
XCTAssertEqual(cmd.names, ["--other", "one", "--", "two", "three"])
}
AssertParse(PassthroughArgs.self, ["--other", "one", "--", "two", "three", "--verbose"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertEqual(cmd.names, ["--other", "one", "--", "two", "three", "--verbose"])
}
AssertParse(PassthroughArgs.self, ["--", "--verbose", "--other", "one", "two", "three"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertEqual(cmd.names, ["--", "--verbose", "--other", "one", "two", "three"])
}
AssertParse(PassthroughArgs.self, ["-one", "-two", "three"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertFalse(cmd.useFiles)
XCTAssertFalse(cmd.useStandardInput)
XCTAssertEqual(cmd.names, ["-one", "-two", "three"])
}
AssertParse(PassthroughArgs.self, ["--config", "release", "one", "two", "--config", "debug"]) { cmd in
XCTAssertEqual(cmd.config, "release")
XCTAssertEqual(cmd.names, ["one", "two", "--config", "debug"])
}
AssertParse(PassthroughArgs.self, ["--config", "release", "--config", "debug", "one", "two"]) { cmd in
XCTAssertEqual(cmd.config, "debug")
XCTAssertEqual(cmd.names, ["one", "two"])
}
AssertParse(PassthroughArgs.self, ["-if", "-one", "-two", "three"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertTrue(cmd.useFiles)
XCTAssertTrue(cmd.useStandardInput)
XCTAssertEqual(cmd.names, ["-one", "-two", "three"])
}
AssertParse(PassthroughArgs.self, ["-one", "-two", "-three", "-if"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertFalse(cmd.useFiles)
XCTAssertFalse(cmd.useStandardInput)
XCTAssertEqual(cmd.names, ["-one", "-two", "-three", "-if"])
}
AssertParse(PassthroughArgs.self, ["-one", "-two", "-three", "-if", "--help"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertFalse(cmd.useFiles)
XCTAssertFalse(cmd.useStandardInput)
XCTAssertEqual(cmd.names, ["-one", "-two", "-three", "-if", "--help"])
}
AssertParse(PassthroughArgs.self, ["-one", "-two", "-three", "-if", "-h"]) { cmd in
XCTAssertFalse(cmd.verbose)
XCTAssertFalse(cmd.useFiles)
XCTAssertFalse(cmd.useStandardInput)
XCTAssertEqual(cmd.names, ["-one", "-two", "-three", "-if", "-h"])
}
}
func testParsing_repeatingCaptureForPassthrough_Fails() throws {
// Only partially matches the `-fib` argument
XCTAssertThrowsError(try PassthroughArgs.parse(["-fib"]))
}
}
|