Skip to content

Commit 5ac90d9

Browse files
authored
fix bug with missing 'async' keyword for test method #16 (#19)
1 parent 95e384a commit 5ac90d9

File tree

5 files changed

+192
-15
lines changed

5 files changed

+192
-15
lines changed

Sources/XCTestParametrizedMacroMacros/MacroDeclarationHelper.swift

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ struct MacroDeclarationHelper {
3636
declaration.signature.parameterClause.parameters.last?.type
3737
}
3838

39+
/// Returns 'FunctionEffectSpecifiersSyntax' from method declaration.
40+
var effectSpecifiers: FunctionEffectSpecifiersSyntax? {
41+
declaration.signature.effectSpecifiers
42+
}
43+
3944
var firstAttribute: AttributeSyntax? {
4045
return declaration.attributes.first?.as(AttributeSyntax.self)
4146
}

Sources/XCTestParametrizedMacroMacros/TestMethodsFactory.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct TestMethodsFactory {
3939
return try zip(input, zip(output, labels)).map { (input, arg) in
4040
let (output, label) = arg
4141
return """
42-
\(raw: buildTestMethodSignature(funcName: funcName, inputParamName: inputParamName, inputObject: input, outputParamName: outputParamName, outputObject: output, label: label))
42+
\(raw: buildTestMethodSignature(funcName: funcName, inputParamName: inputParamName, inputObject: input, outputParamName: outputParamName, outputObject: output, label: label, effectSpecifiers: macroDeclarationHelper.effectSpecifiers))
4343
\(raw: buildLocalVariables(inputParamName: inputParamName,
4444
inputParamType: inputParamType,
4545
inputObject: input,
@@ -58,14 +58,15 @@ struct TestMethodsFactory {
5858
inputObject: ArrayElementListSyntax.Element,
5959
outputParamName: String? = nil,
6060
outputObject: ArrayElementListSyntax.Element? = nil,
61-
label: ArrayElementListSyntax.Element? = nil ) -> String {
61+
label: ArrayElementListSyntax.Element? = nil,
62+
effectSpecifiers: FunctionEffectSpecifiersSyntax?) -> String {
6263
guard label == nil else {
63-
return "func \(funcName)_\(label!.asFunctionName)() throws {"
64+
return "func \(funcName)_\(label!.asFunctionName)() \(effectSpecifiers?.description ?? ""){"
6465
}
6566
if let outputParamName = outputParamName, let outputObject = outputObject {
66-
return "func \(funcName)_\(inputParamName.capitalizedFirst)_\(inputObject.asFunctionName)_\(outputParamName.capitalizedFirst)_\(outputObject.asFunctionName)() throws {"
67+
return "func \(funcName)_\(inputParamName.capitalizedFirst)_\(inputObject.asFunctionName)_\(outputParamName.capitalizedFirst)_\(outputObject.asFunctionName)() \(effectSpecifiers?.description ?? ""){"
6768
} else {
68-
return "func \(funcName)_\(inputParamName.capitalizedFirst)_\(inputObject.asFunctionName)() throws {"
69+
return "func \(funcName)_\(inputParamName.capitalizedFirst)_\(inputObject.asFunctionName)() \(effectSpecifiers?.description ?? ""){"
6970
}
7071
}
7172

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import SwiftSyntaxMacros
2+
import SwiftSyntaxMacrosTestSupport
3+
import XCTest
4+
5+
import XCTestParametrizedMacroMacros
6+
7+
final class EffectSpecifiersTests: XCTestCase {
8+
9+
let testMacros: [String: Macro.Type] = [
10+
"Parametrize": ParametrizeMacro.self,
11+
]
12+
13+
func testWithInputNoSpecifiers() throws {
14+
assertMacroExpansion(
15+
"""
16+
struct TestStruct {
17+
@Parametrize(input: [1])
18+
func testMethod(input n: Int) {
19+
XCTAssertTrue(n>0)
20+
}
21+
}
22+
""",
23+
expandedSource: """
24+
struct TestStruct {
25+
func testMethod(input n: Int) {
26+
XCTAssertTrue(n>0)
27+
}
28+
29+
func testMethod_N_1() {
30+
let n: Int = 1
31+
XCTAssertTrue(n > 0)
32+
}
33+
}
34+
""",
35+
macros: testMacros
36+
)
37+
}
38+
39+
func testWithInputAsyncAndThrows() throws {
40+
assertMacroExpansion(
41+
"""
42+
struct TestStruct {
43+
@Parametrize(input: [1])
44+
func testMethod(input n: Int) async throws {
45+
XCTAssertTrue(n>0)
46+
}
47+
}
48+
""",
49+
expandedSource: """
50+
struct TestStruct {
51+
func testMethod(input n: Int) async throws {
52+
XCTAssertTrue(n>0)
53+
}
54+
55+
func testMethod_N_1() async throws {
56+
let n: Int = 1
57+
XCTAssertTrue(n > 0)
58+
}
59+
}
60+
""",
61+
macros: testMacros
62+
)
63+
}
64+
65+
func testWithInputAsync() throws {
66+
assertMacroExpansion(
67+
"""
68+
struct TestStruct {
69+
@Parametrize(input: [1])
70+
func testMethod(input n: Int) async {
71+
XCTAssertTrue(n>0)
72+
}
73+
}
74+
""",
75+
expandedSource: """
76+
struct TestStruct {
77+
func testMethod(input n: Int) async {
78+
XCTAssertTrue(n>0)
79+
}
80+
81+
func testMethod_N_1() async {
82+
let n: Int = 1
83+
XCTAssertTrue(n > 0)
84+
}
85+
}
86+
""",
87+
macros: testMacros
88+
)
89+
}
90+
91+
func testWithInputOutputNoSpecifiers() throws {
92+
assertMacroExpansion(
93+
"""
94+
struct TestStruct {
95+
@Parametrize(input: [1], output: [1])
96+
func testMethod(input n: Int, output r: Int) {
97+
XCTAssertEqual(n,r)
98+
}
99+
}
100+
""",
101+
expandedSource: """
102+
struct TestStruct {
103+
func testMethod(input n: Int, output r: Int) {
104+
XCTAssertEqual(n,r)
105+
}
106+
107+
func testMethod_N_1_R_1() {
108+
let n: Int = 1
109+
let r: Int = 1
110+
XCTAssertEqual(n, r)
111+
}
112+
}
113+
""",
114+
macros: testMacros
115+
)
116+
}
117+
118+
func testWithInputOutputAsync() throws {
119+
assertMacroExpansion(
120+
"""
121+
struct TestStruct {
122+
@Parametrize(input: [1], output: [1])
123+
func testMethod(input n: Int, output r: Int) async {
124+
XCTAssertEqual(n,r)
125+
}
126+
}
127+
""",
128+
expandedSource: """
129+
struct TestStruct {
130+
func testMethod(input n: Int, output r: Int) async {
131+
XCTAssertEqual(n,r)
132+
}
133+
134+
func testMethod_N_1_R_1() async {
135+
let n: Int = 1
136+
let r: Int = 1
137+
XCTAssertEqual(n, r)
138+
}
139+
}
140+
""",
141+
macros: testMacros
142+
)
143+
}
144+
145+
func testWithInputOutputLabelsAsyncThrows() throws {
146+
assertMacroExpansion(
147+
"""
148+
struct TestStruct {
149+
@Parametrize(input: [1], output: [1], labels: ["One"])
150+
func testMethod(input n: Int, output r: Int) async throws {
151+
XCTAssertEqual(n,r)
152+
}
153+
}
154+
""",
155+
expandedSource: """
156+
struct TestStruct {
157+
func testMethod(input n: Int, output r: Int) async throws {
158+
XCTAssertEqual(n,r)
159+
}
160+
161+
func testMethod_One() async throws {
162+
let n: Int = 1
163+
let r: Int = 1
164+
XCTAssertEqual(n, r)
165+
}
166+
}
167+
""",
168+
macros: testMacros
169+
)
170+
}
171+
}

Tests/XCTestParametrizedMacroTests/InputOutputParametersTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class InputOutputParametersTests: XCTestCase {
2626
XCTAssertEqual(pow2(n),result)
2727
}
2828
29-
func testPow2_N_3_Result_9() throws {
29+
func testPow2_N_3_Result_9() {
3030
let n: Int = 3
3131
let result: Int = 9
3232
XCTAssertEqual(pow2(n), result)
@@ -42,14 +42,14 @@ final class InputOutputParametersTests: XCTestCase {
4242
"""
4343
struct TestStruct {
4444
@Parametrize(input: [4, 5], output: [16, 25])
45-
func testPow2(input n: Int, output result: Int) {
45+
func testPow2(input n: Int, output result: Int) throws {
4646
XCTAssertEqual(pow2(n),result)
4747
}
4848
}
4949
""",
5050
expandedSource: """
5151
struct TestStruct {
52-
func testPow2(input n: Int, output result: Int) {
52+
func testPow2(input n: Int, output result: Int) throws {
5353
XCTAssertEqual(pow2(n),result)
5454
}
5555
@@ -86,13 +86,13 @@ final class InputOutputParametersTests: XCTestCase {
8686
XCTAssertEqual(word.count, length)
8787
}
8888
89-
func testWordLength_Word_Swift_Length_5() throws {
89+
func testWordLength_Word_Swift_Length_5() {
9090
let word: String = "Swift"
9191
let length: Int = 5
9292
XCTAssertEqual(word.count, length)
9393
}
9494
95-
func testWordLength_Word_SwiftMacro_Length_10() throws {
95+
func testWordLength_Word_SwiftMacro_Length_10() {
9696
let word: String = "SwiftMacro"
9797
let length: Int = 10
9898
XCTAssertEqual(word.count, length)

Tests/XCTestParametrizedMacroTests/LabelsParameterTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ final class LabelsParameterTests: XCTestCase {
1515
"""
1616
struct TestStruct {
1717
@Parametrize(input: [3.1415], labels: ["Pi"])
18-
func testValidateDouble(input n: Double) {
18+
func testValidateDouble(input n: Double) throws {
1919
XCTAssertNotNil(validate_number(n))
2020
}
2121
}
2222
""",
2323
expandedSource: """
2424
struct TestStruct {
25-
func testValidateDouble(input n: Double) {
25+
func testValidateDouble(input n: Double) throws {
2626
XCTAssertNotNil(validate_number(n))
2727
}
2828
@@ -52,7 +52,7 @@ final class LabelsParameterTests: XCTestCase {
5252
XCTAssertEqual(pow2(n),result)
5353
}
5454
55-
func testPow2_ThreePowerOfTheTwo() throws {
55+
func testPow2_ThreePowerOfTheTwo() {
5656
let n: Int = 3
5757
let result: Int = 9
5858
XCTAssertEqual(pow2(n), result)
@@ -79,13 +79,13 @@ final class LabelsParameterTests: XCTestCase {
7979
XCTAssertEqual(pow2(n),result)
8080
}
8181
82-
func testPow2_ThreePowerOfTheTwo() throws {
82+
func testPow2_ThreePowerOfTheTwo() {
8383
let n: Int = 3
8484
let result: Int = 9
8585
XCTAssertEqual(pow2(n), result)
8686
}
8787
88-
func testPow2_FourPowerOfTheTwo() throws {
88+
func testPow2_FourPowerOfTheTwo() {
8989
let n: Int = 4
9090
let result: Int = 16
9191
XCTAssertEqual(pow2(n), result)

0 commit comments

Comments
 (0)