Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant tuple in handler type #275

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 10 additions & 47 deletions Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public final class SwiftType {
if isUnknown {
return false
}

var typeName = typeName
if typeName.hasPrefix(.any.withSpace) {
typeName.removeFirst(String.any.withSpace.count)
}

for scalar in typeName.unicodeScalars {
if scalar == " " {
return false
Expand Down Expand Up @@ -472,46 +478,6 @@ public final class SwiftType {
return nil
}



// Process substrings containing angled or square brackets by replacing a comma delimiter
// with another delimiter (e.g. ;) to make it easier to parse tuples
// @param arg The type string to be parsed
// @param left The opening bracket character
// @param right The closing bracket character
// @returns The processed string with a new delimiter
func parseBrackets(_ arg: String, type: BracketType) -> String {
var left = ""
var right = ""
switch type {
case .angle:
left = "<"
right = ">"
case .square:
left = "["
right = "]"
}

var mutableArg = arg
var nextRange: Range<String.Index>? = nil
while let leftRange = mutableArg.range(of: left, options: .caseInsensitive, range: nextRange, locale: nil),
let rightRange = mutableArg.range(of: right, options: .caseInsensitive, range: nextRange, locale: nil) {
let bound = leftRange.lowerBound..<rightRange.lowerBound
let sub = mutableArg[bound]
let newsub = sub.replacingOccurrences(of: ",", with: ";")
mutableArg = mutableArg.replacingOccurrences(of: sub, with: newsub)

if let nextIdx = mutableArg.index(rightRange.upperBound, offsetBy: 1, limitedBy: mutableArg.endIndex) {
nextRange = nextIdx..<mutableArg.endIndex
} else {
break
}
}

return mutableArg
}


static func toClosureType(
params: [SwiftType],
typeParams: [String],
Expand All @@ -532,7 +498,7 @@ public final class SwiftType {
var returnAsStr = ""
var asSuffix = "!"
var returnTypeCast = ""
if !typeParams.filter({ returnComps.contains($0)}).isEmpty {
if typeParams.contains(where: { returnComps.contains($0)}) {
returnAsStr = returnType.typeName
if returnType.isOptional {
displayableReturnType = .anyType + "?"
Expand All @@ -552,15 +518,12 @@ public final class SwiftType {
}
}

if returnType.isSelf {
displayableReturnType = encloser.typeName
if returnType.isSelf {
displayableReturnType = encloser.typeName
returnTypeCast = " as! " + String.`Self`
}

let isSimpleTuple = displayableReturnType.hasPrefix("(") && displayableReturnType.hasSuffix(")") &&
displayableReturnType.components(separatedBy: CharacterSet(charactersIn: "()")).filter({!$0.isEmpty}).count <= 1

if !isSimpleTuple {
if !(SwiftType(displayableReturnType).isSingular || SwiftType(displayableReturnType).isOptional) {
displayableReturnType = "(\(displayableReturnType))"
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/TestActor/FixtureActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ actor FooMock: Foo {
self.bar = bar
}
private(set) var fooCallCount = 0
var fooHandler: ((String) async -> (Result<String, Error>))?
var fooHandler: ((String) async -> Result<String, Error>)?
func foo(arg: String) async -> Result<String, Error> {
fooCallCount += 1
if let fooHandler = fooHandler {
Expand Down Expand Up @@ -51,7 +51,7 @@ actor FooMock: Foo {
var bar: Int = 0

private(set) var bazCallCount = 0
var bazHandler: ((String) async -> (Int))?
var bazHandler: ((String) async -> Int)?
func baz(arg: String) async -> Int {
bazCallCount += 1
if let bazHandler = bazHandler {
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestActor/FixtureGlobalActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RootBuildableMock: RootBuildable {


private(set) var buildCallCount = 0
var buildHandler: (() -> (RootController))?
var buildHandler: (() -> RootController)?
func build() -> RootController {
buildCallCount += 1
if let buildHandler = buildHandler {
Expand Down
4 changes: 2 additions & 2 deletions Tests/TestArgumentsHistory/FixtureArgumentsHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class FooMock: Foo {

private(set) var quxFuncCallCount = 0
var quxFuncArgValues = [Int]()
var quxFuncHandler: ((Int) -> (String))?
var quxFuncHandler: ((Int) -> String)?
func quxFunc(val: Int) -> String {
quxFuncCallCount += 1
quxFuncArgValues.append(val)
Expand Down Expand Up @@ -298,7 +298,7 @@ class FooMock: Foo {

private(set) var barFuncCallCount = 0
var barFuncArgValues = [Any]()
var barFuncHandler: ((Any) -> (Any))?
var barFuncHandler: ((Any) -> Any)?
func barFunc<T: Sequence, U: Collection>(val: T) -> U {
barFuncCallCount += 1
barFuncArgValues.append(val)
Expand Down
4 changes: 2 additions & 2 deletions Tests/TestClassMocking/FixtureMockableClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class LowMock: Low {
}

private(set) var fooCallCount = 0
var fooHandler: (() -> (Int))?
var fooHandler: (() -> Int)?
override func foo() -> Int {
fooCallCount += 1
if let fooHandler = fooHandler {
Expand Down Expand Up @@ -187,7 +187,7 @@ public class LowMock: Low {
}

private(set) var fooCallCount = 0
var fooHandler: (() -> (Int))?
var fooHandler: (() -> Int)?
override func foo() -> Int {
fooCallCount += 1
if let fooHandler = fooHandler {
Expand Down
62 changes: 55 additions & 7 deletions Tests/TestExistentialAny/FixtureExistentialAny.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ protocol ExistentialAny {
var bar: any R<Int> { get }
var baz: any P & Q { get }
var qux: (any P) -> any P { get }

func quux() -> P
func corge() -> any R<Int>
func grault() -> any P & Q
func garply() -> (any P) -> any P
}
"""

let existentialAnyMock =
"""
let existentialAnyMock = """
class ExistentialAnyMock: ExistentialAny {
init() { }
init(foo: P, bar: any R<Int>, baz: any P & Q, qux: @escaping (any P) -> any P) {
Expand All @@ -20,29 +24,73 @@ class ExistentialAnyMock: ExistentialAny {
}


private var _foo: P!

private var _foo: P!
var foo: P {
get { return _foo }
set { _foo = newValue }
}

private var _bar: (any R<Int>)!

private var _bar: (any R<Int>)!
var bar: any R<Int> {
get { return _bar }
set { _bar = newValue }
}

private var _baz: (any P & Q)!

private var _baz: (any P & Q)!
var baz: any P & Q {
get { return _baz }
set { _baz = newValue }
}

private var _qux: ((any P) -> any P)!

private var _qux: ((any P) -> any P)!
var qux: (any P) -> any P {
get { return _qux }
set { _qux = newValue }
}

private(set) var quuxCallCount = 0
var quuxHandler: (() -> P)?
func quux() -> P {
quuxCallCount += 1
if let quuxHandler = quuxHandler {
return quuxHandler()
}
fatalError("quuxHandler returns can't have a default value thus its handler must be set")
}

private(set) var corgeCallCount = 0
var corgeHandler: (() -> any R<Int>)?
func corge() -> any R<Int> {
corgeCallCount += 1
if let corgeHandler = corgeHandler {
return corgeHandler()
}
fatalError("corgeHandler returns can't have a default value thus its handler must be set")
}

private(set) var graultCallCount = 0
var graultHandler: (() -> (any P & Q))?
func grault() -> any P & Q {
graultCallCount += 1
if let graultHandler = graultHandler {
return graultHandler()
}
fatalError("graultHandler returns can't have a default value thus its handler must be set")
}

private(set) var garplyCallCount = 0
var garplyHandler: (() -> ((any P) -> any P))?
func garply() -> (any P) -> any P {
garplyCallCount += 1
if let garplyHandler = garplyHandler {
return garplyHandler()
}
fatalError("garplyHandler returns can't have a default value thus its handler must be set")
}
}
"""

Expand All @@ -69,7 +117,7 @@ class UseSomeProtocolMock: UseSomeProtocol {


private(set) var fooCallCount = 0
var fooHandler: (() -> (any SomeProtocol))?
var fooHandler: (() -> any SomeProtocol)?
func foo() -> any SomeProtocol {
fooCallCount += 1
if let fooHandler = fooHandler {
Expand Down
Loading