Skip to content

Instantly share code, notes, and snippets.

@Kdan
Created July 5, 2020 20:08
Show Gist options
  • Select an option

  • Save Kdan/e0bbfcb90e4a6dc6a0901354bfd24b88 to your computer and use it in GitHub Desktop.

Select an option

Save Kdan/e0bbfcb90e4a6dc6a0901354bfd24b88 to your computer and use it in GitHub Desktop.
protocol MockType {
/// The log storing function calls.
var log: FunctionLogger { get }
}
extension MockType {
/// A Bool indicating if the class has received any function calls.
var hasReceivedCalls: Bool { log.hasReceivedCalls }
/// Retrieve the potential calls for a given function name.
/// - Parameters:
/// - name: The name of the function to retrieve calls for.
/// - Returns: The optional `FunctionCall` for the name provided.
func calls(with name: FunctionName) -> FunctionCall? { log[name] }
/// Log a function call with the given name and parameters.
/// - Parameters:
/// - name: The name of the function to log.
/// - parameters: The list of parameters to add to the log.
func logFunctionCall(with name: String = #function, parameters: [Any?] = []) {
log.insert(with: name, parameters: parameters)
}
/// Log a function call with the given name and parameters.
/// - Parameters:
/// - name: The name of the function to log.
/// - parameters: The list of parameters to add to the log.
/// - response: The response to return after logging the function call.
func logAndReturn<T>(with name: String = #function, parameters: [Any?] = [], _ response: T) -> T {
logFunctionCall(with: name, parameters: parameters)
return response
}
/// Reset the function logs.
func resetFunctionLog() { log.removeAll() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment