Skip to content

Commit

Permalink
Add support for Actor protocol. (#266)
Browse files Browse the repository at this point in the history
* Rename confusing `Type` type to `SwiftType`

* Rename ClassModel to NominalModel and update ModelType

* Actor mock support

* Add description comment to confusing class.

* Stop to using confusing SwiftType usage
  • Loading branch information
sidepelican authored Oct 24, 2024
1 parent f7ff730 commit 0ab3723
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 103 deletions.
8 changes: 4 additions & 4 deletions Sources/MockoloFramework/Models/ArgumentsHistoryModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Foundation

final class ArgumentsHistoryModel: Model {
var name: String
var type: Type
var type: SwiftType
var offset: Int64 = .max
let suffix: String
let capturableParamNames: [String]
let capturableParamTypes: [Type]
let capturableParamTypes: [SwiftType]
let isHistoryAnnotated: Bool

var modelType: ModelType {
return .class
return .argumentsHistory
}

init?(name: String, genericTypeParams: [ParamModel], params: [ParamModel], isHistoryAnnotated: Bool, suffix: String) {
Expand All @@ -28,7 +28,7 @@ final class ArgumentsHistoryModel: Model {
self.capturableParamTypes = capturables.map(\.type)

let genericTypeNameList = genericTypeParams.map(\.name)
self.type = Type.toArgumentsHistoryType(with: capturableParamTypes, typeParams: genericTypeNameList)
self.type = SwiftType.toArgumentsHistoryType(with: capturableParamTypes, typeParams: genericTypeNameList)
}

func enable(force: Bool) -> Bool {
Expand Down
12 changes: 6 additions & 6 deletions Sources/MockoloFramework/Models/ClosureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ import Foundation

final class ClosureModel: Model {
var name: String
var type: Type
var type: SwiftType
var offset: Int64 = .max
let funcReturnType: Type
let funcReturnType: SwiftType
let genericTypeNames: [String]
let paramNames: [String]
let paramTypes: [Type]
let paramTypes: [SwiftType]
let suffix: String

var modelType: ModelType {
return .class
return .closure
}


init(name: String, genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [Type], suffix: String, returnType: Type, encloser: String) {
init(name: String, genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [SwiftType], suffix: String, returnType: SwiftType, encloser: String) {
self.name = name + .handlerSuffix
self.suffix = suffix
let genericTypeNameList = genericTypeParams.map(\.name)
self.genericTypeNames = genericTypeNameList
self.paramNames = paramNames
self.paramTypes = paramTypes
self.funcReturnType = returnType
self.type = Type.toClosureType(with: paramTypes, typeParams: genericTypeNameList, suffix: suffix, returnType: returnType, encloser: encloser)
self.type = SwiftType.toClosureType(with: paramTypes, typeParams: genericTypeNameList, suffix: suffix, returnType: returnType, encloser: encloser)
}

func render(with identifier: String, encloser: String, useTemplateFunc: Bool = false, useMockObservable: Bool = false, allowSetCallCount: Bool = false, mockFinal: Bool = false, enableFuncArgsHistory: Bool = false, disableCombineDefaultValues: Bool = false) -> String? {
Expand Down
4 changes: 2 additions & 2 deletions Sources/MockoloFramework/Models/IfMacroModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
final class IfMacroModel: Model {
var name: String
var offset: Int64
var type: Type
var type: SwiftType
let entities: [Model]

var modelType: ModelType {
Expand All @@ -36,7 +36,7 @@ final class IfMacroModel: Model {
self.name = name
self.entities = entities
self.offset = offset
self.type = Type(name)
self.type = SwiftType(name)
}

func render(with identifier: String, encloser: String, useTemplateFunc: Bool, useMockObservable: Bool, allowSetCallCount: Bool = false, mockFinal: Bool = false, enableFuncArgsHistory: Bool = false, disableCombineDefaultValues: Bool = false) -> String? {
Expand Down
4 changes: 2 additions & 2 deletions Sources/MockoloFramework/Models/MethodModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class MethodModel: Model {
var filePath: String = ""
var data: Data? = nil
var name: String
var type: Type
var type: SwiftType
var offset: Int64
let length: Int64
let accessLevel: String
Expand Down Expand Up @@ -177,7 +177,7 @@ final class MethodModel: Model {
modelDescription: String?,
processed: Bool) {
self.name = name.trimmingCharacters(in: .whitespaces)
self.type = Type(typeName.trimmingCharacters(in: .whitespaces))
self.type = SwiftType(typeName.trimmingCharacters(in: .whitespaces))
self.suffix = [asyncOrReasync, throwsOrRethrows].compactMap { $0 }.joined(separator: " ")
self.offset = offset
self.length = length
Expand Down
11 changes: 9 additions & 2 deletions Sources/MockoloFramework/Models/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
import Foundation

public enum ModelType {
case variable, method, typeAlias, parameter, macro, `class`
case variable
case method
case typeAlias
case parameter
case macro
case nominal
case argumentsHistory
case closure
}

/// Represents a model for an entity such as var, func, class, etc.
Expand All @@ -42,7 +49,7 @@ public protocol Model {
var isStatic: Bool { get }

/// Decl(e.g. class/struct/protocol/enum) or return type (e.g. var/func)
var type: Type { get set }
var type: SwiftType { get set }

/// Offset where this type is declared
var offset: Int64 { get set }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,34 @@

import Foundation

final class ClassModel: Model {
final class NominalModel: Model {
enum NominalTypeDeclKind: String {
case `class`
case `actor`
}

var name: String
var offset: Int64
var type: Type
var type: SwiftType
let attribute: String
let accessLevel: String
let identifier: String
let declType: DeclType
let declTypeOfMockAnnotatedBaseType: DeclType
let inheritedTypes: [String]
let entities: [(String, Model)]
let initParamCandidates: [VariableModel]
let declaredInits: [MethodModel]
let metadata: AnnotationMetadata?
let declKind: NominalTypeDeclKind

var modelType: ModelType {
return .class
return .nominal
}

init(identifier: String,
acl: String,
declType: DeclType,
declTypeOfMockAnnotatedBaseType: DeclType,
declKind: NominalTypeDeclKind,
inheritedTypes: [String],
attributes: [String],
offset: Int64,
Expand All @@ -46,8 +53,9 @@ final class ClassModel: Model {
entities: [(String, Model)]) {
self.identifier = identifier
self.name = metadata?.nameOverride ?? (identifier + "Mock")
self.type = Type(.class)
self.declType = declType
self.type = SwiftType(self.name)
self.declTypeOfMockAnnotatedBaseType = declTypeOfMockAnnotatedBaseType
self.declKind = declKind
self.inheritedTypes = inheritedTypes
self.entities = entities
self.declaredInits = declaredInits
Expand All @@ -68,12 +76,12 @@ final class ClassModel: Model {
enableFuncArgsHistory: Bool = false,
disableCombineDefaultValues: Bool = false
) -> String? {
return applyClassTemplate(
return applyNominalTemplate(
name: name,
identifier: self.identifier,
accessLevel: accessLevel,
attribute: attribute,
declType: declType,
declTypeOfMockAnnotatedBaseType: declTypeOfMockAnnotatedBaseType,
inheritedTypes: inheritedTypes,
metadata: metadata,
useTemplateFunc: useTemplateFunc,
Expand Down
8 changes: 4 additions & 4 deletions Sources/MockoloFramework/Models/ParamModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class ParamModel: Model {
var name: String
var offset: Int64
var length: Int64
var type: Type
var type: SwiftType
let label: String
let isGeneric: Bool
let inInit: Bool
Expand All @@ -40,7 +40,7 @@ final class ParamModel: Model {

init(label: String = "", name: String, typeName: String, isGeneric: Bool = false, inInit: Bool = false, needVarDecl: Bool, offset: Int64, length: Int64) {
self.name = name.trimmingCharacters(in: .whitespaces)
self.type = Type(typeName.trimmingCharacters(in: .whitespaces))
self.type = SwiftType(typeName.trimmingCharacters(in: .whitespaces))
let labelStr = label.trimmingCharacters(in: .whitespaces)
self.label = name != labelStr ? labelStr : ""
self.offset = offset
Expand Down Expand Up @@ -77,9 +77,9 @@ final class ParamModel: Model {
/// ```
func asInitVarDecl(eraseType: Bool) -> String? {
if self.inInit, self.needVarDecl {
let type: `Type`
let type: SwiftType
if eraseType {
type = Type(.anyType)
type = SwiftType(.anyType)
} else {
type = self.type
}
Expand Down
22 changes: 12 additions & 10 deletions Sources/MockoloFramework/Models/ParsedEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct ResolvedEntity {
let uniqueModels: [(String, Model)]
let attributes: [String]
let inheritedTypes: [String]
var inheritsActorProtocol: Bool

var declaredInits: [MethodModel] {
return uniqueModels.filter {$0.1.isInitializer}.compactMap{ $0.1 as? MethodModel }
Expand Down Expand Up @@ -62,16 +63,17 @@ struct ResolvedEntity {


func model() -> Model {
return ClassModel(identifier: key,
acl: entity.entityNode.accessLevel,
declType: entity.entityNode.declType,
inheritedTypes: inheritedTypes,
attributes: attributes,
offset: entity.entityNode.offset,
metadata: entity.metadata,
initParamCandidates: initParamCandidates,
declaredInits: declaredInits,
entities: uniqueModels)
return NominalModel(identifier: key,
acl: entity.entityNode.accessLevel,
declTypeOfMockAnnotatedBaseType: entity.entityNode.declType,
declKind: inheritsActorProtocol ? .actor : .class,
inheritedTypes: inheritedTypes,
attributes: attributes,
offset: entity.entityNode.offset,
metadata: entity.metadata,
initParamCandidates: initParamCandidates,
declaredInits: declaredInits,
entities: uniqueModels)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/MockoloFramework/Models/TypeAliasModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
final class TypeAliasModel: Model {
var filePath: String = ""
var name: String
var type: Type
var type: SwiftType
var offset: Int64 = .max
var length: Int64
var typeOffset: Int64 = 0
Expand Down Expand Up @@ -47,9 +47,9 @@ final class TypeAliasModel: Model {
self.addAcl = encloserType == .protocolType && !processed
// If there's an override typealias value, set it to type
if let val = overrideTypes?[self.name] {
self.type = Type(val)
self.type = SwiftType(val)
} else {
self.type = typeName.isEmpty ? Type(String.anyType) : Type(typeName)
self.type = typeName.isEmpty ? SwiftType(String.anyType) : SwiftType(typeName)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/MockoloFramework/Models/VariableModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

final class VariableModel: Model {
var name: String
var type: Type
var type: SwiftType
var offset: Int64
let accessLevel: String
let attributes: [String]?
Expand Down Expand Up @@ -48,7 +48,7 @@ final class VariableModel: Model {
combineType: CombineType?,
processed: Bool) {
self.name = name.trimmingCharacters(in: .whitespaces)
self.type = Type(typeName.trimmingCharacters(in: .whitespaces))
self.type = SwiftType(typeName.trimmingCharacters(in: .whitespaces))
self.offset = offset
self.isStatic = isStatic
self.shouldOverride = encloserType == .classType
Expand Down
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Operations/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public func generate(sourceDirs: [String],
typeKeyList.forEach { (t: String) in
typeKeys[t] = "\(t)Mock()"
}
Type.customTypeMap = typeKeys
SwiftType.customTypeMap = typeKeys

signpost_begin(name: "Generate models")
log("Resolve inheritance and generate unique entity models...", level: .info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ private func generateUniqueModels(key: String,
mockInheritedTypes.append(.uncheckedSendable)
}

let resolvedEntity = ResolvedEntity(key: key, entity: entity, uniqueModels: uniqueModels, attributes: attributes, inheritedTypes: mockInheritedTypes)
let resolvedEntity = ResolvedEntity(
key: key,
entity: entity,
uniqueModels: uniqueModels,
attributes: attributes,
inheritedTypes: mockInheritedTypes,
inheritsActorProtocol: inheritedTypes.contains(.actorProtocol)
)

return ResolvedEntityContainer(entity: resolvedEntity, paths: paths, imports: pathToContentList)
}
10 changes: 5 additions & 5 deletions Sources/MockoloFramework/Templates/ClosureTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import Foundation

extension ClosureModel {
func applyClosureTemplate(name: String,
type: Type,
type: SwiftType,
genericTypeNames: [String],
paramVals: [String]?,
paramTypes: [Type]?,
paramTypes: [SwiftType]?,
suffix: String,
returnDefaultType: Type) -> String {
returnDefaultType: SwiftType) -> String {

var handlerParamValsStr = ""
if let paramVals = paramVals, let paramTypes = paramTypes {
Expand Down Expand Up @@ -64,7 +64,7 @@ extension ClosureModel {
}


private func renderReturnDefaultStatement(name: String, type: Type) -> String {
private func renderReturnDefaultStatement(name: String, type: SwiftType) -> String {
guard !type.isUnknown else { return "" }

let result = type.defaultVal() ?? String.fatalError
Expand All @@ -79,7 +79,7 @@ extension ClosureModel {
}

private func renderOptionalGenericClosure(
argType: Type,
argType: SwiftType,
argName: String
) -> String? {
let literalComponents = argType.typeName.literalComponents
Expand Down
2 changes: 1 addition & 1 deletion Sources/MockoloFramework/Templates/MethodTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension MethodModel {
genericTypeParams: [ParamModel],
genericWhereClause: String?,
params: [ParamModel],
returnType: Type,
returnType: SwiftType,
accessLevel: String,
suffix: String,
argsHistory: ArgumentsHistoryModel?,
Expand Down
Loading

0 comments on commit 0ab3723

Please sign in to comment.