diff --git a/Sources/IssueReporting/TestContext.swift b/Sources/IssueReporting/TestContext.swift index c60986a..53a45cc 100644 --- a/Sources/IssueReporting/TestContext.swift +++ b/Sources/IssueReporting/TestContext.swift @@ -1,8 +1,8 @@ /// A type representing the context in which a test is being run, _i.e._ either in Swift's native /// Testing framework, or Xcode's XCTest framework. -public enum TestContext { +public enum TestContext: Equatable { /// The Swift Testing framework. - case swiftTesting(Testing) + case swiftTesting(Testing?) /// The XCTest framework. case xcTest @@ -12,8 +12,10 @@ public enum TestContext { /// How the test context is detected depends on the framework: /// /// * If Swift Testing is running, _and_ this is called from the current test's task, this will - /// return ``swiftTesting``. In this way, `TestContext.current == .swiftTesting` is equivalent - /// to checking `Test.current != nil`, but safe to do from library and application code. + /// return ``swiftTesting`` with an associated value of the current test. You can invoke + /// ``isSwiftTesting`` to detect if the test is currently in the Swift Testing framework, + /// which is equivalent to checking `Test.current != nil`, but safe to do from library and + /// application code. /// /// * If XCTest is running, _and_ this is called during the execution of a test _regardless_ of /// task, this will return ``xcTest``. @@ -28,21 +30,50 @@ public enum TestContext { } } - public struct Testing { + /// Determines if the test context is Swift's native Testing framework. + public var isSwiftTesting: Bool { + guard case .swiftTesting = self + else { return false } + return true + } + + public struct Testing: Equatable { public let test: Test - public struct Test: Identifiable { + public struct Test: Equatable, Hashable, Identifiable, Sendable { public let id: ID public let `case`: Test.Case - public struct Case { + public struct Case: Equatable, Hashable, Sendable { public let isParameterized: Bool } - public struct ID: Hashable, @unchecked Sendable { + public struct ID: Equatable, Hashable, @unchecked Sendable { fileprivate let rawValue: AnyHashable } } } + + @available(*, deprecated, message: "Test using pattern matching, instead.") + public static func == (lhs: Self, rhs: Self) -> Bool { + switch (lhs, rhs) { + case (.swiftTesting(nil), .swiftTesting), + (.swiftTesting, .swiftTesting(nil)), + (.xcTest, .xcTest): + return true + case (.swiftTesting(let lhs), .swiftTesting(let rhs)): + return lhs == rhs + case (.swiftTesting, .xcTest), (.xcTest, .swiftTesting): + return false + } + } + + @available( + *, deprecated, + message: "Test for '.swiftTesting' using pattern matching or 'isSwiftTesting', instead." + ) + public static var swiftTesting: Self { + .swiftTesting(nil) + } } extension TestContext.Testing {