-
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
base: main
Are you sure you want to change the base?
TaskLocal test trait #1764
Changes from 7 commits
e037bb3
57b7d2b
28ee525
80c5ea8
702dffd
04abb70
ae13996
b80766b
335f8e5
094b12a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // | ||
| // 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 set. | ||
| /// | ||
| /// ```swift | ||
|
mbrandonw marked this conversation as resolved.
Outdated
|
||
| /// @Suite(.taskLocal($myValue, 42)) | ||
| /// struct MyTests { | ||
| /// // ... | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// - 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 func provideScope( | ||
| for test: Test, | ||
| testCase: Test.Case?, | ||
| performing function: @concurrent () async throws -> Void | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is |
||
| ) async throws { | ||
| try await taskLocal.withValue(value(), operation: function) | ||
| } | ||
| } | ||
|
|
||
| #if DEBUG | ||
| @TaskLocal var dummyLocal = false | ||
|
stmontgomery marked this conversation as resolved.
Outdated
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // | ||
| // 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($dummyLocal, true) | ||
| ) | ||
| func taskLocalBinding() throws { | ||
| #expect(dummyLocal == true) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.