Skip to content

Commit 50843cb

Browse files
authored
Add XCTFailContext task local (#68)
* Add XCTFailContext task local * wip * wip
1 parent 245d527 commit 50843cb

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
run: make test-linux
3131

3232
wasm:
33-
name: SwiftWASM
33+
name: Wasm
3434
runs-on: ubuntu-latest
3535
strategy:
3636
matrix:

Sources/XCTestDynamicOverlay/XCTFail.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import Foundation
22

3+
public struct XCTFailContext {
4+
@TaskLocal public static var current: Self?
5+
6+
public var file: StaticString
7+
public var line: UInt
8+
9+
public init(file: StaticString, line: UInt) {
10+
self.file = file
11+
self.line = line
12+
}
13+
}
14+
315
#if DEBUG
416
#if canImport(ObjectiveC)
517
/// This function generates a failure immediately and unconditionally.
@@ -12,6 +24,10 @@ import Foundation
1224
/// results.
1325
@_disfavoredOverload
1426
public func XCTFail(_ message: String = "") {
27+
if let context = XCTFailContext.current {
28+
XCTFail(message, file: context.file, line: context.line)
29+
return
30+
}
1531
var message = message
1632
attachHostApplicationWarningIfNeeded(&message)
1733
guard
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#if !os(Linux)
2+
import XCTest
3+
import XCTestDynamicOverlay
4+
5+
public final class XCTContextTests: XCTestCase {
6+
func testContext() {
7+
XCTExpectFailure {
8+
$0.compactDescription == "Failed"
9+
&& $0.sourceCodeContext.location
10+
== XCTSourceCodeLocation(filePath: "unknown", lineNumber: 1)
11+
}
12+
XCTFailContext.$current.withValue(XCTFailContext(file: "unknown", line: 1)) {
13+
XCTestDynamicOverlay.XCTFail("Failed")
14+
}
15+
}
16+
}
17+
#endif

0 commit comments

Comments
 (0)