Skip to content

Commit

Permalink
Make TestContext Equatable. (#128)
Browse files Browse the repository at this point in the history
* Make TestContext Equatable.

* Backwards compat

* wip

* wip

* fix?

* fix?

---------

Co-authored-by: Stephen Celis <[email protected]>
  • Loading branch information
mbrandonw and stephencelis authored Sep 10, 2024
1 parent d713332 commit 3fcc3f2
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions Sources/IssueReporting/TestContext.swift
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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``.
Expand All @@ -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 {
Expand Down

0 comments on commit 3fcc3f2

Please sign in to comment.