Skip to content
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

Make TestContext Equatable. #128

Merged
merged 7 commits into from
Sep 10, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions Sources/IssueReporting/TestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// Testing framework, or Xcode's XCTest framework.
public enum TestContext {
/// 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 @@ -27,8 +29,15 @@ public enum TestContext {
return .xcTest
}
}

/// 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 {
public struct Testing: Equatable {
public let test: Test

public struct Test: Hashable, Identifiable, Sendable {
Expand All @@ -46,3 +55,23 @@ extension TestContext.Testing {
self.init(test: Test(id: Test.ID(rawValue: id)))
}
}

extension TestContext: Equatable {
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
default:
return false
}
}

@available(*, deprecated, message: "Test for '.swiftTesting' using pattern matching, instead.")
public static var swiftTesting: Self {
.swiftTesting(nil)
}
}
Loading