RollDiceExampleTests.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 RollDiceExampleTests: XCTestCase {
  15. override func setUp() {
  16. Platform.Environment[.columns] = nil
  17. }
  18. func testRollDice() throws {
  19. try AssertExecuteCommand(command: "roll --times 6")
  20. }
  21. func testRollDice_Help() throws {
  22. let helpText = """
  23. USAGE: roll [--times <n>] [--sides <m>] [--seed <seed>] [--verbose]
  24. OPTIONS:
  25. --times <n> Rolls the dice <n> times. (default: 1)
  26. --sides <m> Rolls an <m>-sided dice. (default: 6)
  27. Use this option to override the default value of a six-sided die.
  28. --seed <seed> A seed to use for repeatable random generation.
  29. -v, --verbose Show all roll results.
  30. -h, --help Show help information.
  31. """
  32. try AssertExecuteCommand(command: "roll -h", expected: helpText)
  33. try AssertExecuteCommand(command: "roll --help", expected: helpText)
  34. }
  35. func testRollDice_Fail() throws {
  36. try AssertExecuteCommand(
  37. command: "roll --times",
  38. expected: """
  39. Error: Missing value for '--times <n>'
  40. Help: --times <n> Rolls the dice <n> times.
  41. Usage: roll [--times <n>] [--sides <m>] [--seed <seed>] [--verbose]
  42. See 'roll --help' for more information.
  43. """,
  44. exitCode: .validationFailure)
  45. try AssertExecuteCommand(
  46. command: "roll --times ZZZ",
  47. expected: """
  48. Error: The value 'ZZZ' is invalid for '--times <n>'
  49. Help: --times <n> Rolls the dice <n> times.
  50. Usage: roll [--times <n>] [--sides <m>] [--seed <seed>] [--verbose]
  51. See 'roll --help' for more information.
  52. """,
  53. exitCode: .validationFailure)
  54. }
  55. }