Accessing information about the call site of a function is surprisingly simple in Swift by simply using #file, #function and #line. This can be a great help when we want to put assertions within a shared function or print text without losing the original context. 🙌

extension  XCTestCase {
    struct UnexpectedNilError: Error {}

    func unwrapAssert<ValueT>(_ value: ValueT?,
                              message: String = "Unexpected nil",
                              file: StaticString = #file, 
                              line: UInt = #line) throws -> ValueT {
        guard let value = value else {
            XCTFail(message, file: file, line: line)
            throw  UnexpectedNilError()
        }
        return value
    }
}

Thanks for reading and happy coding! 🙏