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

Organize the arguments of Model.render. #272

Merged
merged 10 commits into from
Nov 12, 2024
14 changes: 10 additions & 4 deletions Sources/MockoloFramework/Models/ArgumentsHistoryModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ final class ArgumentsHistoryModel: Model {
return force || isHistoryAnnotated
}

func render(with identifier: String, encloser: String, useTemplateFunc: Bool = false, useMockObservable: Bool = false, allowSetCallCount: Bool = false, mockFinal: Bool = false, enableFuncArgsHistory: Bool, disableCombineDefaultValues: Bool = false) -> String? {
guard enable(force: enableFuncArgsHistory) else {
func render(
context: RenderContext,
arguments: GenerationArguments
) -> String? {
guard enable(force: arguments.enableFuncArgsHistory) else {
return nil
}
guard let overloadingResolvedName = context.overloadingResolvedName else {
return nil
}

switch capturableParamNames.count {
case 1:
return "\(identifier)\(String.argsHistorySuffix).append(\(capturableParamNames[0]))"
return "\(overloadingResolvedName)\(String.argsHistorySuffix).append(\(capturableParamNames[0]))"
case 2...:
let paramNamesStr = capturableParamNames.joined(separator: ", ")
return "\(identifier)\(String.argsHistorySuffix).append((\(paramNamesStr)))"
return "\(overloadingResolvedName)\(String.argsHistorySuffix).append((\(paramNamesStr)))"
default:
fatalError("paramNames must not be empty.")
}
Expand Down
30 changes: 19 additions & 11 deletions Sources/MockoloFramework/Models/ClosureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import Foundation

final class ClosureModel: Model {
var type: SwiftType
let name: String = "" // closure type cannot have a name
let offset: Int64 = .max

Expand All @@ -32,28 +31,37 @@ final class ClosureModel: Model {
return .closure
}

init(genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [SwiftType], isAsync: Bool, throwing: ThrowingKind, returnType: SwiftType, encloser: String) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encloser is unnecessary here because type depends on RenderContext.

init(genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [SwiftType], isAsync: Bool, throwing: ThrowingKind, returnType: SwiftType) {
// In the mock's call handler, rethrows is unavailable.
let throwing = throwing.coerceRethrowsToThrows
self.isAsync = isAsync
self.throwing = throwing
let genericTypeNameList = genericTypeParams.map(\.name)
self.genericTypeNames = genericTypeNameList
self.genericTypeNames = genericTypeParams.map(\.name)
self.paramNames = paramNames
self.paramTypes = paramTypes
self.funcReturnType = returnType
self.type = SwiftType.toClosureType(
}

func type(context: RenderContext) -> SwiftType {
return SwiftType.toClosureType(
params: paramTypes,
typeParams: genericTypeNameList,
typeParams: genericTypeNames,
isAsync: isAsync,
throwing: throwing,
returnType: returnType,
encloser: encloser
returnType: funcReturnType,
encloser: context.enclosingType!
)
}

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? {
return applyClosureTemplate(name: identifier + .handlerSuffix,

func render(
context: RenderContext,
arguments: GenerationArguments = .default
) -> String? {
guard let overloadingResolvedName = context.overloadingResolvedName else {
return nil
}
return applyClosureTemplate(type: type(context: context),
name: overloadingResolvedName + .handlerSuffix,
paramVals: paramNames,
paramTypes: paramTypes,
returnDefaultType: funcReturnType)
Expand Down
12 changes: 10 additions & 2 deletions Sources/MockoloFramework/Models/IfMacroModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ final class IfMacroModel: Model {
self.offset = offset
}

func render(with identifier: String, encloser: String, useTemplateFunc: Bool, useMockObservable: Bool, allowSetCallCount: Bool = false, mockFinal: Bool = false, enableFuncArgsHistory: Bool = false, disableCombineDefaultValues: Bool = false) -> String? {
return applyMacroTemplate(name: name, useTemplateFunc: useTemplateFunc, useMockObservable: useMockObservable, allowSetCallCount: allowSetCallCount, mockFinal: mockFinal, enableFuncArgsHistory: enableFuncArgsHistory, disableCombineDefaultValues: disableCombineDefaultValues, entities: entities)
func render(
context: RenderContext,
arguments: GenerationArguments
) -> String? {
return applyMacroTemplate(
name: name,
context: context,
arguments: arguments,
entities: entities
)
}
}
44 changes: 18 additions & 26 deletions Sources/MockoloFramework/Models/MethodModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ final class MethodModel: Model {
let processed: Bool
let modelDescription: String?
let isStatic: Bool
let shouldOverride: Bool
let isAsync: Bool
let throwing: ThrowingKind
let funcsWithArgsHistory: [String]
Expand All @@ -49,7 +48,7 @@ final class MethodModel: Model {
}

/// This is used to uniquely identify methods with the same signature and different generic requirements
var genericWhereClauseToSignatureComponent: String {
private var genericWhereClauseToSignatureComponent: String {
guard let genericWhereClause else {
return ""
}
Expand Down Expand Up @@ -111,7 +110,7 @@ final class MethodModel: Model {

let genericTypeNames = self.genericTypeParams.map { $0.name.capitalizeFirstLetter + $0.type.displayName }
args.append(contentsOf: genericTypeNames)
if let genericWhereClause {
if genericWhereClause != nil {
args.append(genericWhereClauseToSignatureComponent)
}
args.append(contentsOf: paramTypes.map(\.displayName))
Expand All @@ -137,7 +136,7 @@ final class MethodModel: Model {
return ret
}()

func handler(encloser: String) -> ClosureModel? {
func handler() -> ClosureModel? {
if isInitializer {
return nil
}
Expand All @@ -147,14 +146,12 @@ final class MethodModel: Model {
paramTypes: params.map(\.type),
isAsync: isAsync,
throwing: throwing,
returnType: returnType,
encloser: encloser)
returnType: returnType)
}

init(name: String,
typeName: String,
kind: MethodKind,
encloserType: DeclType,
acl: String,
genericTypeParams: [ParamModel],
genericWhereClause: String?,
Expand All @@ -176,7 +173,6 @@ final class MethodModel: Model {
self.length = length
self.kind = kind
self.isStatic = isStatic
self.shouldOverride = encloserType == .classType
self.params = params
self.genericTypeParams = genericTypeParams
self.genericWhereClause = genericWhereClause
Expand All @@ -200,7 +196,11 @@ final class MethodModel: Model {
return name(by: level - 1) + postfix
}

func render(with identifier: String, encloser: String, useTemplateFunc: Bool, useMockObservable: Bool, allowSetCallCount: Bool = false, mockFinal: Bool = false, enableFuncArgsHistory: Bool, disableCombineDefaultValues: Bool = false) -> String? {
func render(
context: RenderContext,
arguments: GenerationArguments
) -> String? {
let shouldOverride = context.annotatedTypeKind == .class
if processed {
var prefix = shouldOverride ? "\(String.override) " : ""

Expand All @@ -216,22 +216,14 @@ final class MethodModel: Model {
return nil
}

let result = applyMethodTemplate(name: name,
identifier: identifier,
kind: kind,
useTemplateFunc: useTemplateFunc,
allowSetCallCount: allowSetCallCount,
enableFuncArgsHistory: enableFuncArgsHistory,
isStatic: isStatic,
customModifiers: customModifiers,
isOverride: shouldOverride,
genericTypeParams: genericTypeParams,
genericWhereClause: genericWhereClause,
params: params,
returnType: returnType,
accessLevel: accessLevel,
argsHistory: argsHistory,
handler: handler(encloser: encloser))
return result
guard let overloadingResolvedName = context.overloadingResolvedName else {
return nil
}

return applyMethodTemplate(overloadingResolvedName: overloadingResolvedName,
arguments: arguments,
isOverride: shouldOverride,
handler: handler(),
context: context)
}
}
23 changes: 15 additions & 8 deletions Sources/MockoloFramework/Models/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public enum ModelType {
case closure
}

enum NominalTypeDeclKind: String {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the most places, DeclType has incorrect cases so prepares new enum.

case `class`
case `actor`
case `protocol`
}

struct RenderContext {
var overloadingResolvedName: String?
var enclosingType: SwiftType?
var annotatedTypeKind: NominalTypeDeclKind?
}

/// Represents a model for an entity such as var, func, class, etc.
protocol Model: AnyObject {
/// Identifier
Expand All @@ -45,14 +57,9 @@ protocol Model: AnyObject {
var offset: Int64 { get }

/// Applies a corresponding template to this model to output mocks
func render(with identifier: String,
encloser: String,
useTemplateFunc: Bool,
useMockObservable: Bool,
allowSetCallCount: Bool,
mockFinal: Bool,
enableFuncArgsHistory: Bool,
disableCombineDefaultValues: Bool
func render(
context: RenderContext,
arguments: GenerationArguments
) -> String?

/// Used to differentiate multiple entities with the same name
Expand Down
29 changes: 6 additions & 23 deletions Sources/MockoloFramework/Models/NominalModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@
//

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

let namespaces: [String]
let name: String
let offset: Int64
let type: SwiftType
let attribute: String
let accessLevel: String
let identifier: String
let declTypeOfMockAnnotatedBaseType: DeclType
let declKindOfMockAnnotatedBaseType: NominalTypeDeclKind
let inheritedTypes: [String]
let entities: [(String, Model)]
let initParamCandidates: [VariableModel]
Expand All @@ -42,7 +37,7 @@ final class NominalModel: Model {
init(identifier: String,
namespaces: [String],
acl: String,
declTypeOfMockAnnotatedBaseType: DeclType,
declKindOfMockAnnotatedBaseType: NominalTypeDeclKind,
declKind: NominalTypeDeclKind,
inheritedTypes: [String],
attributes: [String],
Expand All @@ -55,7 +50,7 @@ final class NominalModel: Model {
self.name = metadata?.nameOverride ?? (identifier + "Mock")
self.type = SwiftType(self.name)
self.namespaces = namespaces
self.declTypeOfMockAnnotatedBaseType = declTypeOfMockAnnotatedBaseType
self.declKindOfMockAnnotatedBaseType = declKindOfMockAnnotatedBaseType
self.declKind = declKind
self.inheritedTypes = inheritedTypes
self.entities = entities
Expand All @@ -68,29 +63,17 @@ final class NominalModel: Model {
}

func render(
with identifier: String,
encloser: String,
useTemplateFunc: Bool,
useMockObservable: Bool,
allowSetCallCount: Bool = false,
mockFinal: Bool = false,
enableFuncArgsHistory: Bool = false,
disableCombineDefaultValues: Bool = false
context: RenderContext,
arguments: GenerationArguments
) -> String? {
return applyNominalTemplate(
name: name,
identifier: self.identifier,
accessLevel: accessLevel,
attribute: attribute,
declTypeOfMockAnnotatedBaseType: declTypeOfMockAnnotatedBaseType,
inheritedTypes: inheritedTypes,
metadata: metadata,
useTemplateFunc: useTemplateFunc,
useMockObservable: useMockObservable,
allowSetCallCount: allowSetCallCount,
mockFinal: mockFinal,
enableFuncArgsHistory: enableFuncArgsHistory,
disableCombineDefaultValues: disableCombineDefaultValues,
arguments: arguments,
initParamCandidates: initParamCandidates,
declaredInits: declaredInits,
entities: entities
Expand Down
5 changes: 4 additions & 1 deletion Sources/MockoloFramework/Models/ParamModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ final class ParamModel: Model {
return nil
}

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? {
func render(
context: RenderContext = .init(),
arguments: GenerationArguments = .default
) -> String? {
return applyParamTemplate(name: name, label: label, type: type, inInit: inInit)
}
}
Loading
Loading