Skip to content

Commit

Permalink
Merge pull request #235 from sidepelican/existential_any
Browse files Browse the repository at this point in the history
Wrap underlyingType if the type has existential any
  • Loading branch information
sidepelican authored May 15, 2023
2 parents 10241d1 + 1ab20b3 commit 5c56a72
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Models/ParamModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class ParamModel: Model {
if self.inInit, self.needVarDecl {
let type: `Type`
if eraseType {
type = Type(.any)
type = Type(.anyType)
} else {
type = self.type
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Models/TypeAliasModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class TypeAliasModel: Model {
if let val = overrideTypes?[self.name] {
self.type = Type(val)
} else {
self.type = typeName.isEmpty ? Type(String.any) : Type(typeName)
self.type = typeName.isEmpty ? Type(String.anyType) : Type(typeName)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Utils/InheritanceResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func lookupEntities(key: String,
if declType == .protocolType { // TODO: remove this once parent protocol (current decl = classtype) handling is resolved.
// If the protocol inherits other protocols, look up their entities as well.
for parent in current.entityNode.inheritedTypes {
if parent != .class, parent != .any, parent != .anyObject {
if parent != .class, parent != .anyType, parent != .anyObject {
let (parentModels, parentProcessedModels, parentAttributes, parentPaths, parentPathToContents) = lookupEntities(key: parent, declType: declType, protocolMap: protocolMap, inheritanceMap: inheritanceMap)
models.append(contentsOf: parentModels)
processedModels.append(contentsOf: parentProcessedModels)
Expand Down
3 changes: 2 additions & 1 deletion Sources/MockoloFramework/Utils/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ extension String {
static let mockType = "protocol"
static let unknownVal = "Unknown"
static let prefix = "prefix"
static let any = "Any"
static let anyType = "Any"
static let any = "any"
static let some = "some"
static let anyObject = "AnyObject"
static let fatalError = "fatalError"
Expand Down
18 changes: 11 additions & 7 deletions Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public final class `Type` {
}

// Use force unwrapped for the underlying type so it doesn't always have to be set in the init (need to allow blank init).
if hasClosure {
if hasClosure || hasExistentialAny {
ret = "(\(ret))!"
} else {
if isOptional {
Expand Down Expand Up @@ -129,6 +129,10 @@ public final class `Type` {
return typeName.range(of: String.closureArrow) != nil
}

var hasExistentialAny: Bool {
return typeName.hasPrefix(.any)
}

var isEscaping: Bool {
return typeName.hasPrefix(String.escaping)
}
Expand Down Expand Up @@ -525,16 +529,16 @@ public final class `Type` {
if !typeParams.filter({ returnComps.contains($0)}).isEmpty {
returnAsStr = returnType.typeName
if returnType.isOptional {
displayableReturnType = .any + "?"
displayableReturnType = .anyType + "?"
returnAsStr.removeLast()
asSuffix = "?"
} else if returnType.isIUO {
displayableReturnType = .any + "!"
displayableReturnType = .anyType + "!"
returnAsStr.removeLast()
} else if returnType.isSelf {
returnAsStr = String.`Self`
} else {
displayableReturnType = .any
displayableReturnType = .anyType
}

if !returnAsStr.isEmpty {
Expand Down Expand Up @@ -598,7 +602,7 @@ public final class `Type` {
let left = ret[ret.startIndex..<closureRng.lowerBound]
for item in typeParamList {
if isEscaping, left.literalComponents.contains(item) {
ret = String.any
ret = String.anyType
if isTypeOptional {
ret += "?"
}
Expand All @@ -608,7 +612,7 @@ public final class `Type` {

for item in typeParamList {
if ret.literalComponents.contains(item) {
ret = ret.replacingOccurrences(of: item, with: String.any)
ret = ret.replacingOccurrences(of: item, with: String.anyType)
}
}
return ret
Expand All @@ -618,7 +622,7 @@ public final class `Type` {
}.isEmpty == false

if hasGenericType || isOpaqueType {
ret = .any
ret = .anyType
if isTypeOptional {
ret += "?"
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/TestExistentialAny/ExistentialAnyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

class ExistentialAnyTests: MockoloTestCase {
func testForArgFuncs() {
verify(srcContent: existentialAny,
dstContent: existentialAnyMock)
}
}
51 changes: 51 additions & 0 deletions Tests/TestExistentialAny/FixtureExistentialAny.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
let existentialAny = """
/// \(String.mockAnnotation)
protocol ExistentialAny {
var foo: P { get }
var bar: any R<Int> { get }
var baz: any P & Q { get }
var qux: (any P) -> any P { get }
}
"""

let existentialAnyMock =
"""
class ExistentialAnyMock: ExistentialAny {
init() { }
init(foo: P, bar: any R<Int>, baz: any P & Q, qux: @escaping (any P) -> any P) {
self._foo = foo
self._bar = bar
self._baz = baz
self._qux = qux
}
private(set) var fooSetCallCount = 0
private var _foo: P! { didSet { fooSetCallCount += 1 } }
var foo: P {
get { return _foo }
set { _foo = newValue }
}
private(set) var barSetCallCount = 0
private var _bar: (any R<Int>)! { didSet { barSetCallCount += 1 } }
var bar: any R<Int> {
get { return _bar }
set { _bar = newValue }
}
private(set) var bazSetCallCount = 0
private var _baz: (any P & Q)! { didSet { bazSetCallCount += 1 } }
var baz: any P & Q {
get { return _baz }
set { _baz = newValue }
}
private(set) var quxSetCallCount = 0
private var _qux: ((any P) -> any P)! { didSet { quxSetCallCount += 1 } }
var qux: (any P) -> any P {
get { return _qux }
set { _qux = newValue }
}
}
"""

0 comments on commit 5c56a72

Please sign in to comment.