I have a helper function that looks something like this:
private func test(state: State, against expectedState: State, sourceLocation: SourceLocation) {
// a bunch of irrelevant checks happening here
#expect(something == somethingElse, sourceLocation: SourceLocation)
}
Works like a charm. Great work! But:
XCTAssert & co have the #line and #file values that will set them with default values if you call them. This helps me finding the actual test that failed while calling a helper function in a unit test, instead of all of my tests failing inside of the helper function.
I tried using private func test(state: State, against expectedState: State, sourceLocation = SourceLocation()) { but that only highlights the function definition line where I create the instance.
Even if #line still seems to be around, there doesn't seem to be a way to create a SourceLocation manually either.
Is this still work in progress or I simply missed something?
I do find references like sourceLocation: SourceLocation = #_sourceLocation but that macro does not work for me.
1 Like
grynspan
(Jonathan Grynspan)
2
If you want to create an instance of SourceLocation manually, you can do so with:
let sourceLocation = SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column)
Where the arguments are previously-captured values for #fileID etc.
We only recently merged this change and have not released a tag including it yet. I expect to tag an update to Swift Testing that includes this change in a few weeks (unless a colleague does so first.) A recent Swift compiler is required because this change is dependent on SE-0422.
1 Like
Yes, tested this and it works:
private func test(
state: State,
against expectedState: State,
fileID: String = #fileID,
filePath: String = #filePath,
line: Int = #line,
column: Int = #column
) {
// a bunch of irrelevant checks happening here
let sourceLocation = SourceLocation(fileID: fileID, filePath: filePath, line: line, column: column)
#expect(something == somethingElse, sourceLocation: sourceLocation)
}
1 Like