Skip to content

Instantly share code, notes, and snippets.

@Kdan
Last active January 26, 2021 14:24
Show Gist options
  • Select an option

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

Select an option

Save Kdan/65be1302cf689bf1a51a462ccb9c0d5a to your computer and use it in GitHub Desktop.
class Mock<T: RawRepresentable> where T.RawValue == FunctionName {
let log = FunctionLogger<T>()
/// 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: T) -> FunctionCall? { log[name] }
/// Log a function call with the given name and parameters.
/// - Parameters:
/// - name: The name of the function to log.
/// - parameters: The parameters to add to the log. Preferably in the format of a tuple.
func log(with name: FunctionName = #function, parameters: Any? = nil) {
log.insert(with: name, parameters: parameters)
}
/// Log a function call with the given name and parameters and return the given response.
/// - Parameters:
/// - name: The name of the function to log.
/// - parameters: The parameters to add to the log. Preferably in the format of a tuple.
/// - response: The response to return after logging the function call.
func log<T>(with name: FunctionName = #function, parameters: Any? = nil, return response: T) -> T {
log.insert(with: name, parameters: parameters)
return response
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment