-
Notifications
You must be signed in to change notification settings - Fork 157
TaskLocal test trait #1764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbrandonw
wants to merge
10
commits into
swiftlang:main
Choose a base branch
from
pointfreeco:task-local-test-trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
TaskLocal test trait #1764
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e037bb3
Add '.taskLocal' test trait.
mbrandonw 57b7d2b
Change isRecursive to computed property
stephencelis 28ee525
Feedback
stephencelis 80c5ea8
Add to CMakeLists.txt
stephencelis 702dffd
Add test case
stephencelis 04abb70
More feedback
stephencelis ae13996
Capture value as an autoclosure.
mbrandonw b80766b
Add test for recursive functionality.
mbrandonw 335f8e5
Update docs
mbrandonw 094b12a
Sendable closure instead of concurrent.
mbrandonw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,55 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| extension Trait { | ||
| /// Constructs a trait that binds a task local value for the duration of a test | ||
| /// or suite. | ||
| /// | ||
| /// - Parameters: | ||
| /// - taskLocal: The task local to bind the value to. | ||
| /// - value: The value to bind to `taskLocal`. | ||
| /// | ||
| /// This value will only be evaluated if the test this | ||
| /// trait is applied to runs, and it will be unbound from | ||
| /// the task local and released once the trait is finished | ||
| /// providing scope for the test. | ||
| /// | ||
| /// - Note: You must define the task local outside the test target where the trait is used. | ||
| public static func taskLocal<Value: Sendable>( | ||
| _ taskLocal: TaskLocal<Value>, | ||
| _ value: @autoclosure @escaping @Sendable () throws -> Value | ||
| ) -> Self | ||
| where Self == TaskLocalTrait<Value> { | ||
|
grynspan marked this conversation as resolved.
|
||
| TaskLocalTrait(taskLocal: taskLocal, value: value) | ||
| } | ||
| } | ||
|
|
||
| /// A type that that binds a task local value for the duration of a test or suite. | ||
| /// | ||
| /// To add this trait to a test, use ``Trait/taskLocal(_:_:)``. | ||
| public struct TaskLocalTrait<Value: Sendable>: SuiteTrait, TestTrait, TestScoping { | ||
| /// This trait's task local. | ||
| fileprivate var taskLocal: TaskLocal<Value> | ||
|
|
||
| /// This trait's value. | ||
| fileprivate var value: @Sendable () throws -> Value | ||
|
|
||
| public var isRecursive: Bool { true } | ||
|
|
||
| public func provideScope( | ||
| for test: Test, | ||
| testCase: Test.Case?, | ||
| performing function: @Sendable () async throws -> Void | ||
| ) async throws { | ||
| try await taskLocal.withValue(value()) { | ||
| try await function() | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,41 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| @testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing | ||
|
|
||
| @Suite("Task Local Tests", .tags(.traitRelated)) | ||
| struct TaskLocalTests { | ||
| @Test( | ||
| ".taskLocal trait", | ||
| .taskLocal(local, true) | ||
| ) | ||
| func taskLocalBinding() throws { | ||
| #expect(local.wrappedValue == true) | ||
| } | ||
|
|
||
| @Suite(.serialized, .taskLocal(stateLocal, State())) struct MutableLocal { | ||
| @Test func run1() { | ||
| #expect(stateLocal.wrappedValue.count == 0) | ||
| stateLocal.wrappedValue.count += 1 | ||
| #expect(stateLocal.wrappedValue.count == 1) | ||
| } | ||
| @Test func run2() { | ||
| #expect(stateLocal.wrappedValue.count == 0) | ||
| stateLocal.wrappedValue.count += 1 | ||
| #expect(stateLocal.wrappedValue.count == 1) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private let local = TaskLocal(wrappedValue: false) | ||
| private class State: @unchecked Sendable { | ||
| var count = 0 | ||
| } | ||
| private let stateLocal = TaskLocal(wrappedValue: State()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.