forked from apple/swift-argument-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support passing arguments to async main (apple#568)
* Support passing arguments to async main * Add test for AsyncParsableCommand.main()
- Loading branch information
1 parent
1d191b0
commit 29b3d39
Showing
3 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Tests/ArgumentParserEndToEndTests/AsyncCommandEndToEndTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===----------------------------------------------------------*- swift -*-===// | ||
// | ||
// 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 XCTest | ||
import ArgumentParser | ||
|
||
final class AsyncCommandEndToEndTests: XCTestCase { | ||
} | ||
|
||
actor AsyncStatusCheck { | ||
struct Status: OptionSet { | ||
var rawValue: UInt8 | ||
|
||
static var root: Self { .init(rawValue: 1 << 0) } | ||
static var sub: Self { .init(rawValue: 1 << 1) } | ||
} | ||
|
||
@MainActor | ||
var status: Status = [] | ||
|
||
@MainActor | ||
func update(_ status: Status) { | ||
self.status.insert(status) | ||
} | ||
} | ||
|
||
var statusCheck = AsyncStatusCheck() | ||
|
||
// MARK: AsyncParsableCommand.main() testing | ||
|
||
struct AsyncCommand: AsyncParsableCommand { | ||
static var configuration: CommandConfiguration { | ||
.init(subcommands: [SubCommand.self]) | ||
} | ||
|
||
func run() async throws { | ||
await statusCheck.update(.root) | ||
} | ||
|
||
struct SubCommand: AsyncParsableCommand { | ||
func run() async throws { | ||
await statusCheck.update(.sub) | ||
} | ||
} | ||
} | ||
|
||
extension AsyncCommandEndToEndTests { | ||
@MainActor | ||
func testAsyncMain_root() async throws { | ||
XCTAssertFalse(statusCheck.status.contains(.root)) | ||
await AsyncCommand.main([]) | ||
XCTAssertTrue(statusCheck.status.contains(.root)) | ||
} | ||
|
||
@MainActor | ||
func testAsyncMain_sub() async throws { | ||
XCTAssertFalse(statusCheck.status.contains(.sub)) | ||
await AsyncCommand.main(["sub-command"]) | ||
XCTAssertTrue(statusCheck.status.contains(.sub)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters