1
1
/// A type representing the context in which a test is being run, _i.e._ either in Swift's native
2
2
/// Testing framework, or Xcode's XCTest framework.
3
- public enum TestContext {
3
+ public enum TestContext : Equatable {
4
4
/// The Swift Testing framework.
5
- case swiftTesting( Testing )
5
+ case swiftTesting( Testing ? )
6
6
7
7
/// The XCTest framework.
8
8
case xcTest
@@ -12,8 +12,10 @@ public enum TestContext {
12
12
/// How the test context is detected depends on the framework:
13
13
///
14
14
/// * If Swift Testing is running, _and_ this is called from the current test's task, this will
15
- /// return ``swiftTesting``. In this way, `TestContext.current == .swiftTesting` is equivalent
16
- /// to checking `Test.current != nil`, but safe to do from library and application code.
15
+ /// return ``swiftTesting`` with an associated value of the current test. You can invoke
16
+ /// ``isSwiftTesting`` to detect if the test is currently in the Swift Testing framework,
17
+ /// which is equivalent to checking `Test.current != nil`, but safe to do from library and
18
+ /// application code.
17
19
///
18
20
/// * If XCTest is running, _and_ this is called during the execution of a test _regardless_ of
19
21
/// task, this will return ``xcTest``.
@@ -28,21 +30,50 @@ public enum TestContext {
28
30
}
29
31
}
30
32
31
- public struct Testing {
33
+ /// Determines if the test context is Swift's native Testing framework.
34
+ public var isSwiftTesting : Bool {
35
+ guard case . swiftTesting = self
36
+ else { return false }
37
+ return true
38
+ }
39
+
40
+ public struct Testing : Equatable {
32
41
public let test : Test
33
42
34
- public struct Test : Identifiable {
43
+ public struct Test : Equatable , Hashable , Identifiable , Sendable {
35
44
public let id : ID
36
45
public let `case` : Test . Case
37
46
38
- public struct Case {
47
+ public struct Case : Equatable , Hashable , Sendable {
39
48
public let isParameterized : Bool
40
49
}
41
- public struct ID : Hashable , @unchecked Sendable {
50
+ public struct ID : Equatable , Hashable , @unchecked Sendable {
42
51
fileprivate let rawValue : AnyHashable
43
52
}
44
53
}
45
54
}
55
+
56
+ @available ( * , deprecated, message: " Test using pattern matching, instead. " )
57
+ public static func == ( lhs: Self , rhs: Self ) -> Bool {
58
+ switch ( lhs, rhs) {
59
+ case ( . swiftTesting( nil ) , . swiftTesting) ,
60
+ ( . swiftTesting, . swiftTesting( nil ) ) ,
61
+ ( . xcTest, . xcTest) :
62
+ return true
63
+ case ( . swiftTesting( let lhs) , . swiftTesting( let rhs) ) :
64
+ return lhs == rhs
65
+ case ( . swiftTesting, . xcTest) , ( . xcTest, . swiftTesting) :
66
+ return false
67
+ }
68
+ }
69
+
70
+ @available (
71
+ * , deprecated,
72
+ message: " Test for '.swiftTesting' using pattern matching or 'isSwiftTesting', instead. "
73
+ )
74
+ public static var swiftTesting : Self {
75
+ . swiftTesting( nil )
76
+ }
46
77
}
47
78
48
79
extension TestContext . Testing {
0 commit comments