Skip to content

Commit

Permalink
Fixed issue with generics not recognized as generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrzej Michnia authored and Przemyslaw-Wosko committed Apr 4, 2018
1 parent eb59f67 commit 3f5220b
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -168,3 +168,4 @@ Example/fastlane/test_output/report.junit

# Sourcery genreated file
default.profraw
.DS_Store
10 changes: 5 additions & 5 deletions Podfile.lock
@@ -1,8 +1,8 @@
PODS:
- Sourcery (0.9.0)
- SwiftyMocky (1.0.2):
- SwiftyMocky/Core (= 1.0.2)
- SwiftyMocky/Core (1.0.2):
- SwiftyMocky (2.0.1):
- SwiftyMocky/Core (= 2.0.1)
- SwiftyMocky/Core (2.0.1):
- Sourcery

DEPENDENCIES:
Expand All @@ -14,8 +14,8 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Sourcery: e3829460a1fa0bcd3b689e6087a89cc063cd2289
SwiftyMocky: cfbf3036c4abfb722423d6cd50ad6e9be3aacdb5
SwiftyMocky: dfbd6b7bc4af462de9a0ae8e104f761a5a3e6ea6

PODFILE CHECKSUM: 71c85b79b35b28e80d11a137d918ed3d63d9e119

COCOAPODS: 1.3.1
COCOAPODS: 1.4.0
4 changes: 3 additions & 1 deletion Sources/Templates/Mock.swifttemplate
Expand Up @@ -131,7 +131,9 @@ import SourceryRuntime

func containsType(_ type: String) -> Bool {
return types.contains(where: {
"\($0).Type" == type
"\($0).Type" == type ||
type.contains("<\($0)>") ||
type.contains("<\($0).Type>")
})
}

Expand Down
Expand Up @@ -14,6 +14,14 @@ protocol ProtocolWithGenericMethods {
func methodWithGenericConstraint<U>(param: [U]) -> U where U: Equatable
}

struct Resource<T> { }
struct Response<T> { }
struct Observable<T> { }
//sourcery: AutoMockable
protocol ProtocolWithGenericMethodsNested {
func methodWithGeneric<T>(resource: Resource<T>) -> Observable<Response<T>>
}

//sourcery: AutoMockable
//sourcery: associatedtype = "T: Sequence"
protocol ProtocolWithAssociatedType {
Expand Down
110 changes: 110 additions & 0 deletions SwiftyMocky-Tests/iOS/Mocks/Mock.generated.swift
Expand Up @@ -2595,6 +2595,116 @@ class ProtocolWithGenericMethodsMock: ProtocolWithGenericMethods, Mock {
}
}

// MARK: - ProtocolWithGenericMethodsNested
class ProtocolWithGenericMethodsNestedMock: ProtocolWithGenericMethodsNested, Mock {
private var invocations: [MethodType] = []
private var methodReturnValues: [Given] = []
private var methodPerformValues: [Perform] = []
var matcher: Matcher = Matcher.default


typealias Property = Swift.Never


func methodWithGeneric<T>(resource: Resource<T>) -> Observable<Response<T>> {
addInvocation(.imethodWithGeneric__resource_resource(Parameter<Resource<T>>.value(resource).wrapAsGeneric()))
let perform = methodPerformValue(.imethodWithGeneric__resource_resource(Parameter<Resource<T>>.value(resource).wrapAsGeneric())) as? (Resource<T>) -> Void
perform?(resource)
let givenValue: (value: Any?, error: Error?) = methodReturnValue(.imethodWithGeneric__resource_resource(Parameter<Resource<T>>.value(resource).wrapAsGeneric()))
let value = givenValue.value as? Observable<Response<T>>
return value.orFail("stub return value not specified for methodWithGeneric<T>(resource: Resource<T>). Use given")
}

fileprivate enum MethodType {
case imethodWithGeneric__resource_resource(Parameter<GenericAttribute>)

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Bool {
switch (lhs, rhs) {
case (.imethodWithGeneric__resource_resource(let lhsResource), .imethodWithGeneric__resource_resource(let rhsResource)):
guard Parameter.compare(lhs: lhsResource, rhs: rhsResource, with: matcher) else { return false }
return true
}
}

func intValue() -> Int {
switch self {
case let .imethodWithGeneric__resource_resource(p0): return p0.intValue
}
}
}

struct Given {
fileprivate var method: MethodType
var returns: Any?
var `throws`: Error?

private init(method: MethodType, returns: Any?, throws: Error?) {
self.method = method
self.returns = returns
self.`throws` = `throws`
}

static func methodWithGeneric<T>(resource: Parameter<Resource<T>>, willReturn: Observable<Response<T>>) -> Given {
return Given(method: .imethodWithGeneric__resource_resource(resource.wrapAsGeneric()), returns: willReturn, throws: nil)
}
}

struct Verify {
fileprivate var method: MethodType

static func methodWithGeneric<T>(resource: Parameter<Resource<T>>) -> Verify {
return Verify(method: .imethodWithGeneric__resource_resource(resource.wrapAsGeneric()))
}
}

struct Perform {
fileprivate var method: MethodType
var performs: Any

static func methodWithGeneric<T>(resource: Parameter<Resource<T>>, perform: (Resource<T>) -> Void) -> Perform {
return Perform(method: .imethodWithGeneric__resource_resource(resource.wrapAsGeneric()), performs: perform)
}
}

private func matchingCalls(_ method: Verify) -> Int {
return matchingCalls(method.method).count
}

public func given(_ method: Given) {
methodReturnValues.append(method)
methodReturnValues.sort { $0.method.intValue() < $1.method.intValue() }
}

public func perform(_ method: Perform) {
methodPerformValues.append(method)
methodPerformValues.sort { $0.method.intValue() < $1.method.intValue() }
}

public func verify(_ method: Verify, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) {
let invocations = matchingCalls(method.method)
MockyAssert(count.matches(invocations.count), "Expeced: \(count) invocations of `\(method.method)`, but was: \(invocations.count)", file: file, line: line)
}
public func verify(property: Property, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) { }

private func addInvocation(_ call: MethodType) {
invocations.append(call)
}

private func methodReturnValue(_ method: MethodType) -> (value: Any?, error: Error?) {
let matched = methodReturnValues.reversed().first { MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) }
return (value: matched?.returns, error: matched?.`throws`)
}

private func methodPerformValue(_ method: MethodType) -> Any? {
let matched = methodPerformValues.reversed().first { MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) }
return matched?.performs
}

private func matchingCalls(_ method: MethodType) -> [MethodType] {
return invocations.filter { MethodType.compareParameters(lhs: $0, rhs: method, matcher: matcher) }
}
}

// MARK: - ProtocolWithInitializers
class ProtocolWithInitializersMock: ProtocolWithInitializers, Mock {
private var invocations: [MethodType] = []
Expand Down
110 changes: 110 additions & 0 deletions SwiftyMocky-Tests/macOS/Mocks/Mock.generated.swift
Expand Up @@ -2595,6 +2595,116 @@ class ProtocolWithGenericMethodsMock: ProtocolWithGenericMethods, Mock {
}
}

// MARK: - ProtocolWithGenericMethodsNested
class ProtocolWithGenericMethodsNestedMock: ProtocolWithGenericMethodsNested, Mock {
private var invocations: [MethodType] = []
private var methodReturnValues: [Given] = []
private var methodPerformValues: [Perform] = []
var matcher: Matcher = Matcher.default


typealias Property = Swift.Never


func methodWithGeneric<T>(resource: Resource<T>) -> Observable<Response<T>> {
addInvocation(.imethodWithGeneric__resource_resource(Parameter<Resource<T>>.value(resource).wrapAsGeneric()))
let perform = methodPerformValue(.imethodWithGeneric__resource_resource(Parameter<Resource<T>>.value(resource).wrapAsGeneric())) as? (Resource<T>) -> Void
perform?(resource)
let givenValue: (value: Any?, error: Error?) = methodReturnValue(.imethodWithGeneric__resource_resource(Parameter<Resource<T>>.value(resource).wrapAsGeneric()))
let value = givenValue.value as? Observable<Response<T>>
return value.orFail("stub return value not specified for methodWithGeneric<T>(resource: Resource<T>). Use given")
}

fileprivate enum MethodType {
case imethodWithGeneric__resource_resource(Parameter<GenericAttribute>)

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Bool {
switch (lhs, rhs) {
case (.imethodWithGeneric__resource_resource(let lhsResource), .imethodWithGeneric__resource_resource(let rhsResource)):
guard Parameter.compare(lhs: lhsResource, rhs: rhsResource, with: matcher) else { return false }
return true
}
}

func intValue() -> Int {
switch self {
case let .imethodWithGeneric__resource_resource(p0): return p0.intValue
}
}
}

struct Given {
fileprivate var method: MethodType
var returns: Any?
var `throws`: Error?

private init(method: MethodType, returns: Any?, throws: Error?) {
self.method = method
self.returns = returns
self.`throws` = `throws`
}

static func methodWithGeneric<T>(resource: Parameter<Resource<T>>, willReturn: Observable<Response<T>>) -> Given {
return Given(method: .imethodWithGeneric__resource_resource(resource.wrapAsGeneric()), returns: willReturn, throws: nil)
}
}

struct Verify {
fileprivate var method: MethodType

static func methodWithGeneric<T>(resource: Parameter<Resource<T>>) -> Verify {
return Verify(method: .imethodWithGeneric__resource_resource(resource.wrapAsGeneric()))
}
}

struct Perform {
fileprivate var method: MethodType
var performs: Any

static func methodWithGeneric<T>(resource: Parameter<Resource<T>>, perform: (Resource<T>) -> Void) -> Perform {
return Perform(method: .imethodWithGeneric__resource_resource(resource.wrapAsGeneric()), performs: perform)
}
}

private func matchingCalls(_ method: Verify) -> Int {
return matchingCalls(method.method).count
}

public func given(_ method: Given) {
methodReturnValues.append(method)
methodReturnValues.sort { $0.method.intValue() < $1.method.intValue() }
}

public func perform(_ method: Perform) {
methodPerformValues.append(method)
methodPerformValues.sort { $0.method.intValue() < $1.method.intValue() }
}

public func verify(_ method: Verify, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) {
let invocations = matchingCalls(method.method)
MockyAssert(count.matches(invocations.count), "Expeced: \(count) invocations of `\(method.method)`, but was: \(invocations.count)", file: file, line: line)
}
public func verify(property: Property, count: Count = Count.moreOrEqual(to: 1), file: StaticString = #file, line: UInt = #line) { }

private func addInvocation(_ call: MethodType) {
invocations.append(call)
}

private func methodReturnValue(_ method: MethodType) -> (value: Any?, error: Error?) {
let matched = methodReturnValues.reversed().first { MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) }
return (value: matched?.returns, error: matched?.`throws`)
}

private func methodPerformValue(_ method: MethodType) -> Any? {
let matched = methodPerformValues.reversed().first { MethodType.compareParameters(lhs: $0.method, rhs: method, matcher: matcher) }
return matched?.performs
}

private func matchingCalls(_ method: MethodType) -> [MethodType] {
return invocations.filter { MethodType.compareParameters(lhs: $0, rhs: method, matcher: matcher) }
}
}

// MARK: - ProtocolWithInitializers
class ProtocolWithInitializersMock: ProtocolWithInitializers, Mock {
private var invocations: [MethodType] = []
Expand Down

0 comments on commit 3f5220b

Please sign in to comment.