/
ErrorMessageTests.swift
271 lines (222 loc) · 8.25 KB
/
ErrorMessageTests.swift
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
263
264
265
266
267
268
269
270
271
//===----------------------------------------------------------*- 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
@testable import ArgumentParser
final class ErrorMessageTests: XCTestCase {}
// MARK: -
fileprivate struct Bar: ParsableArguments {
@Option() var name: String
@Option(name: [.short, .long]) var format: String
}
extension ErrorMessageTests {
func testMissing_1() {
AssertErrorMessage(Bar.self, [], "Missing expected argument '--name <name>'")
}
func testMissing_2() {
AssertErrorMessage(Bar.self, ["--name", "a"], "Missing expected argument '--format <format>'")
}
func testUnknownOption_1() {
AssertErrorMessage(Bar.self, ["--name", "a", "--format", "b", "--verbose"], "Unknown option '--verbose'")
}
func testUnknownOption_2() {
AssertErrorMessage(Bar.self, ["--name", "a", "--format", "b", "-q"], "Unknown option '-q'")
}
func testUnknownOption_3() {
AssertErrorMessage(Bar.self, ["--name", "a", "--format", "b", "-bar"], "Unknown option '-bar'")
}
func testUnknownOption_4() {
AssertErrorMessage(Bar.self, ["--name", "a", "-foz", "b"], "Unknown option '-o'")
}
func testMissingValue_1() {
AssertErrorMessage(Bar.self, ["--name", "a", "--format"], "Missing value for '--format <format>'")
}
func testMissingValue_2() {
AssertErrorMessage(Bar.self, ["--name", "a", "-f"], "Missing value for '-f <format>'")
}
func testUnusedValue_1() {
AssertErrorMessage(Bar.self, ["--name", "a", "--format", "f", "b"], "Unexpected argument 'b'")
}
func testUnusedValue_2() {
AssertErrorMessage(Bar.self, ["--name", "a", "--format", "f", "b", "baz"], "2 unexpected arguments: 'b', 'baz'")
}
}
fileprivate enum Format: String, Equatable, Decodable, ExpressibleByArgument, CaseIterable {
case text
case json
case csv
}
fileprivate enum Name: String, Equatable, Decodable, ExpressibleByArgument, CaseIterable {
case bruce
case clint
case hulk
case natasha
case steve
case thor
case tony
}
fileprivate struct Foo: ParsableArguments {
@Option(name: [.short, .long])
var format: Format
@Option(name: [.short, .long])
var name: Name?
}
fileprivate struct EnumWithFewCasesArrayArgument: ParsableArguments {
@Argument
var formats: [Format]
}
fileprivate struct EnumWithManyCasesArrayArgument: ParsableArguments {
@Argument
var names: [Name]
}
extension ErrorMessageTests {
func testWrongEnumValue() {
AssertErrorMessage(Foo.self, ["--format", "png"], "The value 'png' is invalid for '--format <format>'. Please provide one of 'text', 'json' or 'csv'.")
AssertErrorMessage(Foo.self, ["-f", "png"], "The value 'png' is invalid for '-f <format>'. Please provide one of 'text', 'json' or 'csv'.")
AssertErrorMessage(Foo.self, ["-f", "text", "--name", "loki"],
"""
The value 'loki' is invalid for '--name <name>'. Please provide one of the following:
- bruce
- clint
- hulk
- natasha
- steve
- thor
- tony
""")
AssertErrorMessage(Foo.self, ["-f", "text", "-n", "loki"],
"""
The value 'loki' is invalid for '-n <name>'. Please provide one of the following:
- bruce
- clint
- hulk
- natasha
- steve
- thor
- tony
""")
AssertErrorMessage(EnumWithFewCasesArrayArgument.self, ["png"], "The value 'png' is invalid for '<formats>'. Please provide one of 'text', 'json' or 'csv'.")
AssertErrorMessage(EnumWithManyCasesArrayArgument.self, ["loki"],
"""
The value 'loki' is invalid for '<names>'. Please provide one of the following:
- bruce
- clint
- hulk
- natasha
- steve
- thor
- tony
""")
}
}
fileprivate struct Baz: ParsableArguments {
@Flag
var verbose: Bool = false
}
extension ErrorMessageTests {
func testUnexpectedValue() {
AssertErrorMessage(Baz.self, ["--verbose=foo"], "The option '--verbose' does not take any value, but 'foo' was specified.")
}
}
fileprivate struct Qux: ParsableArguments {
@Argument()
var firstNumber: Int
@Option(name: .customLong("number-two"))
var secondNumber: Int
}
extension ErrorMessageTests {
func testMissingArgument() {
AssertErrorMessage(Qux.self, ["--number-two", "2"], "Missing expected argument '<first-number>'")
}
func testInvalidNumber() {
AssertErrorMessage(Qux.self, ["--number-two", "2", "a"], "The value 'a' is invalid for '<first-number>'")
AssertErrorMessage(Qux.self, ["--number-two", "a", "1"], "The value 'a' is invalid for '--number-two <number-two>'")
}
}
fileprivate struct Qwz: ParsableArguments {
@Option() var name: String?
@Option(name: [.customLong("title", withSingleDash: true)]) var title: String?
}
extension ErrorMessageTests {
func testMispelledArgument_1() {
AssertErrorMessage(Qwz.self, ["--nme"], "Unknown option '--nme'. Did you mean '--name'?")
AssertErrorMessage(Qwz.self, ["-name"], "Unknown option '-name'. Did you mean '--name'?")
}
func testMispelledArgument_2() {
AssertErrorMessage(Qwz.self, ["-ttle"], "Unknown option '-ttle'. Did you mean '-title'?")
AssertErrorMessage(Qwz.self, ["--title"], "Unknown option '--title'. Did you mean '-title'?")
}
func testMispelledArgument_3() {
AssertErrorMessage(Qwz.self, ["--not-similar"], "Unknown option '--not-similar'")
}
func testMispelledArgument_4() {
AssertErrorMessage(Qwz.self, ["-x"], "Unknown option '-x'")
}
}
private struct Options: ParsableArguments {
enum OutputBehaviour: String, EnumerableFlag {
case stats, count, list
static func name(for value: OutputBehaviour) -> NameSpecification {
.shortAndLong
}
}
@Flag(help: "Program output")
var behaviour: OutputBehaviour = .list
@Flag(inversion: .prefixedNo, exclusivity: .exclusive) var bool: Bool
}
private struct OptOptions: ParsableArguments {
enum OutputBehaviour: String, EnumerableFlag {
case stats, count, list
static func name(for value: OutputBehaviour) -> NameSpecification {
.short
}
}
@Flag(help: "Program output")
var behaviour: OutputBehaviour?
}
extension ErrorMessageTests {
func testDuplicateFlags() {
AssertErrorMessage(Options.self, ["--list", "--bool", "-s"], "Value to be set with flag \'-s\' had already been set with flag \'--list\'")
AssertErrorMessage(Options.self, ["-cbl"], "Value to be set with flag \'l\' in \'-cbl\' had already been set with flag \'c\' in \'-cbl\'")
AssertErrorMessage(Options.self, ["-bc", "--stats", "-l"], "Value to be set with flag \'--stats\' had already been set with flag \'c\' in \'-bc\'")
AssertErrorMessage(Options.self, ["--no-bool", "--bool"], "Value to be set with flag \'--bool\' had already been set with flag \'--no-bool\'")
AssertErrorMessage(OptOptions.self, ["-cbl"], "Value to be set with flag \'l\' in \'-cbl\' had already been set with flag \'c\' in \'-cbl\'")
}
}
// (see issue #434).
private struct EmptyArray: ParsableArguments {
@Option(parsing: .upToNextOption)
var array: [String] = []
@Flag(name: [.short, .long])
var verbose = false
}
extension ErrorMessageTests {
func testEmptyArrayOption() {
AssertErrorMessage(EmptyArray.self, ["--array"], "Missing value for '--array <array>'")
AssertErrorMessage(EmptyArray.self, ["--array", "--verbose"], "Missing value for '--array <array>'")
AssertErrorMessage(EmptyArray.self, ["-verbose", "--array"], "Missing value for '--array <array>'")
AssertErrorMessage(EmptyArray.self, ["--array", "-v"], "Missing value for '--array <array>'")
AssertErrorMessage(EmptyArray.self, ["-v", "--array"], "Missing value for '--array <array>'")
}
}
// MARK: -
fileprivate struct Repeat: ParsableArguments {
@Option() var count: Int?
@Argument() var phrase: String
}
extension ErrorMessageTests {
func testBadOptionBeforeArgument() {
AssertErrorMessage(
Repeat.self,
["--cont", "5", "Hello"],
"Unknown option '--cont'. Did you mean '--count'?")
}
}