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)
}
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.
I'm looking for a way to not having to pass in SourceLocation
manually from the calling site, like I could do with #file
and #line
in XCTAssert.
#line
and #file
etc are not exclusive to XCTest. They are macros, part of the Swift Standard Library. They are exactly what SourceLocation.init
uses as the default parameter values. You can do the same here.
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)
}