MathExampleTests.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2020 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. import ArgumentParserTestHelpers
  12. import XCTest
  13. @testable import ArgumentParser
  14. final class MathExampleTests: XCTestCase {
  15. override func setUp() {
  16. Platform.Environment[.columns] = nil
  17. }
  18. func testMath_Simple() throws {
  19. try AssertExecuteCommand(command: "math 1 2 3 4 5", expected: "15\n")
  20. try AssertExecuteCommand(
  21. command: "math multiply 1 2 3 4 5", expected: "120\n")
  22. }
  23. func testMath_Help() throws {
  24. let helpText = """
  25. OVERVIEW: A utility for performing maths.
  26. USAGE: math <subcommand>
  27. OPTIONS:
  28. --version Show the version.
  29. -h, --help Show help information.
  30. SUBCOMMANDS:
  31. add (default) Print the sum of the values.
  32. multiply, mul Print the product of the values.
  33. stats Calculate descriptive statistics.
  34. See 'math help <subcommand>' for detailed help.
  35. """
  36. try AssertExecuteCommand(command: "math -h", expected: helpText)
  37. try AssertExecuteCommand(command: "math --help", expected: helpText)
  38. try AssertExecuteCommand(command: "math help", expected: helpText)
  39. }
  40. func testMath_AddHelp() throws {
  41. let helpText = """
  42. OVERVIEW: Print the sum of the values.
  43. USAGE: math add [--hex-output] [<values> ...]
  44. ARGUMENTS:
  45. <values> A group of integers to operate on.
  46. OPTIONS:
  47. -x, --hex-output Use hexadecimal notation for the result.
  48. --version Show the version.
  49. -h, --help Show help information.
  50. """
  51. try AssertExecuteCommand(command: "math add -h", expected: helpText)
  52. try AssertExecuteCommand(command: "math add --help", expected: helpText)
  53. try AssertExecuteCommand(command: "math help add", expected: helpText)
  54. // Verify that extra help flags are ignored.
  55. try AssertExecuteCommand(command: "math help add -h", expected: helpText)
  56. try AssertExecuteCommand(command: "math help add -help", expected: helpText)
  57. try AssertExecuteCommand(
  58. command: "math help add --help", expected: helpText)
  59. }
  60. func testMath_StatsMeanHelp() throws {
  61. let helpText = """
  62. OVERVIEW: Print the average of the values.
  63. USAGE: math stats average [--kind <kind>] [<values> ...]
  64. ARGUMENTS:
  65. <values> A group of floating-point values to operate on.
  66. OPTIONS:
  67. --kind <kind> The kind of average to provide. (values: mean,
  68. median, mode; default: mean)
  69. --version Show the version.
  70. -h, --help Show help information.
  71. """
  72. try AssertExecuteCommand(
  73. command: "math stats average -h", expected: helpText)
  74. try AssertExecuteCommand(
  75. command: "math stats average --help", expected: helpText)
  76. try AssertExecuteCommand(
  77. command: "math help stats average", expected: helpText)
  78. }
  79. func testMath_StatsQuantilesHelp() throws {
  80. let helpText = """
  81. OVERVIEW: Print the quantiles of the values (TBD).
  82. USAGE: math stats quantiles [<one-of-four>] [<custom-arg>] [<custom-deprecated-arg>] [<values> ...] [--file <file>] [--directory <directory>] [--shell <shell>] [--custom <custom>] [--custom-deprecated <custom-deprecated>]
  83. ARGUMENTS:
  84. <one-of-four>
  85. <custom-arg>
  86. <custom-deprecated-arg>
  87. <values> A group of floating-point values to operate on.
  88. OPTIONS:
  89. --file <file>
  90. --directory <directory>
  91. --shell <shell>
  92. --custom <custom>
  93. --custom-deprecated <custom-deprecated>
  94. --version Show the version.
  95. -h, --help Show help information.
  96. """
  97. // The "quantiles" subcommand's run() method is unimplemented, so it
  98. // just generates the help text.
  99. try AssertExecuteCommand(
  100. command: "math stats quantiles", expected: helpText)
  101. try AssertExecuteCommand(
  102. command: "math stats quantiles -h", expected: helpText)
  103. try AssertExecuteCommand(
  104. command: "math stats quantiles --help", expected: helpText)
  105. try AssertExecuteCommand(
  106. command: "math help stats quantiles", expected: helpText)
  107. }
  108. func testMath_CustomValidation() throws {
  109. try AssertExecuteCommand(
  110. command: "math stats average --kind mode",
  111. expected: """
  112. Error: Please provide at least one value to calculate the mode.
  113. Usage: math stats average [--kind <kind>] [<values> ...]
  114. See 'math stats average --help' for more information.
  115. """,
  116. exitCode: .validationFailure)
  117. }
  118. func testMath_Versions() throws {
  119. try AssertExecuteCommand(
  120. command: "math --version",
  121. expected: "1.0.0\n")
  122. try AssertExecuteCommand(
  123. command: "math stats --version",
  124. expected: "1.0.0\n")
  125. try AssertExecuteCommand(
  126. command: "math stats average --version",
  127. expected: "1.5.0-alpha\n")
  128. }
  129. func testMath_ExitCodes() throws {
  130. try AssertExecuteCommand(
  131. command: "math stats quantiles --test-success-exit-code",
  132. expected: "",
  133. exitCode: .success)
  134. try AssertExecuteCommand(
  135. command: "math stats quantiles --test-failure-exit-code",
  136. expected: "",
  137. exitCode: .failure)
  138. try AssertExecuteCommand(
  139. command: "math stats quantiles --test-validation-exit-code",
  140. expected: "",
  141. exitCode: .validationFailure)
  142. try AssertExecuteCommand(
  143. command: "math stats quantiles --test-custom-exit-code 42",
  144. expected: "",
  145. exitCode: ExitCode(42))
  146. }
  147. func testMath_Fail() throws {
  148. try AssertExecuteCommand(
  149. command: "math --foo",
  150. expected: """
  151. Error: Unknown option '--foo'
  152. Usage: math add [--hex-output] [<values> ...]
  153. See 'math add --help' for more information.
  154. """,
  155. exitCode: .validationFailure)
  156. try AssertExecuteCommand(
  157. command: "math ZZZ",
  158. expected: """
  159. Error: The value 'ZZZ' is invalid for '<values>'
  160. Help: <values> A group of integers to operate on.
  161. Usage: math add [--hex-output] [<values> ...]
  162. See 'math add --help' for more information.
  163. """,
  164. exitCode: .validationFailure)
  165. }
  166. }
  167. // MARK: - Completion Script
  168. // swift-format-ignore: AlwaysUseLowerCamelCase
  169. // https://github.com/apple/swift-argument-parser/issues/710
  170. extension MathExampleTests {
  171. func testMathBashCompletionScript() throws {
  172. let script = try AssertExecuteCommand(
  173. command: "math --generate-completion-script bash")
  174. try assertSnapshot(actual: script, extension: "bash")
  175. }
  176. func testMathZshCompletionScript() throws {
  177. let script = try AssertExecuteCommand(
  178. command: "math --generate-completion-script zsh")
  179. try assertSnapshot(actual: script, extension: "zsh")
  180. }
  181. func testMathFishCompletionScript() throws {
  182. let script = try AssertExecuteCommand(
  183. command: "math --generate-completion-script fish")
  184. try assertSnapshot(actual: script, extension: "fish")
  185. }
  186. func testMath_BashCustomCompletion() throws {
  187. try testMath_CustomCompletion(forShell: .bash)
  188. }
  189. func testMath_FishCustomCompletion() throws {
  190. try testMath_CustomCompletion(forShell: .fish)
  191. }
  192. func testMath_ZshCustomCompletion() throws {
  193. try testMath_CustomCompletion(forShell: .zsh)
  194. }
  195. private func testMath_CustomCompletion(
  196. forShell shell: CompletionShell
  197. ) throws {
  198. try AssertExecuteCommand(
  199. command: "math ---completion stats quantiles -- --custom 0 0",
  200. expected: shell.format(completions: [
  201. "hello",
  202. "helicopter",
  203. "heliotrope",
  204. ]) + "\n",
  205. environment: [
  206. Platform.Environment.Key.shellName.rawValue: shell.rawValue
  207. ]
  208. )
  209. try AssertExecuteCommand(
  210. command: "math ---completion stats quantiles -- --custom 0 1 h",
  211. expected: shell.format(completions: [
  212. "hello",
  213. "helicopter",
  214. "heliotrope",
  215. ]) + "\n",
  216. environment: [
  217. Platform.Environment.Key.shellName.rawValue: shell.rawValue
  218. ]
  219. )
  220. try AssertExecuteCommand(
  221. command: "math ---completion stats quantiles -- --custom 0 1 a",
  222. expected: shell.format(completions: [
  223. "aardvark",
  224. "aaaaalbert",
  225. ]) + "\n",
  226. environment: [
  227. Platform.Environment.Key.shellName.rawValue: shell.rawValue
  228. ]
  229. )
  230. }
  231. }