tjarratt / fake4swift

A small CLI for generating Swift test doubles
MIT License
24 stars 3 forks source link

Swift fakes don't seem to like functions that return generics. #12

Open younata opened 8 years ago

younata commented 8 years ago

For example, if I have a protocol that looks like:

public protocol Archiver {
    func zipIntoArchive(destinationArchiveURL: NSURL, _ inputPaths: [String]) -> SignalProducer<(), CarthageError>
    func unzipArchiveToDirectory(fileURL: NSURL, _ destinationDirectoryURL: NSURL) -> SignalProducer<(), CarthageError>
    func unzipArchiveToTemporaryDirectory(fileURL: NSURL) -> SignalProducer<NSURL, CarthageError>
}

and I hit "^g", I get:

import Foundation

// this file was generated by Xcode-Better-Refactor-Tools
// https://github.com/tjarratt/xcode-better-refactor-tools

class FakeArchiver : Archiver {
    init() {
    }

    private(set) var zipIntoArchiveCallCount : Int = 0
    var zipIntoArchiveStub : ((NSURL, [String]) -> (SignalProducer<())?
    private var zipIntoArchiveArgs : Array<(NSURL, [String])> = []
    func zipIntoArchiveReturns(stubbedValues: (SignalProducer<()) {
        self.zipIntoArchiveStub = {(destinationArchiveURL: NSURL, inputPaths: [String]) -> (SignalProducer<() in
        return stubbedValues
        }
        }
        func zipIntoArchiveArgsForCall(callIndex: Int) -> (NSURL, [String]) {
        return self.zipIntoArchiveArgs[callIndex]
        }
        func zipIntoArchive(destinationArchiveURL: NSURL, inputPaths: [String]) -> (SignalProducer<() {
        self.zipIntoArchiveCallCount++
        self.zipIntoArchiveArgs.append((destinationArchiveURL, inputPaths))
        return self.zipIntoArchiveStub!(destinationArchiveURL, inputPaths)
        }

        private(set) var unzipArchiveToDirectoryCallCount : Int = 0
        var unzipArchiveToDirectoryStub : ((NSURL, NSURL) -> (SignalProducer<())?
        private var unzipArchiveToDirectoryArgs : Array<(NSURL, NSURL)> = []
        func unzipArchiveToDirectoryReturns(stubbedValues: (SignalProducer<()) {
        self.unzipArchiveToDirectoryStub = {(fileURL: NSURL, destinationDirectoryURL: NSURL) -> (SignalProducer<() in
        return stubbedValues
        }
        }
        func unzipArchiveToDirectoryArgsForCall(callIndex: Int) -> (NSURL, NSURL) {
        return self.unzipArchiveToDirectoryArgs[callIndex]
        }
        func unzipArchiveToDirectory(fileURL: NSURL, destinationDirectoryURL: NSURL) -> (SignalProducer<() {
        self.unzipArchiveToDirectoryCallCount++
        self.unzipArchiveToDirectoryArgs.append((fileURL, destinationDirectoryURL))
        return self.unzipArchiveToDirectoryStub!(fileURL, destinationDirectoryURL)
        }

        private(set) var unzipArchiveToTemporaryDirectoryCallCount : Int = 0
        var unzipArchiveToTemporaryDirectoryStub : ((NSURL) -> (SignalProducer<NSURL, CarthageError>))?
        private var unzipArchiveToTemporaryDirectoryArgs : Array<(NSURL)> = []
        func unzipArchiveToTemporaryDirectoryReturns(stubbedValues: (SignalProducer<NSURL, CarthageError>)) {
        self.unzipArchiveToTemporaryDirectoryStub = {(fileURL: NSURL) -> (SignalProducer<NSURL, CarthageError>) in
        return stubbedValues
        }
        }
        func unzipArchiveToTemporaryDirectoryArgsForCall(callIndex: Int) -> (NSURL) {
        return self.unzipArchiveToTemporaryDirectoryArgs[callIndex]
        }
        func unzipArchiveToTemporaryDirectory(fileURL: NSURL) -> (SignalProducer<NSURL, CarthageError>) {
        self.unzipArchiveToTemporaryDirectoryCallCount++
        self.unzipArchiveToTemporaryDirectoryArgs.append((fileURL))
        return self.unzipArchiveToTemporaryDirectoryStub!(fileURL)
        }

        static func reset() {
        }
}

which, uh, is not correct.

tjarratt commented 8 years ago

Oh no! Thanks for filing this @younata. You're the best at finding edge cases (seriously, no sarcasm).

Yeah, that's not terribly surprising. As a workaround, I bet you could typealias your generic type and have this work just fine.


typealias CarthageSignalProducer SignalProducer<NSURL, CarthageError>

public protocol Archiver {
    func unzipArchiveToTemporaryDirectory(fileURL: NSURL) -> CarthageSignalProducer
}

(I did not verify if this code compiles, but you get the idea).

I'd be happy to merge a PR for this if you're interested. I just got the plugin compiling and tests passing again after the Xcode 7.3 update, so it should be in a good spot to contribute if you'd like.

If not, well, I'll put it on my list of work to do ... eventually? :skull: