Skip to content

Commit

Permalink
Merge pull request #213 from fummicc1/support-ORT-param-handler
Browse files Browse the repository at this point in the history
Support `some` parameter's handler
  • Loading branch information
uhooi authored Feb 7, 2023
2 parents fd6e5f2 + 6bfc41c commit 3be40ef
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions Sources/MockoloFramework/Utils/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extension String {
static let unknownVal = "Unknown"
static let prefix = "prefix"
static let any = "Any"
static let some = "some"
static let anyObject = "AnyObject"
static let fatalError = "fatalError"
static let available = "available"
Expand Down
8 changes: 6 additions & 2 deletions Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public final class Type {
return typeName.hasPrefix(String.autoclosure)
}

var isOpaqueType: Bool {
typeName.contains(String.some + " ")
}

var isRxObservable: Bool {
return typeName.hasPrefix(.rxObservableLeftAngleBracket) || typeName.hasPrefix(.observableLeftAngleBracket)
}
Expand Down Expand Up @@ -611,9 +615,9 @@ public final class Type {
} else {
let hasGenericType = typeParamList.filter{ (item: String) -> Bool in
ret.literalComponents.contains(item)
}
}.isEmpty == false

if !hasGenericType.isEmpty {
if hasGenericType || isOpaqueType {
ret = .any
if isTypeOptional {
ret += "?"
Expand Down
91 changes: 91 additions & 0 deletions Tests/TestFuncs/TestOpaqueTypeFuncs/FixtureOpaqueTypeFunc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import MockoloFramework

let someParameterOptionalType = """
/// \(String.mockAnnotation)
public protocol OpaqueTypeProtocol {
func nonOptional(_ type: some Error) -> Int
func optional(_ type: (some Error)?)
}
"""

let someParameterOptionalTypeMock = """
public class OpaqueTypeProtocolMock: OpaqueTypeProtocol {
public init() { }
public private(set) var nonOptionalCallCount = 0
public var nonOptionalHandler: ((Any) -> (Int))?
public func nonOptional(_ type: some Error) -> Int {
nonOptionalCallCount += 1
if let nonOptionalHandler = nonOptionalHandler {
return nonOptionalHandler(type)
}
return 0
}
public private(set) var optionalCallCount = 0
public var optionalHandler: ((Any?) -> ())?
public func optional(_ type: (some Error)?) {
optionalCallCount += 1
if let optionalHandler = optionalHandler {
optionalHandler(type)
}
}
}
"""

let someMultiParameterOptionalType = """
/// \(String.mockAnnotation)
public protocol OpaqueTypeWithMultiTypeProtocol {
func nonOptional(_ type: some Error) -> Int
func optional(_ type: ((some (Error & Foo)))?)
func multiParam(_ typeA: some Error, _ typeB: some Error)
}
"""


let someMultiParameterOptionalTypeMock = """
public class OpaqueTypeWithMultiTypeProtocolMock: OpaqueTypeWithMultiTypeProtocol {
public init() { }
public private(set) var nonOptionalCallCount = 0
public var nonOptionalHandler: ((Any) -> (Int))?
public func nonOptional(_ type: some Error) -> Int {
nonOptionalCallCount += 1
if let nonOptionalHandler = nonOptionalHandler {
return nonOptionalHandler(type)
}
return 0
}
public private(set) var optionalCallCount = 0
public var optionalHandler: ((Any?) -> ())?
public func optional(_ type: ((some (Error & Foo)))?) {
optionalCallCount += 1
if let optionalHandler = optionalHandler {
optionalHandler(type)
}
}
public private(set) var multiParamCallCount = 0
public var multiParamHandler: ((Any, Any) -> ())?
public func multiParam(_ typeA: some Error, _ typeB: some Error) {
multiParamCallCount += 1
if let multiParamHandler = multiParamHandler {
multiParamHandler(typeA, typeB)
}
}
}
"""
14 changes: 14 additions & 0 deletions Tests/TestFuncs/TestOpaqueTypeFuncs/OpaqueTypeFuncTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

class OpaqueTypeFuncTests: MockoloTestCase {

func testOpaqueTypeParamterFuncs() {
verify(srcContent: someParameterOptionalType,
dstContent: someParameterOptionalTypeMock)
}

func testOpaqueTypeMultiParamterFuncs() {
verify(srcContent: someMultiParameterOptionalType,
dstContent: someMultiParameterOptionalTypeMock)
}
}

0 comments on commit 3be40ef

Please sign in to comment.