File: DefaultSubcommandEndToEndTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (193 lines) | stat: -rw-r--r-- 7,043 bytes parent folder | download
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
//===----------------------------------------------------------*- 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 ArgumentParserTestHelpers
import ArgumentParser

final class DefaultSubcommandEndToEndTests: XCTestCase {
}

// MARK: -

private struct Main: ParsableCommand {
  static var configuration = CommandConfiguration(
    subcommands: [Default.self, Foo.self, Bar.self],
    defaultSubcommand: Default.self
  )
}

private struct Default: ParsableCommand {
  enum Mode: String, CaseIterable, ExpressibleByArgument {
    case foo, bar, baz
  }

  @Option var mode: Mode = .foo
}

private struct Foo: ParsableCommand {}
private struct Bar: ParsableCommand {}

extension DefaultSubcommandEndToEndTests {
  func testDefaultSubcommand() {
    AssertParseCommand(Main.self, Default.self, []) { def in
      XCTAssertEqual(.foo, def.mode)
    }

    AssertParseCommand(Main.self, Default.self, ["--mode=bar"]) { def in
      XCTAssertEqual(.bar, def.mode)
    }

    AssertParseCommand(Main.self, Default.self, ["--mode", "bar"]) { def in
      XCTAssertEqual(.bar, def.mode)
    }

    AssertParseCommand(Main.self, Default.self, ["--mode", "baz"]) { def in
      XCTAssertEqual(.baz, def.mode)
    }
  }

  func testNonDefaultSubcommand() {
    AssertParseCommand(Main.self, Foo.self, ["foo"]) { _ in }
    AssertParseCommand(Main.self, Bar.self, ["bar"]) { _ in }

    AssertParseCommand(Main.self, Default.self, ["default", "--mode", "bar"]) { def in
      XCTAssertEqual(.bar, def.mode)
    }
  }

  func testParsingFailure() {
    XCTAssertThrowsError(try Main.parseAsRoot(["--mode", "qux"]))
    XCTAssertThrowsError(try Main.parseAsRoot(["qux"]))
  }
}

extension DefaultSubcommandEndToEndTests {
  fileprivate struct MyCommand: ParsableCommand {
    static var configuration = CommandConfiguration(
      subcommands: [Plugin.self, NonDefault.self, Other.self],
      defaultSubcommand: Plugin.self
    )
    
    @OptionGroup
    var options: CommonOptions
  }
  
  fileprivate struct CommonOptions: ParsableArguments {
    @Flag(name: [.customLong("verbose"), .customShort("v")],
          help: "Enable verbose aoutput.")
    var verbose = false
  }
  
  fileprivate struct Plugin: ParsableCommand {
    @OptionGroup var options: CommonOptions
    @Argument var pluginName: String
    
    @Argument(parsing: .captureForPassthrough)
    var pluginArguments: [String] = []
  }
  
  fileprivate struct NonDefault: ParsableCommand {
    @OptionGroup var options: CommonOptions
    @Argument var pluginName: String
    
    @Argument(parsing: .captureForPassthrough)
    var pluginArguments: [String] = []
  }
  
  fileprivate struct Other: ParsableCommand {
    @OptionGroup var options: CommonOptions
  }
  
  func testRemainingDefaultImplicit() throws {
    AssertParseCommand(MyCommand.self, Plugin.self, ["my-plugin"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, [])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["--verbose", "my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, true)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["my-plugin", "--help"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--help"])
      XCTAssertEqual(plugin.options.verbose, false)
    }
  }

  func testRemainingDefaultExplicit() throws {
    AssertParseCommand(MyCommand.self, Plugin.self, ["plugin", "my-plugin"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, [])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["plugin", "my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["--verbose", "plugin", "my-plugin", "--verbose"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--verbose"])
      XCTAssertEqual(plugin.options.verbose, true)
    }
    AssertParseCommand(MyCommand.self, Plugin.self, ["--verbose", "plugin", "my-plugin", "--help"]) { plugin in
      XCTAssertEqual(plugin.pluginName, "my-plugin")
      XCTAssertEqual(plugin.pluginArguments, ["--help"])
      XCTAssertEqual(plugin.options.verbose, true)
    }
  }

  func testRemainingNonDefault() throws {
    AssertParseCommand(MyCommand.self, NonDefault.self, ["non-default", "my-plugin"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, [])
      XCTAssertEqual(nondef.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, NonDefault.self, ["non-default", "my-plugin", "--verbose"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, ["--verbose"])
      XCTAssertEqual(nondef.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, NonDefault.self, ["--verbose", "non-default", "my-plugin", "--verbose"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, ["--verbose"])
      XCTAssertEqual(nondef.options.verbose, true)
    }
    AssertParseCommand(MyCommand.self, NonDefault.self, ["--verbose", "non-default", "my-plugin", "--help"]) { nondef in
      XCTAssertEqual(nondef.pluginName, "my-plugin")
      XCTAssertEqual(nondef.pluginArguments, ["--help"])
      XCTAssertEqual(nondef.options.verbose, true)
    }
  }

  func testRemainingDefaultOther() throws {
    AssertParseCommand(MyCommand.self, Other.self, ["other"]) { other in
      XCTAssertEqual(other.options.verbose, false)
    }
    AssertParseCommand(MyCommand.self, Other.self, ["other", "--verbose"]) { other in
      XCTAssertEqual(other.options.verbose, true)
    }
  }
  
  func testRemainingDefaultFailure() {
    XCTAssertThrowsError(try MyCommand.parseAsRoot([]))
    XCTAssertThrowsError(try MyCommand.parseAsRoot(["--verbose"]))
    XCTAssertThrowsError(try MyCommand.parseAsRoot(["plugin", "--verbose", "my-plugin"]))
  }
}