Skip to content

Commit 3fcc3f2

Browse files
Make TestContext Equatable. (#128)
* Make TestContext Equatable. * Backwards compat * wip * wip * fix? * fix? --------- Co-authored-by: Stephen Celis <stephen@stephencelis.com>
1 parent d713332 commit 3fcc3f2

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

Sources/IssueReporting/TestContext.swift

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// A type representing the context in which a test is being run, _i.e._ either in Swift's native
22
/// Testing framework, or Xcode's XCTest framework.
3-
public enum TestContext {
3+
public enum TestContext: Equatable {
44
/// The Swift Testing framework.
5-
case swiftTesting(Testing)
5+
case swiftTesting(Testing?)
66

77
/// The XCTest framework.
88
case xcTest
@@ -12,8 +12,10 @@ public enum TestContext {
1212
/// How the test context is detected depends on the framework:
1313
///
1414
/// * 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.
1719
///
1820
/// * If XCTest is running, _and_ this is called during the execution of a test _regardless_ of
1921
/// task, this will return ``xcTest``.
@@ -28,21 +30,50 @@ public enum TestContext {
2830
}
2931
}
3032

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 {
3241
public let test: Test
3342

34-
public struct Test: Identifiable {
43+
public struct Test: Equatable, Hashable, Identifiable, Sendable {
3544
public let id: ID
3645
public let `case`: Test.Case
3746

38-
public struct Case {
47+
public struct Case: Equatable, Hashable, Sendable {
3948
public let isParameterized: Bool
4049
}
41-
public struct ID: Hashable, @unchecked Sendable {
50+
public struct ID: Equatable, Hashable, @unchecked Sendable {
4251
fileprivate let rawValue: AnyHashable
4352
}
4453
}
4554
}
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+
}
4677
}
4778

4879
extension TestContext.Testing {

0 commit comments

Comments
 (0)