AsyncCommandEndToEndTests.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ArgumentParser
  12. import XCTest
  13. final class AsyncCommandEndToEndTests: XCTestCase {}
  14. actor AsyncStatusCheck {
  15. struct Status: OptionSet {
  16. var rawValue: UInt8
  17. static var root: Self { .init(rawValue: 1 << 0) }
  18. static var sub: Self { .init(rawValue: 1 << 1) }
  19. }
  20. @MainActor
  21. var status: Status = []
  22. @MainActor
  23. func update(_ status: Status) {
  24. self.status.insert(status)
  25. }
  26. }
  27. @MainActor
  28. var statusCheck = AsyncStatusCheck()
  29. // MARK: AsyncParsableCommand.main() testing
  30. struct AsyncCommand: AsyncParsableCommand {
  31. static var configuration: CommandConfiguration {
  32. .init(subcommands: [SubCommand.self])
  33. }
  34. func run() async throws {
  35. await statusCheck.update(.root)
  36. }
  37. struct SubCommand: AsyncParsableCommand {
  38. func run() async throws {
  39. await statusCheck.update(.sub)
  40. }
  41. }
  42. }
  43. // swift-format-ignore: AlwaysUseLowerCamelCase
  44. // https://github.com/apple/swift-argument-parser/issues/710
  45. extension AsyncCommandEndToEndTests {
  46. @MainActor
  47. func testAsyncMain_root() async throws {
  48. XCTAssertFalse(statusCheck.status.contains(.root))
  49. await AsyncCommand.main([])
  50. XCTAssertTrue(statusCheck.status.contains(.root))
  51. }
  52. @MainActor
  53. func testAsyncMain_sub() async throws {
  54. XCTAssertFalse(statusCheck.status.contains(.sub))
  55. await AsyncCommand.main(["sub-command"])
  56. XCTAssertTrue(statusCheck.status.contains(.sub))
  57. }
  58. }