-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathMathExampleTests.swift
More file actions
292 lines (237 loc) · 9.19 KB
/
MathExampleTests.swift
File metadata and controls
292 lines (237 loc) · 9.19 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//===----------------------------------------------------------------------===//
//
// 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 ArgumentParserTestHelpers
import XCTest
@testable import ArgumentParser
final class MathExampleTests: XCTestCase {
override func setUp() {
Platform.Environment[.columns] = nil
}
func testMath_Simple() throws {
try AssertExecuteCommand(command: "math 1 2 3 4 5", expected: "15\n")
try AssertExecuteCommand(
command: "math multiply 1 2 3 4 5", expected: "120\n")
}
func testMath_Help() throws {
let helpText = """
OVERVIEW: A utility for performing maths.
USAGE: math <subcommand>
OPTIONS:
--version Show the version.
-h, --help Show help information.
SUBCOMMANDS:
add (default) Print the sum of the values.
multiply, mul Print the product of the values.
stats Calculate descriptive statistics.
See 'math help <subcommand>' for detailed help.
"""
try AssertExecuteCommand(command: "math -h", expected: helpText)
try AssertExecuteCommand(command: "math --help", expected: helpText)
try 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.
--version Show the version.
-h, --help Show help information.
"""
try AssertExecuteCommand(command: "math add -h", expected: helpText)
try AssertExecuteCommand(command: "math add --help", expected: helpText)
try AssertExecuteCommand(command: "math help add", expected: helpText)
// Verify that extra help flags are ignored.
try AssertExecuteCommand(command: "math help add -h", expected: helpText)
try AssertExecuteCommand(command: "math help add -help", expected: helpText)
try AssertExecuteCommand(
command: "math help add --help", 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. (values: mean,
median, mode; default: mean)
--version Show the version.
-h, --help Show help information.
"""
try AssertExecuteCommand(
command: "math stats average -h", expected: helpText)
try AssertExecuteCommand(
command: "math stats average --help", expected: helpText)
try 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> ...] [--file <file>] [--one-of-four <one-of-four>] [--custom-arg <custom-arg>] [--custom-deprecated-arg <custom-deprecated-arg>] [--shell <shell>] [--custom <custom>] [--custom-deprecated <custom-deprecated>]
ARGUMENTS:
<values> A group of floating-point values to operate on.
INPUT OPTIONS:
--one-of-four <one-of-four>
Choose one of four predefined options
--custom-arg <custom-arg>
Custom argument
--custom-deprecated-arg <custom-deprecated-arg>
Deprecated custom argument
SHELL OPTIONS:
--shell <shell> Run a shell command for input or completion
CUSTOM OPTIONS:
--custom <custom> Custom user-provided option with dynamic completion
--custom-deprecated <custom-deprecated>
Deprecated custom option
OPTIONS:
--file <file> Input file or directory to process (default section).
--version Show the version.
-h, --help Show help information.
"""
// The "quantiles" subcommand's run() method is unimplemented, so it
// just generates the help text.
try AssertExecuteCommand(
command: "math stats quantiles", expected: helpText)
try AssertExecuteCommand(
command: "math stats quantiles -h", expected: helpText)
try AssertExecuteCommand(
command: "math stats quantiles --help", expected: helpText)
try AssertExecuteCommand(
command: "math help stats quantiles", expected: helpText)
}
func testMath_CustomValidation() throws {
try AssertExecuteCommand(
command: "math stats average --kind mode",
expected: """
Error: Please provide at least one value to calculate the mode.
Usage: math stats average [--kind <kind>] [<values> ...]
See 'math stats average --help' for more information.
""",
exitCode: .validationFailure)
}
func testMath_Versions() throws {
try AssertExecuteCommand(
command: "math --version",
expected: "1.0.0\n")
try AssertExecuteCommand(
command: "math stats --version",
expected: "1.0.0\n")
try AssertExecuteCommand(
command: "math stats average --version",
expected: "1.5.0-alpha\n")
}
func testMath_ExitCodes() throws {
try AssertExecuteCommand(
command: "math stats quantiles --test-success-exit-code",
expected: "",
exitCode: .success)
try AssertExecuteCommand(
command: "math stats quantiles --test-failure-exit-code",
expected: "",
exitCode: .failure)
try AssertExecuteCommand(
command: "math stats quantiles --test-validation-exit-code",
expected: "",
exitCode: .validationFailure)
try AssertExecuteCommand(
command: "math stats quantiles --test-custom-exit-code 42",
expected: "",
exitCode: ExitCode(42))
}
func testMath_Fail() throws {
try AssertExecuteCommand(
command: "math --foo",
expected: """
Error: Unknown option '--foo'
Usage: math add [--hex-output] [<values> ...]
See 'math add --help' for more information.
""",
exitCode: .validationFailure)
try AssertExecuteCommand(
command: "math ZZZ",
expected: """
Error: The value 'ZZZ' is invalid for '<values>'
Help: <values> A group of integers to operate on.
Usage: math add [--hex-output] [<values> ...]
See 'math add --help' for more information.
""",
exitCode: .validationFailure)
}
}
// MARK: - Completion Script
// swift-format-ignore: AlwaysUseLowerCamelCase
// https://github.com/apple/swift-argument-parser/issues/710
extension MathExampleTests {
func testMathBashCompletionScript() throws {
let script = try AssertExecuteCommand(
command: "math --generate-completion-script bash")
try assertSnapshot(actual: script, extension: "bash")
}
func testMathZshCompletionScript() throws {
let script = try AssertExecuteCommand(
command: "math --generate-completion-script zsh")
try assertSnapshot(actual: script, extension: "zsh")
}
func testMathFishCompletionScript() throws {
let script = try AssertExecuteCommand(
command: "math --generate-completion-script fish")
try assertSnapshot(actual: script, extension: "fish")
}
func testMath_BashCustomCompletion() throws {
try testMath_CustomCompletion(forShell: .bash)
}
func testMath_FishCustomCompletion() throws {
try testMath_CustomCompletion(forShell: .fish)
}
func testMath_ZshCustomCompletion() throws {
try testMath_CustomCompletion(forShell: .zsh)
}
private func testMath_CustomCompletion(
forShell shell: CompletionShell
) throws {
try AssertExecuteCommand(
command: "math ---completion stats quantiles -- --custom 0 0",
expected: shell.format(completions: [
"hello",
"helicopter",
"heliotrope",
]) + "\n",
environment: [
Platform.Environment.Key.shellName.rawValue: shell.rawValue
]
)
try AssertExecuteCommand(
command: "math ---completion stats quantiles -- --custom 0 1 h",
expected: shell.format(completions: [
"hello",
"helicopter",
"heliotrope",
]) + "\n",
environment: [
Platform.Environment.Key.shellName.rawValue: shell.rawValue
]
)
try AssertExecuteCommand(
command: "math ---completion stats quantiles -- --custom 0 1 a",
expected: shell.format(completions: [
"aardvark",
"aaaaalbert",
]) + "\n",
environment: [
Platform.Environment.Key.shellName.rawValue: shell.rawValue
]
)
}
}