Skip to content

Commit

Permalink
fix: automockable no optional properties and throw functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-T-Dev committed Nov 14, 2024
1 parent c37cddc commit e0b75c4
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 18 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [0.2.4] - 2024-11-14

### Fixed

- Add support to thrown functions
- Fix support to no optional properties

# [0.2.3] - 2024-11-06

### Fixed
Expand Down
11 changes: 2 additions & 9 deletions Sources/SageSwiftKitClient/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ struct PlayingObject {
}



protocol Foo {
}

protocol FooA {
}

@AutoMockable()
protocol Bar: Foo, FooA {
var foo: String { set get }
public protocol DateSelectorViewModel {
func make() throws -> String
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public enum AutoMockable: PeerMacro {
accessLevel: accessLevel.tokenSyntax
)

varConformance.build()
varConformance.buildReturnVar()
varConformance.build()
}

// Implementation of each function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftSyntaxBuilder
struct ClassMockForFunctionBuilder {
let funcData: FunctionsMockData

var parametersName: String { "Parameters" }
var parametersName: String { "ParametersMock" }
var callsName: String { "calls" }

init(funcData: FunctionsMockData) {
Expand Down Expand Up @@ -51,6 +51,21 @@ struct ClassMockForFunctionBuilder {
)
}

if funcData.needThrows {
VariableDeclSyntax(
modifiers: .init(itemsBuilder: {
.init(name: funcData.accessLevel)
}),
Keyword.var,
name: "returnError",
type: TypeAnnotationSyntax(
type: OptionalTypeSyntax(
wrappedType: IdentifierTypeSyntax(name: "Error")
)
)
)
}

InitializerDeclSyntax(
signature: .init(parameterClause: .init(
parameters: .init(itemsBuilder: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct ProtocolFunctionsConformanceBuilder {
body: .init(statements: .init(itemsBuilder: {
buildCall()

if data.needThrows {
buildThrow()
}

if data.returnValue != nil {
buildReturn()
}
Expand Down Expand Up @@ -66,6 +70,32 @@ struct ProtocolFunctionsConformanceBuilder {
)
}

private func buildThrow() -> CodeBlockItemSyntax {
CodeBlockItemSyntax(
item: .expr(ExprSyntax(
fromProtocol: IfExprSyntax(
conditions: ConditionElementListSyntax(itemsBuilder: {
OptionalBindingConditionSyntax(
bindingSpecifier: .keyword(.let),
pattern: IdentifierPatternSyntax(identifier: "error"),
initializer: InitializerClauseSyntax(
equal: .equalToken(),
value: DeclReferenceExprSyntax(
baseName: "\(mockEntity).returnError".tokenSyntax
)
)
)
}),
body: CodeBlockSyntax(
statements: .init(itemsBuilder: {
ThrowStmtSyntax(expression: DeclReferenceExprSyntax(baseName: "error"))
})
)
)
))
)
}

private func buildReturn() -> ReturnStmtSyntax {
return ReturnStmtSyntax(
returnKeyword: .keyword(.return),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ struct ProtocolVarsConformanceBuilder {
return nil
}

guard let typeSyntax = myType.type.as(IdentifierTypeSyntax.self) else {
return myType
if let optional = myType.type.as(OptionalTypeSyntax.self) {
return TypeAnnotationSyntax(type: optional)
}

return TypeAnnotationSyntax(type: TypeSyntax(stringLiteral: "\(typeSyntax.name.text)!"))
return TypeAnnotationSyntax(
type: ImplicitlyUnwrappedOptionalTypeSyntax(
wrappedType: myType.type.trimmed,
exclamationMark: .exclamationMarkToken()
)
)
}

init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct FunctionsMockData {

var mocksVarName: String { "mock" }

var needThrows: Bool { syntax.signature.effectSpecifiers?.throwsSpecifier != nil }

init(syntax: FunctionDeclSyntax, accessLevel: TokenSyntax) {
self.syntax = syntax
self.name = syntax.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ final class MockableMacrosTests: XCTestCase {
internal init() {
}
internal class TmpFunc_Value {
internal struct Parameters {
internal struct ParametersMock {
internal let value: String
}
internal var calls: [Parameters] = []
internal var lastCall: Parameters? {
internal var calls: [ParametersMock] = []
internal var lastCall: ParametersMock? {
return self.calls.last
}
internal var called: Bool {
Expand All @@ -53,6 +53,7 @@ final class MockableMacrosTests: XCTestCase {
internal var tmpFunc_Value = TmpFunc_Value()
}
internal var mock = FunctionMocks()
internal var valueReturn: String?
internal var value: String? {
get {
return valueReturn
Expand All @@ -61,7 +62,6 @@ final class MockableMacrosTests: XCTestCase {
self.valueReturn = newValue
}
}
internal var valueReturn: String?
internal func tmpFunc(value: String) -> Int {
self.mock.tmpFunc_Value.calls.append(.init(value: value))
return self.mock.tmpFunc_Value.returnValue
Expand Down

0 comments on commit e0b75c4

Please sign in to comment.