Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native support for callback arguments #1

Open
danielr opened this issue Jun 3, 2019 · 0 comments
Open

Native support for callback arguments #1

danielr opened this issue Jun 3, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@danielr
Copy link
Owner

danielr commented Jun 3, 2019

Mocking functions with callbacks usually involves calling the callback with configurable arguments. It would be nice, if Mokka could directly support this.

Something like this could work:

public class CallbackFunctionMock<Args, CallbackArgs, CallbackReturnValue>: FunctionMock<Args> {

    var result: CallbackArgs?
    
    public func recordCall(_ args: Args, _ callback: (CallbackArgs) -> CallbackReturnValue) {
        super.recordCall(args)
        
        if let callbackArgs = result {
            _ = callback(callbackArgs)
        }
    }
}

This would then be used like this:

let doSomethingAsyncFunc = CallbackFunctionMock<String, Int, Void>(name: "doSomethingAsync(arg:completion:)")
func doSomethingAsync(arg: String, completion: (Int) -> Void) {
    doSomethingAsyncFunc.recordCall(arg, completion)
}

There are some open questions, though:

  • How to support callbacks with multiple arguments? (Is tuple splat still a thing, or has it been removed in some Swift version?)
  • How to support multiple callbacks?
  • What about functions that have a callback and a return value?
  • Can we somehow get rid of the requirement to separately specify the generic type parameters for the callback arguments and the callback return value? This is not very intuitive.
@danielr danielr added the enhancement New feature or request label Jun 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant