1
0

RepeatExampleTests.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 RepeatExampleTests: XCTestCase {
  15. override func setUp() {
  16. Platform.Environment[.columns] = nil
  17. }
  18. func testRepeat() throws {
  19. try AssertExecuteCommand(
  20. command: "repeat hello",
  21. expected: """
  22. hello
  23. hello
  24. """)
  25. }
  26. func testRepeat_include_counter() throws {
  27. try AssertExecuteCommand(
  28. command: "repeat --include-counter hello",
  29. expected: """
  30. 1: hello
  31. 2: hello
  32. """)
  33. }
  34. func testRepeat_Count() throws {
  35. try AssertExecuteCommand(
  36. command: "repeat hello --count 6",
  37. expected: """
  38. hello
  39. hello
  40. hello
  41. hello
  42. hello
  43. hello
  44. """)
  45. }
  46. func testRepeat_Help() throws {
  47. let helpText = """
  48. USAGE: repeat [--count <count>] [--include-counter] <phrase>
  49. ARGUMENTS:
  50. <phrase> The phrase to repeat.
  51. OPTIONS:
  52. --count <count> How many times to repeat 'phrase'.
  53. --include-counter Include a counter with each repetition.
  54. -h, --help Show help information.
  55. """
  56. try AssertExecuteCommand(command: "repeat -h", expected: helpText)
  57. try AssertExecuteCommand(command: "repeat --help", expected: helpText)
  58. }
  59. func testRepeat_Fail() throws {
  60. try AssertExecuteCommand(
  61. command: "repeat",
  62. expected: """
  63. Error: Missing expected argument '<phrase>'
  64. USAGE: repeat [--count <count>] [--include-counter] <phrase>
  65. ARGUMENTS:
  66. <phrase> The phrase to repeat.
  67. OPTIONS:
  68. --count <count> How many times to repeat 'phrase'.
  69. --include-counter Include a counter with each repetition.
  70. -h, --help Show help information.
  71. """,
  72. exitCode: .validationFailure)
  73. try AssertExecuteCommand(
  74. command: "repeat hello --count",
  75. expected: """
  76. Error: Missing value for '--count <count>'
  77. Help: --count <count> How many times to repeat 'phrase'.
  78. Usage: repeat [--count <count>] [--include-counter] <phrase>
  79. See 'repeat --help' for more information.
  80. """,
  81. exitCode: .validationFailure)
  82. try AssertExecuteCommand(
  83. command: "repeat hello --count ZZZ",
  84. expected: """
  85. Error: The value 'ZZZ' is invalid for '--count <count>'
  86. Help: --count <count> How many times to repeat 'phrase'.
  87. Usage: repeat [--count <count>] [--include-counter] <phrase>
  88. See 'repeat --help' for more information.
  89. """,
  90. exitCode: .validationFailure)
  91. try AssertExecuteCommand(
  92. command: "repeat --version hello",
  93. expected: """
  94. Error: Unknown option '--version'
  95. Usage: repeat [--count <count>] [--include-counter] <phrase>
  96. See 'repeat --help' for more information.
  97. """,
  98. exitCode: .validationFailure)
  99. }
  100. }