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

Add labels to history tuple array #284

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 10 additions & 8 deletions Sources/MockoloFramework/Models/ArgumentsHistoryModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class ArgumentsHistoryModel: Model {
let name: String
let capturedValueType: SwiftType
let offset: Int64 = .max
let capturableParams: [(String, SwiftType)]
let capturableParamLabels: [String]
let isHistoryAnnotated: Bool

var modelType: ModelType {
Expand All @@ -35,10 +35,12 @@ final class ArgumentsHistoryModel: Model {
self.name = name + .argsHistorySuffix
self.isHistoryAnnotated = isHistoryAnnotated

self.capturableParams = capturables.map { ($0.name.safeName, $0.type) }

let genericTypeNameList = genericTypeParams.map(\.name)
self.capturedValueType = SwiftType.toArgumentsCaptureType(with: capturableParams.map(\.1), typeParams: genericTypeNameList)
self.capturableParamLabels = capturables.map(\.name.safeName)

self.capturedValueType = SwiftType.toArgumentsCaptureType(
with: capturables.map { ($0.name, $0.type) },
typeParams: genericTypeParams.map(\.name)
)
}

func enable(force: Bool) -> Bool {
Expand All @@ -56,11 +58,11 @@ final class ArgumentsHistoryModel: Model {
return nil
}

switch capturableParams.count {
switch capturableParamLabels.count {
case 1:
return "\(overloadingResolvedName)\(String.argsHistorySuffix).append(\(capturableParams[0].0))"
return "\(overloadingResolvedName)\(String.argsHistorySuffix).append(\(capturableParamLabels[0]))"
case 2...:
let paramNamesStr = capturableParams.map(\.0).joined(separator: ", ")
let paramNamesStr = capturableParamLabels.joined(separator: ", ")
return "\(overloadingResolvedName)\(String.argsHistorySuffix).append((\(paramNamesStr)))"
default:
fatalError("paramNames must not be empty.")
Expand Down
27 changes: 19 additions & 8 deletions Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ extension SubscriptDeclSyntax {
func model(with acl: String, declKind: NominalTypeDeclKind, processed: Bool) -> Model {
let isStatic = self.modifiers.isStatic

let params = self.parameterClause.parameters.compactMap { $0.model(inInit: false, declKind: declKind) }
let params = self.parameterClause.parameters.enumerated().compactMap {
$1.model(inInit: false, declKind: declKind, index: $0)
}
let genericTypeParams = self.genericParameterClause?.parameters.compactMap { $0.model(inInit: false) } ?? []
let genericWhereClause = self.genericWhereClause?.description

Expand Down Expand Up @@ -518,7 +520,9 @@ extension FunctionDeclSyntax {
func model(with acl: String, declKind: NominalTypeDeclKind, funcsWithArgsHistory: [String]?, customModifiers: [String : Modifier]?, processed: Bool) -> Model {
let isStatic = self.modifiers.isStatic

let params = self.signature.parameterClause.parameters.compactMap { $0.model(inInit: false, declKind: declKind) }
let params = self.signature.parameterClause.parameters.enumerated().compactMap {
$1.model(inInit: false, declKind: declKind, index: $0)
}
let genericTypeParams = self.genericParameterClause?.parameters.compactMap { $0.model(inInit: false) } ?? []
let genericWhereClause = self.genericWhereClause?.description

Expand Down Expand Up @@ -560,7 +564,9 @@ extension InitializerDeclSyntax {
func model(with acl: String, declKind: NominalTypeDeclKind, processed: Bool) -> Model {
let requiredInit = isRequired(with: declKind)

let params = self.signature.parameterClause.parameters.compactMap { $0.model(inInit: true, declKind: declKind) }
let params = self.signature.parameterClause.parameters.enumerated().compactMap {
$1.model(inInit: true, declKind: declKind, index: $0)
}
let genericTypeParams = self.genericParameterClause?.parameters.compactMap { $0.model(inInit: true) } ?? []
let genericWhereClause = self.genericWhereClause?.description

Expand Down Expand Up @@ -599,19 +605,24 @@ extension GenericParameterSyntax {
}

extension FunctionParameterSyntax {
func model(inInit: Bool, declKind: NominalTypeDeclKind) -> ParamModel {
var label = ""
var name = ""
func model(inInit: Bool, declKind: NominalTypeDeclKind, index: Int) -> ParamModel {
let label: String
let name: String
// Get label and name of args
let first = self.firstName.text
if let second = self.secondName?.text {
label = first
name = second
if second == "_" {
name = "_\(index)"
} else {
name = second
}
} else {
if first == "_" {
label = first
name = first + "arg"
name = "_\(index)"
} else {
label = ""
name = first
}
}
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 @@ -79,7 +79,7 @@ extension MethodModel {
if context.requiresSendable {
let paramNamesStr: String?
if let argsHistory = model.argsHistory, argsHistory.enable(force: arguments.enableFuncArgsHistory) {
paramNamesStr = argsHistory.capturableParams.map(\.0).joined(separator: ", ")
paramNamesStr = argsHistory.capturableParamLabels.joined(separator: ", ")
} else {
paramNamesStr = nil
}
Expand Down
14 changes: 9 additions & 5 deletions Sources/MockoloFramework/Utils/TypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,11 @@ public final class SwiftType {
return SwiftType(typeStr, cast: returnTypeCast)
}

static func toArgumentsCaptureType(with params: [SwiftType], typeParams: [String]) -> SwiftType {
static func toArgumentsCaptureType(with params: [(label: String, type: SwiftType)], typeParams: [String]) -> SwiftType {
assert(!params.isEmpty)

// Expected only history capturable types.
let displayableParamTypes = params.compactMap { (subtype: SwiftType) -> String? in
let displayableParamTypes = params.map { $0.type }.compactMap { (subtype: SwiftType) -> String? in
var processedType = subtype.processTypeParams(with: typeParams)

if subtype.isInOut {
Expand All @@ -556,13 +558,15 @@ public final class SwiftType {

return processedType
}

let displayableParamStr = displayableParamTypes.joined(separator: ", ")

if displayableParamTypes.count >= 2 {
let displayableParamStr = zip(params.map(\.label), displayableParamTypes).map {
"\($0): \($1)"
}
.joined(separator: ", ")
return SwiftType("(\(displayableParamStr))")
} else {
return SwiftType(displayableParamStr)
return SwiftType(displayableParamTypes[0])
}
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/TestArgumentsHistory/ArgumentsHistoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ class ArgumentsHistoryTests: MockoloTestCase {
dstContent: argumentsHistoryStaticCase.expected._source,
enableFuncArgsHistory: true)
}

func testArgumentsHistoryLabels() {
verify(srcContent: argumentsHistoryLabels._source,
dstContent: argumentsHistoryLabels.expected._source,
enableFuncArgsHistory: true)
}
}
51 changes: 43 additions & 8 deletions Tests/TestArgumentsHistory/FixtureArgumentsHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}

private(set) var bazFuncCallCount = 0
var bazFuncArgValues = [(String, Float)]()
var bazFuncArgValues = [(arg: String, default: Float)]()
var bazFuncHandler: ((String, Float) -> ())?
func bazFunc(arg: String, default: Float) {
bazFuncCallCount += 1
Expand Down Expand Up @@ -83,7 +83,7 @@
}

private(set) var bazFuncCallCount = 0
var bazFuncArgValues = [(String, Float)]()
var bazFuncArgValues = [(arg: String, default: Float)]()
var bazFuncHandler: ((String, Float) -> ())?
func bazFunc(arg: String, default: Float) {
bazFuncCallCount += 1
Expand Down Expand Up @@ -159,7 +159,7 @@
}

private(set) var quuxFuncCallCount = 0
var quuxFuncArgValues = [(String, Float)]()
var quuxFuncArgValues = [(val1: String, val2: Float)]()
var quuxFuncHandler: ((String, Float) -> ())?
func quuxFunc(val1: String, val2: Float) {
quuxFuncCallCount += 1
Expand All @@ -173,9 +173,6 @@
}
}

let argumentsHistorySimpleCaseMock = """
"""

@Fixture enum argumentsHistoryTupleCase {
/// @mockable(history: fooFunc = true)
protocol Foo {
Expand All @@ -200,7 +197,7 @@ let argumentsHistorySimpleCaseMock = """
}

private(set) var barFuncCallCount = 0
var barFuncArgValues = [((bar1: Int, String), (bar3: Int, bar4: String))]()
var barFuncArgValues = [(val1: (bar1: Int, String), val2: (bar3: Int, bar4: String))]()
var barFuncHandler: (((bar1: Int, String), (bar3: Int, bar4: String)) -> ())?
func barFunc(val1: (bar1: Int, String), val2: (bar3: Int, bar4: String)) {
barFuncCallCount += 1
Expand Down Expand Up @@ -289,7 +286,7 @@ let argumentsHistorySimpleCaseMock = """
init() { }

private(set) var fooFuncCallCount = 0
var fooFuncArgValues = [(Any, Any?)]()
var fooFuncArgValues = [(val1: Any, val2: Any?)]()
var fooFuncHandler: ((Any, Any?) -> ())?
func fooFunc<T: StringProtocol>(val1: T, val2: T?) {
fooFuncCallCount += 1
Expand Down Expand Up @@ -477,3 +474,41 @@ let argumentsHistorySimpleCaseMock = """
}
}
}

@Fixture enum argumentsHistoryLabels {
/// @mockable
protocol Foo {
func foo(arg0: Int, _ arg1: Double, first throws: String)
func bar(_: Int, _: Void, _ _: Void)
}

@Fixture enum expected {
class FooMock: Foo {
init() { }

private(set) var fooCallCount = 0
var fooArgValues = [(arg0: Int, arg1: Double, throws: String)]()
var fooHandler: ((Int, Double, String) -> ())?
func foo(arg0: Int, _ arg1: Double, first throws: String) {
fooCallCount += 1
fooArgValues.append((arg0, arg1, `throws`))
if let fooHandler = fooHandler {
fooHandler(arg0, arg1, `throws`)
}

}

private(set) var barCallCount = 0
var barArgValues = [(_0: Int, _1: Void, _2: Void)]()
var barHandler: ((Int, Void, Void) -> ())?
func bar(_ _0: Int, _ _1: Void, _ _2: Void) {
barCallCount += 1
barArgValues.append((_0, _1, _2))
if let barHandler = barHandler {
barHandler(_0, _1, _2)
}

}
}
}
}
4 changes: 2 additions & 2 deletions Tests/TestSendable/FixtureSendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public final class SendableProtocolMock: SendableProtocol, @unchecked Sendable {
return ""
}

private let updateArg0State = MockoloMutex(MockoloHandlerState<(Any, AnyObject), @Sendable (Any, AnyObject) async throws -> ()>())
private let updateArg0State = MockoloMutex(MockoloHandlerState<(arg0: Any, arg1: AnyObject), @Sendable (Any, AnyObject) async throws -> ()>())
public var updateArg0CallCount: Int {
return updateArg0State.withLock(\.callCount)
}
public var updateArg0ArgValues: [(Any, AnyObject)] {
public var updateArg0ArgValues: [(arg0: Any, arg1: AnyObject)] {
return updateArg0State.withLock(\.argValues).map(\.value)
}
public var updateArg0Handler: (@Sendable (Any, AnyObject) async throws -> ())? {
Expand Down