Code Monkey home page Code Monkey logo

Comments (9)

lupuuss avatar lupuuss commented on August 14, 2024

This and other ...By are analogous to returnsBy, which accepts () -> T on purpose, so it's possible to use property/function reference.

from mokkery.

dalewking avatar dalewking commented on August 14, 2024

The two are not mutually exclusive

from mokkery.

lupuuss avatar lupuuss commented on August 14, 2024

I am not sure what you mean. Could you elaborate?

from mokkery.

dalewking avatar dalewking commented on August 14, 2024

You can have an overloaded version that is more like calls. To get around this I have defined a function like this:

    infix fun <T> SuspendAnsweringScope<Result<T>>.returnsSuccessBy(
        block: (CallArgs) -> T,
    ) {
        calls {
            try { success(block(it)) } catch (t: Throwable) { failure(t) }
        }
    }

from mokkery.

lupuuss avatar lupuuss commented on August 14, 2024

I don't really like adding this overload:

  1. It's ambiguous. To resolve it, you're forced to use a function reference or specify the lambda parameter explicitly.
  2. The distinction between returns..By and calls becomes less clear. I see ...By answers as simple utilities for working with properties (especially) or functions without accessing CallArgs.

However, I do see a blind spot in the API - a missing utility that simplifies working with results while still providing full access to the mocked method context. To address this gap, I propose:

public infix fun <T> BlockingAnsweringScope<Result<T>>.callsCatching(block: BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T) 
public infix fun <T> SuspendAnsweringScope<Result<T>>.callsCatching(block: suspend SuspendCallDefinitionScope<Result<T>>.(CallArgs) -> T)
  1. It has stronger name association with calls.
  2. Clear intent - the name also relates to runCatching, clearly explaining what's happening.
  3. It provides access to other mocked method context operations.

from mokkery.

dalewking avatar dalewking commented on August 14, 2024

Works for me. Awaiting this feature. I was trying to implement it myself, but I don't think it is possible outside the library.

from mokkery.

lupuuss avatar lupuuss commented on August 14, 2024

You can temporarily add this:

public infix fun <T> BlockingAnsweringScope<Result<T>>.callsCatching(block: BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T) {
    calls { runCatching { block(it) } }
}

It's not perfect as the Answer.description returns calls {...} instead of callsCatching {...}. It should not be a problem as this description is used only by debug utils. If it's problematic, implementing custom answers is fully possible (since 2.2.0 it's possible to instantiate BlockingCallDefinitionScope and SuspendCallDefinitionScope).

from mokkery.

dalewking avatar dalewking commented on August 14, 2024

You can temporarily add this:

public infix fun <T> BlockingAnsweringScope<Result<T>>.callsCatching(block: BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T) {
    calls { runCatching { block(it) } }
}

It's not perfect as the Answer.description returns calls {...} instead of callsCatching {...}. It should not be a problem as this description is used only by debug utils. If it's problematic, implementing custom answers is fully possible (since 2.2.0 it's possible to instantiate BlockingCallDefinitionScope and SuspendCallDefinitionScope).

That doesn't quite work as you want block to be of type BlockingCallDefinitionScope<T>.(CallArgs) -> T not BlockingCallDefinitionScope<Result<T>>.(CallArgs) -> T, i..e. it shoud return T not Result<T>

from mokkery.

lupuuss avatar lupuuss commented on August 14, 2024

It is correct. The T in CallDefinitionScope<T> indicates the return type of a mocked method, so it being Result<T> is correct. The only inconvenience is that callOriginal and callSuper are returning Result that requires unwrapping.

Also, it got me thinking about the other issue. callOriginal and callSuper might fail and probably MokkeryRuntimeExceptions should not be caught with callsCatching

from mokkery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.