Skip to content

Commit

Permalink
Merge pull request #126 from tinder-maxwellelliott/cast_generic_closure
Browse files Browse the repository at this point in the history
[#119] Attempts to fix Optional Generic Closures
  • Loading branch information
ellie authored Sep 10, 2020
2 parents 0be2520 + d127679 commit 63ec5ef
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Sources/MockoloFramework/Templates/ClosureTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ extension ClosureModel {
if argType.isInOut {
return "&" + argName.safeName
}
if argType.hasClosure && argType.isOptional,
let renderedClosure = renderOptionalGenericClosure(argType: argType, argName: argName) {
return renderedClosure
}
return argName.safeName
}
handlerParamValsStr = zipped.joined(separator: ", ")
Expand Down Expand Up @@ -70,4 +74,21 @@ extension ClosureModel {
}
return "return \(result)"
}

private func renderOptionalGenericClosure(
argType: Type,
argName: String
) -> String? {
let literalComponents = argType.typeName.literalComponents
for genericTypeName in genericTypeNames {
if literalComponents.contains(genericTypeName) {
var processTypeParams = argType.processTypeParams(with: genericTypeNames)
let closureCast = processTypeParams.withoutTrailingCharacters(["!", "?"])
return argName.safeName +
" as? " +
closureCast
}
}
return nil
}
}
11 changes: 10 additions & 1 deletion Sources/MockoloFramework/Utils/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,16 @@ extension String {
var withRightParen: String {
return "\(self))"
}


mutating func withoutTrailingCharacters(_ characters: [String]) -> String {
for character in characters {
if hasSuffix(character) {
_ = self.removeLast()
}
}
return self
}


func canBeInitParam(type: String, isStatic: Bool) -> Bool {
return !(isStatic || type == .unknownVal || (type.hasSuffix("?") && type.contains(String.closureArrow)) || isGenerated(type: Type(type)))
Expand Down
10 changes: 10 additions & 0 deletions Tests/TestGenericFuncs/FixtureGenericFunc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protocol GenericFunc {
func tell<BodyType: AnotherBody>(_ type: ResponseType, with handler: @escaping (BodyType) -> ()) -> Disposable
func pull<T>(events: [SomeEvent], value: T, once: Bool, closure: @escaping (T?) -> ())
func pull<U: ObservableType>(events: [SomeEvent], until: U?, closure: @escaping () -> ())
func optionalPull<T>(events: [SomeEvent], value: T, once: Bool, closure: ((T?) -> ())?)
func add<T: FixedWidthInteger>(n1: T, n2: T?)
func add<T: Sequence> (a: T?, b: T?)
func add<T: Collection> (a: T, b: T)
Expand Down Expand Up @@ -133,6 +134,15 @@ class GenericFuncMock: GenericFunc {
pullEventsHandler(events, until, closure)
}
}
var optionalPullCallCount = 0
var optionalPullHandler: (([SomeEvent], Any, Bool, ((Any?) -> ())?) -> ())?
func optionalPull<T>(events: [SomeEvent], value: T, once: Bool, closure: ((T?) -> ())?) {
optionalPullCallCount += 1
if let optionalPullHandler = optionalPullHandler {
optionalPullHandler(events, value, once, closure as? ((Any?) -> ()))
}
}
var addCallCount = 0
var addHandler: ((Any, Any?) -> ())?
Expand Down

0 comments on commit 63ec5ef

Please sign in to comment.