Skip to content

Commit d713332

Browse files
authored
Add Test.Case.isParameterized to Testing context (#127)
1 parent b41188c commit d713332

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

Sources/IssueReporting/Internal/SwiftTesting.swift

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,19 @@ func _withKnownIssue(
234234
await withKnownIssue(message, isIntermittent, fileID, filePath, line, column, body)
235235
}
236236
@usableFromInline
237-
func _currentTestID() -> AnyHashable? {
238-
guard let function = function(for: "$s25IssueReportingTestSupport08_currentC2IDypyF")
237+
func _currentTestData() -> (id: AnyHashable, isParameterized: Bool)? {
238+
guard let function = function(for: "$s25IssueReportingTestSupport08_currentC4DataypyF")
239239
else {
240240
#if DEBUG
241-
return Test.current?.id
241+
guard let id = Test.current?.id, let isParameterized = Test.Case.current?.isParameterized
242+
else { return nil }
243+
return (id, isParameterized)
242244
#else
243245
return nil
244246
#endif
245247
}
246248

247-
return (function as! @Sendable () -> AnyHashable?)()
249+
return (function as! @Sendable () -> (id: AnyHashable, isParameterized: Bool)?)()
248250
}
249251

250252
#if DEBUG
@@ -352,8 +354,8 @@ func _currentTestID() -> AnyHashable? {
352354
}
353355
}
354356

355-
struct Test: @unchecked Sendable {
356-
static var current: Self? {
357+
private struct Test: @unchecked Sendable {
358+
fileprivate static var current: Self? {
357359
guard
358360
let current = unsafeBitCast(
359361
symbol: "$s7Testing4TestV7currentACSgvgZ",
@@ -364,7 +366,30 @@ func _currentTestID() -> AnyHashable? {
364366
return current()
365367
}
366368

367-
struct Case {}
369+
fileprivate struct Case {
370+
static var current: Self? {
371+
guard
372+
let current = unsafeBitCast(
373+
symbol: "$s7Testing4TestV4CaseV7currentAESgvgZ",
374+
in: "Testing",
375+
to: (@convention(thin) () -> Test.Case?).self
376+
)
377+
else { return nil }
378+
return current()
379+
}
380+
381+
private var arguments: [Argument]
382+
private var body: @Sendable () async throws -> Void
383+
384+
fileprivate var isParameterized: Bool {
385+
!arguments.isEmpty
386+
}
387+
388+
private struct Argument: Sendable {
389+
var value: any Sendable
390+
var parameter: Parameter
391+
}
392+
}
368393
private var name: String
369394
private var displayName: String?
370395
private var traits: [any Trait]

Sources/IssueReporting/TestContext.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public enum TestContext {
2121
/// If executed outside of a test process, this will return `nil`.
2222
public static var current: Self? {
2323
guard isTesting else { return nil }
24-
if let currentTestID = _currentTestID() {
25-
return .swiftTesting(Testing(id: currentTestID))
24+
if case let (id, isParameterized)? = _currentTestData() {
25+
return .swiftTesting(Testing(id: id, isParameterized: isParameterized))
2626
} else {
2727
return .xcTest
2828
}
@@ -31,9 +31,13 @@ public enum TestContext {
3131
public struct Testing {
3232
public let test: Test
3333

34-
public struct Test: Hashable, Identifiable, Sendable {
34+
public struct Test: Identifiable {
3535
public let id: ID
36+
public let `case`: Test.Case
3637

38+
public struct Case {
39+
public let isParameterized: Bool
40+
}
3741
public struct ID: Hashable, @unchecked Sendable {
3842
fileprivate let rawValue: AnyHashable
3943
}
@@ -42,7 +46,12 @@ public enum TestContext {
4246
}
4347

4448
extension TestContext.Testing {
45-
fileprivate init(id: AnyHashable) {
46-
self.init(test: Test(id: Test.ID(rawValue: id)))
49+
fileprivate init(id: AnyHashable, isParameterized: Bool) {
50+
self.init(
51+
test: Test(
52+
id: Test.ID(rawValue: id),
53+
case: Test.Case(isParameterized: isParameterized)
54+
)
55+
)
4756
}
4857
}

Sources/IssueReportingTestSupport/SwiftTesting.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ private func __withKnownIssueAsync(
100100
#endif
101101
}
102102

103-
public func _currentTestID() -> Any { __currentTestID }
103+
public func _currentTestData() -> Any { __currentTestData }
104104
@Sendable
105-
private func __currentTestID() -> AnyHashable? {
105+
private func __currentTestData() -> (id: AnyHashable, isParameterized: Bool)? {
106106
#if canImport(Testing)
107-
return Test.current?.id
107+
guard let id = Test.current?.id, let isParameterized = Test.Case.current?.isParameterized
108+
else { return nil }
109+
return (id, isParameterized)
108110
#else
109111
return nil
110112
#endif

0 commit comments

Comments
 (0)