Giter Club home page Giter Club logo

Comments (4)

TadeasKriz avatar TadeasKriz commented on May 18, 2024

@krin-san Could you try the commit d8b65f542f6ce2dbcabb58b0b002081d1578b38a? It should be fixed.

from cuckoo.

krin-san avatar krin-san commented on May 18, 2024

@TadeasKriz, the issue with closures now fixed by this commit. However, I catch a few new bugs.

I simplified my real singleton class to smth. like this:

class MyClass {
    static let sharedInstance = MyClass()
    private init() {}
}

The singleton has no fields to initialize (initialization block is empty) but I want to keep initializer private. Unfortunately, this leads to the following error in auto-generated code:

class MockMyClass: MyClass, Cuckoo.Mock {
    typealias Stubbing = __StubbingProxy_MyClass
    typealias Verification = __VerificationProxy_MyClass
    let manager = Cuckoo.MockManager()

    private var observed: MyClass?

// ERROR: Initializer does not override a designated initializer from its superclass
    required override init() {
    }

    ...
}

It's very logical. I should keep the initializer as internal or public to make it mockable - and it works! But let's check another case:

class MyClass {
    var field: String

    init() {
        field = ""
    }
}

I don't want to just leave an empty internal initializer - it's just meaningless. Now this class contains some field that cannot be initialized by default value so I should write my own initialer. This leads to the following issues in auto-generated code:

class MockMyClass: MyClass, Cuckoo.Mock {
    typealias Stubbing = __StubbingProxy_MyClass
    typealias Verification = __VerificationProxy_MyClass
    let manager = Cuckoo.MockManager()

    private var observed: MyClass?

    required override init() {
    }

    required init(spyOn victim: MyClass) {
        observed = victim
    }

    override var field: String {
        get {
            return manager.getter("field", original: observed.map { o in return { () -> String in o.field } })
        }
        set {
            manager.setter("field", value: newValue, original: observed != nil ? { self.observed?.field = $0 } : nil)
        }
    }

    override init() {
        return manager.call("init()", parameters: (), original: observed.map { o in return { () in o.init() } })
    }

    struct __StubbingProxy_MyClass: Cuckoo.StubbingProxy {
        private let manager: Cuckoo.MockManager

        init(manager: Cuckoo.MockManager) {
            self.manager = manager
        }

        var field: Cuckoo.ToBeStubbedProperty<String> {
            return Cuckoo.ToBeStubbedProperty(manager: manager, name: "field")
        }

        @warn_unused_result
// ERROR: Expected identifier in function
        func init() -> Cuckoo.StubNoReturnFunction<()> {
            return Cuckoo.StubNoReturnFunction(stub: manager.createStub("init()", parameterMatchers: []))
        }
    }

    struct __VerificationProxy_MyClass: Cuckoo.VerificationProxy {
        private let manager: Cuckoo.MockManager
        private let callMatcher: Cuckoo.CallMatcher
        private let sourceLocation: Cuckoo.SourceLocation

        init(manager: Cuckoo.MockManager, callMatcher: Cuckoo.CallMatcher, sourceLocation: Cuckoo.SourceLocation) {
            self.manager = manager
            self.callMatcher = callMatcher
            self.sourceLocation = sourceLocation
        }

        var field: Cuckoo.VerifyProperty<String> {
            return Cuckoo.VerifyProperty(manager: manager, name: "field", callMatcher: callMatcher, sourceLocation: sourceLocation)
        }

// ERROR: Expected identifier in function
        func init() -> Cuckoo.__DoNotUse<Void> {
            return manager.verify("init()", callMatcher: callMatcher, parameterMatchers: [] as [Cuckoo.ParameterMatcher<Void>], sourceLocation: sourceLocation)
        }
    }
}

The init is not a usual mockable function so the func init syntax is invalid.

from cuckoo.

TadeasKriz avatar TadeasKriz commented on May 18, 2024

Thank you for the report. The issue with initializers is currently being worked on so hopefully it is something we can fix. Mocking singletons with private initializers cannot be done as you said. Also we cannot return a different type from the singletons sharedInstance property, which means you cannot mock and test against it if your code uses singletons.

from cuckoo.

krin-san avatar krin-san commented on May 18, 2024

As I already mentioned, singleton and private initializer is not an issue. The issues revealed when I tried to make a class testable.
Normally, in a project I use a singleton instance of MyClass but I allocate it as a usual object while testing.

Looks like the key problem of this issue was resolved and the problem I catch related to #54

from cuckoo.

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.