Giter Site home page Giter Site logo

Comments (11)

rokridi avatar rokridi commented on June 12, 2024 1

Hello @art-divin

@pocketal and I are working on a fix for this issue.
We wonder if it is possible to add isVariadic information to closure parameter. We need that information to be able to handle variadic closure parameters.

from sourcery.

art-divin avatar art-divin commented on June 12, 2024 1

Hello @rokridi , @pocketal , this is very nice of you to invest your time into improving Sourcery 🤝

We wonder if it is possible to add isVariadic information to closure parameter

Let's see!

from sourcery.

art-divin avatar art-divin commented on June 12, 2024 1

@rokridi I would like to mention that acceptable implementation would be:

var totoArgsAnyStubWithSomeNameProtocolVoidCallsCount = 0
var totoArgsAnyStubWithSomeNameProtocolVoidCalled: Bool {
  return totoArgsAnyStubWithSomeNameProtocolVoidCallsCount > 0
}
var totoArgsAnyStubWithSomeNameProtocolVoidReceivedArgs: ([(any StubWithSomeNameProtocol)])?
var totoArgsAnyStubWithSomeNameProtocolVoidReceivedInvocations: [[(any StubWithSomeNameProtocol)]] = []
var totoArgsAnyStubWithSomeNameProtocolVoidClosure: (([(any StubWithSomeNameProtocol)]) -> Void)?

func toto(args: any StubWithSomeNameProtocol...) {
  totoArgsAnyStubWithSomeNameProtocolVoidCallsCount += 1
  totoArgsAnyStubWithSomeNameProtocolVoidReceivedArgs = args
  totoArgsAnyStubWithSomeNameProtocolVoidReceivedInvocations.append(args)
  totoArgsAnyStubWithSomeNameProtocolVoidClosure?(args)
}

i.e. keeping any and parentheses around the type, as opposed to the expected result in the OP.

from sourcery.

rokridi avatar rokridi commented on June 12, 2024 1

Here is what the Stencil could look like (AutoMockable.txt), however we did not succeed at handling functions having a parameter which is a closure having a variadic parameter.

Example:

func foo(closure: (String...) -> Void)

The corresponding mock:

    var func13ParamStringVoidVoidClosure: (((String) -> Void) -> Void)?
    func func13(param: (String) -> Void) {
        func13ParamStringVoidVoidCallsCount += 1
        func13ParamStringVoidVoidClosure?(param)
    }

Expected mock:

    var func13ParamStringVoidVoidClosure: (((String...) -> Void) -> Void)?
    func func13(param: (String...) -> Void) {
        func13ParamStringVoidVoidCallsCount += 1
        func13ParamStringVoidVoidClosure?(param)
    }

from sourcery.

art-divin avatar art-divin commented on June 12, 2024 1

I can pick it up from here, feel free to take a look at other, easy to reproduce bugs that are to be fixed for the next release:
https://github.com/krzysztofzablocki/Sourcery/milestone/6

from sourcery.

art-divin avatar art-divin commented on June 12, 2024

a1343ff introduces isVariadic member to ClosureParameter. I am adding UTs for it; in the meanwhile, you could take the leverage using that implementation already 👍🏻

Edit: since Swift itself does not support variadic arguments for closures, it is unreasonable to add such implementation detail.

from sourcery.

art-divin avatar art-divin commented on June 12, 2024

I am not 100% sure you actually need ClosureParameter.isVariadic, rather, try simply to modify AutoMockable.stencil template like here:

{% macro existentialClosureVariableTypeName typeName isVariadic -%}
    {%- if typeName|contains:"any " and typeName|contains:"!" -%}
        {{ typeName | replace:"any","(any" | replace:"!",")?" }}
    {%- elif typeName|contains:"any " and typeName.isClosure and typeName|contains:"?" -%}
        {{ typeName | replace:"any","(any" | replace:"?",")?" }}
    {%- elif typeName|contains:"any " and typeName|contains:"?" -%}
        {{ typeName | replace:"any","(any" | replace:"?",")?" }}
    {%- elif typeName|contains:"some " and typeName|contains:"!" -%}
        {{ typeName | replace:"some","(any" | replace:"!",")?" }}
    {%- elif typeName|contains:"some " and typeName.isClosure and typeName|contains:"?" -%}
        {{ typeName | replace:"some","(any" | replace:"?",")?" }}
    {%- elif typeName|contains:"some " and typeName|contains:"?" -%}
        {{ typeName | replace:"some","(any" | replace:"?",")?" }}
    {%- elif isVariadic -%}
-        {{ typeName }}...
+        [{{ typeName }}]
    {%- else -%}
        {{ typeName|replace:"some ","any " }}
    {%- endif -%}
{%- endmacro %}

from sourcery.

rokridi avatar rokridi commented on June 12, 2024

@art-divin thanks for replying. I updated the issue's description.
The actual stencil has issues. It does not handle closures with existential parameters properly. The macros need to be re-written.
Here is an example of a function having a parameter which is a closure having a variadic parameter:

func foo(closure: (String...) -> Void)

But this case is tricky to handle.

from sourcery.

art-divin avatar art-divin commented on June 12, 2024

@rokridi thank you, I see. So the branch I've referred above should support ClosureParameter.isVariadic. Please check

from sourcery.

art-divin avatar art-divin commented on June 12, 2024

@rokridi

var func13ParamStringVoidVoidClosure: (((String...) -> Void) -> Void)?

This is not compilable swift - checked yesterday also, to my amusement, variadic is only supported in function signature, does not work with variables.

So, it'd work as:

    var func13ParamStringVoidVoidClosure: ((([(String)]) -> Void) -> Void)?

from sourcery.

rokridi avatar rokridi commented on June 12, 2024

This PR should fix the issue

from sourcery.

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.