File tree 8 files changed +70
-18
lines changed
SageSwiftKitMacros/MockableMacros
Tests/SageSwiftKitTests/MockableMacrosTests
8 files changed +70
-18
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
5
5
The format is based on [ Keep a Changelog] ( https://keepachangelog.com/en/1.1.0/ ) ,
6
6
and this project adheres to [ Semantic Versioning] ( https://semver.org/spec/v2.0.0.html ) .
7
7
8
+ # [ 0.2.4] - 2024-11-14
9
+
10
+ ### Fixed
11
+
12
+ - Add support to thrown functions
13
+ - Fix support to no optional properties
14
+
8
15
# [ 0.2.3] - 2024-11-06
9
16
10
17
### Fixed
Original file line number Diff line number Diff line change @@ -37,14 +37,7 @@ struct PlayingObject {
37
37
}
38
38
39
39
40
-
41
- protocol Foo {
42
- }
43
-
44
- protocol FooA {
45
- }
46
-
47
40
@AutoMockable ( )
48
- protocol Bar : Foo , FooA {
49
- var foo : String { set get }
41
+ public protocol DateSelectorViewModel {
42
+ func make ( ) throws -> String
50
43
}
Original file line number Diff line number Diff line change @@ -109,8 +109,8 @@ public enum AutoMockable: PeerMacro {
109
109
accessLevel: accessLevel. tokenSyntax
110
110
)
111
111
112
- varConformance. build ( )
113
112
varConformance. buildReturnVar ( )
113
+ varConformance. build ( )
114
114
}
115
115
116
116
// Implementation of each function
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import SwiftSyntaxBuilder
11
11
struct ClassMockForFunctionBuilder {
12
12
let funcData : FunctionsMockData
13
13
14
- var parametersName : String { " Parameters " }
14
+ var parametersName : String { " ParametersMock " }
15
15
var callsName : String { " calls " }
16
16
17
17
init ( funcData: FunctionsMockData ) {
@@ -51,6 +51,21 @@ struct ClassMockForFunctionBuilder {
51
51
)
52
52
}
53
53
54
+ if funcData. needThrows {
55
+ VariableDeclSyntax (
56
+ modifiers: . init( itemsBuilder: {
57
+ . init( name: funcData. accessLevel)
58
+ } ) ,
59
+ Keyword . var,
60
+ name: " returnError " ,
61
+ type: TypeAnnotationSyntax (
62
+ type: OptionalTypeSyntax (
63
+ wrappedType: IdentifierTypeSyntax ( name: " Error " )
64
+ )
65
+ )
66
+ )
67
+ }
68
+
54
69
InitializerDeclSyntax (
55
70
signature: . init( parameterClause: . init(
56
71
parameters: . init( itemsBuilder: {
Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ struct ProtocolFunctionsConformanceBuilder {
29
29
body: . init( statements: . init( itemsBuilder: {
30
30
buildCall ( )
31
31
32
+ if data. needThrows {
33
+ buildThrow ( )
34
+ }
35
+
32
36
if data. returnValue != nil {
33
37
buildReturn ( )
34
38
}
@@ -66,6 +70,32 @@ struct ProtocolFunctionsConformanceBuilder {
66
70
)
67
71
}
68
72
73
+ private func buildThrow( ) -> CodeBlockItemSyntax {
74
+ CodeBlockItemSyntax (
75
+ item: . expr( ExprSyntax (
76
+ fromProtocol: IfExprSyntax (
77
+ conditions: ConditionElementListSyntax ( itemsBuilder: {
78
+ OptionalBindingConditionSyntax (
79
+ bindingSpecifier: . keyword( . let) ,
80
+ pattern: IdentifierPatternSyntax ( identifier: " error " ) ,
81
+ initializer: InitializerClauseSyntax (
82
+ equal: . equalToken( ) ,
83
+ value: DeclReferenceExprSyntax (
84
+ baseName: " \( mockEntity) .returnError " . tokenSyntax
85
+ )
86
+ )
87
+ )
88
+ } ) ,
89
+ body: CodeBlockSyntax (
90
+ statements: . init( itemsBuilder: {
91
+ ThrowStmtSyntax ( expression: DeclReferenceExprSyntax ( baseName: " error " ) )
92
+ } )
93
+ )
94
+ )
95
+ ) )
96
+ )
97
+ }
98
+
69
99
private func buildReturn( ) -> ReturnStmtSyntax {
70
100
return ReturnStmtSyntax (
71
101
returnKeyword: . keyword( . return) ,
Original file line number Diff line number Diff line change @@ -24,11 +24,16 @@ struct ProtocolVarsConformanceBuilder {
24
24
return nil
25
25
}
26
26
27
- guard let typeSyntax = myType. type. as ( IdentifierTypeSyntax . self) else {
28
- return myType
27
+ if let optional = myType. type. as ( OptionalTypeSyntax . self) {
28
+ return TypeAnnotationSyntax ( type : optional )
29
29
}
30
30
31
- return TypeAnnotationSyntax ( type: TypeSyntax ( stringLiteral: " \( typeSyntax. name. text) ! " ) )
31
+ return TypeAnnotationSyntax (
32
+ type: ImplicitlyUnwrappedOptionalTypeSyntax (
33
+ wrappedType: myType. type. trimmed,
34
+ exclamationMark: . exclamationMarkToken( )
35
+ )
36
+ )
32
37
}
33
38
34
39
init (
Original file line number Diff line number Diff line change @@ -33,6 +33,8 @@ struct FunctionsMockData {
33
33
34
34
var mocksVarName : String { " mock " }
35
35
36
+ var needThrows : Bool { syntax. signature. effectSpecifiers? . throwsSpecifier != nil }
37
+
36
38
init ( syntax: FunctionDeclSyntax , accessLevel: TokenSyntax ) {
37
39
self . syntax = syntax
38
40
self . name = syntax. name
Original file line number Diff line number Diff line change @@ -35,11 +35,11 @@ final class MockableMacrosTests: XCTestCase {
35
35
internal init() {
36
36
}
37
37
internal class TmpFunc_Value {
38
- internal struct Parameters {
38
+ internal struct ParametersMock {
39
39
internal let value: String
40
40
}
41
- internal var calls: [Parameters ] = []
42
- internal var lastCall: Parameters ? {
41
+ internal var calls: [ParametersMock ] = []
42
+ internal var lastCall: ParametersMock ? {
43
43
return self.calls.last
44
44
}
45
45
internal var called: Bool {
@@ -53,6 +53,7 @@ final class MockableMacrosTests: XCTestCase {
53
53
internal var tmpFunc_Value = TmpFunc_Value()
54
54
}
55
55
internal var mock = FunctionMocks()
56
+ internal var valueReturn: String?
56
57
internal var value: String? {
57
58
get {
58
59
return valueReturn
@@ -61,7 +62,6 @@ final class MockableMacrosTests: XCTestCase {
61
62
self.valueReturn = newValue
62
63
}
63
64
}
64
- internal var valueReturn: String?
65
65
internal func tmpFunc(value: String) -> Int {
66
66
self.mock.tmpFunc_Value.calls.append(.init(value: value))
67
67
return self.mock.tmpFunc_Value.returnValue
You can’t perform that action at this time.
0 commit comments