Skip to content

Commit 5cd48e4

Browse files
committed
Add more tests cases
1 parent 44e79da commit 5cd48e4

File tree

5 files changed

+259
-33
lines changed

5 files changed

+259
-33
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/basicformat/BasicFormatFile.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,16 @@ let basicFormatFile = SourceFile {
123123

124124
FunctionDecl("open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool") {
125125
SwitchStmt("""
126-
switch (token.tokenKind, token.previousToken(viewMode: .sourceAccurate)?.tokenKind) {
127-
case (.leftAngle, .identifier(_)),
128-
(.rightAngle, .identifier(_)),
129-
(.rightAngle, .postfixQuestionMark):
126+
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
127+
case (.identifier, .leftAngle),
128+
(.identifier, .rightAngle),
129+
(.rightAngle, .exclamationMark),
130+
(.rightAngle, .rightBrace),
131+
(.rightSquareBracket, .rightAngle),
132+
(.postfixQuestionMark, .rightAngle):
133+
return false
134+
case (.leftParen, .spacedBinaryOperator("*")):
130135
return false
131-
case (.spacedBinaryOperator(let `operator`), .leftParen):
132-
return `operator` != "*"
133136
default:
134137
break
135138
}
@@ -154,18 +157,24 @@ let basicFormatFile = SourceFile {
154157
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
155158
case (.asKeyword, .exclamationMark),
156159
(.asKeyword, .postfixQuestionMark),
160+
(.exclamationMark, .leftParen),
161+
(.exclamationMark, .period),
157162
(.initKeyword, .leftParen),
158163
(.initKeyword, .postfixQuestionMark),
159-
(.leftAngle, .identifier(_)),
164+
(.rightAngle, .exclamationMark),
165+
(.leftAngle, .identifier),
166+
(.leftAngle, .leftSquareBracket),
167+
(.leftAngle, .leftBrace),
160168
(.rightAngle, .leftParen),
161169
(.rightAngle, .postfixQuestionMark),
162170
(.postfixQuestionMark, .leftParen),
171+
(.postfixQuestionMark, .rightAngle),
163172
(.postfixQuestionMark, .rightParen),
164173
(.tryKeyword, .exclamationMark),
165174
(.tryKeyword, .postfixQuestionMark):
166175
return false
167-
case (.spacedBinaryOperator(let `operator`), .comma):
168-
return `operator` != "*"
176+
case (.spacedBinaryOperator("*"), .comma):
177+
return false
169178
default:
170179
break
171180
}

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,16 @@ open class BasicFormat: SyntaxRewriter {
124124
}
125125

126126
open func requiresLeadingSpace(_ token: TokenSyntax) -> Bool {
127-
switch (token.tokenKind, token.previousToken(viewMode: .sourceAccurate)?.tokenKind) {
128-
case (.leftAngle, .identifier(_ )),
129-
(.rightAngle, .identifier(_ )),
130-
(.rightAngle, .postfixQuestionMark):
127+
switch (token.previousToken(viewMode: .sourceAccurate)?.tokenKind, token.tokenKind) {
128+
case (.identifier, .leftAngle),
129+
(.identifier, .rightAngle),
130+
(.rightAngle, .exclamationMark),
131+
(.rightAngle, .rightBrace),
132+
(.rightSquareBracket, .rightAngle),
133+
(.postfixQuestionMark, .rightAngle):
134+
return false
135+
case (.leftParen, .spacedBinaryOperator("*")):
131136
return false
132-
case (.spacedBinaryOperator(let `operator`), .leftParen):
133-
return `operator` != "*"
134137
default:
135138
break
136139
}
@@ -162,18 +165,24 @@ open class BasicFormat: SyntaxRewriter {
162165
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
163166
case (.asKeyword, .exclamationMark),
164167
(.asKeyword, .postfixQuestionMark),
168+
(.exclamationMark, .leftParen),
169+
(.exclamationMark, .period),
165170
(.initKeyword, .leftParen),
166171
(.initKeyword, .postfixQuestionMark),
167-
(.leftAngle, .identifier(_ )),
172+
(.rightAngle, .exclamationMark),
173+
(.leftAngle, .identifier),
174+
(.leftAngle, .leftSquareBracket),
175+
(.leftAngle, .leftBrace),
168176
(.rightAngle, .leftParen),
169177
(.rightAngle, .postfixQuestionMark),
170178
(.postfixQuestionMark, .leftParen),
179+
(.postfixQuestionMark, .rightAngle),
171180
(.postfixQuestionMark, .rightParen),
172181
(.tryKeyword, .exclamationMark),
173182
(.tryKeyword, .postfixQuestionMark):
174183
return false
175-
case (.spacedBinaryOperator(let `operator`), .comma):
176-
return `operator` != "*"
184+
case (.spacedBinaryOperator("*"), .comma):
185+
return false
177186
default:
178187
break
179188
}

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 186 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,193 @@ final class FunctionTests: XCTestCase {
3838
}
3939

4040
func testFunctionDeclEnsurePropperSpacing() {
41-
let buildable = FunctionDecl(
42-
"""
43-
@available(*, deprecated, message: "Use function on Baz")
44-
private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
45-
}
46-
"""
47-
)
41+
let testCases: [UInt: (FunctionDecl, String)] = [
42+
#line: (
43+
FunctionDecl(
44+
"""
45+
@available(*, deprecated, message: "Use function on Baz")
46+
private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
47+
}
48+
"""
49+
),
50+
"""
51+
@available(*, deprecated, message: "Use function on Baz")
52+
private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
53+
}
54+
"""
55+
),
56+
#line: (
57+
FunctionDecl(
58+
"""
59+
public static func == (lhs: String, rhs: String) -> Bool {
60+
return lhs < rhs
61+
}
62+
"""
63+
),
64+
"""
65+
public static func == (lhs: String, rhs: String) -> Bool {
66+
return lhs < rhs
67+
}
68+
"""
69+
),
70+
#line: (
71+
FunctionDecl(
72+
"""
73+
public static func == (lhs: String, rhs: String) -> Bool {
74+
return lhs > rhs
75+
}
76+
"""
77+
),
78+
"""
79+
public static func == (lhs: String, rhs: String) -> Bool {
80+
return lhs > rhs
81+
}
82+
"""
83+
),
84+
#line: (
85+
FunctionDecl(
86+
"""
87+
public static func == (lhs1: String, lhs2: String, rhs1: String, rhs2: String) -> Bool {
88+
return (lhs1, lhs2) > (rhs1, rhs2)
89+
}
90+
"""
91+
),
92+
"""
93+
public static func == (lhs1: String, lhs2: String, rhs1: String, rhs2: String) -> Bool {
94+
return (lhs1, lhs2) > (rhs1, rhs2)
95+
}
96+
"""
97+
),
98+
#line: (
99+
FunctionDecl(
100+
"""
101+
public func foo<Generic>(input: Bas) -> Foo<Generic> {
102+
return input as Foo<Generic>!
103+
}
104+
"""
105+
),
106+
"""
107+
public func foo<Generic>(input: Bas) -> Foo<Generic> {
108+
return input as Foo<Generic>!
109+
}
110+
"""
111+
),
112+
#line: (
113+
FunctionDecl(
114+
"""
115+
public func foo<Generic>(input: Bas) -> Foo<Generic?> {
116+
return input as Foo<Generic?>!
117+
}
118+
"""
119+
),
120+
"""
121+
public func foo<Generic>(input: Bas) -> Foo<Generic?> {
122+
return input as Foo<Generic?>!
123+
}
124+
"""
125+
),
126+
#line: (
127+
FunctionDecl(
128+
"""
129+
public func foo<Generic>(input: [Bar]) -> Foo<[Bar]> {
130+
return input
131+
}
132+
"""
133+
),
134+
"""
135+
public func foo<Generic>(input: [Bar]) -> Foo<[Bar]> {
136+
return input
137+
}
138+
"""
139+
),
140+
#line: (
141+
FunctionDecl(
142+
"""
143+
public func foo(myOptionalClosure: MyClosure?) {
144+
myOptionalClosure!()
145+
}
146+
"""
147+
),
148+
"""
149+
public func foo(myOptionalClosure: MyClosure?) {
150+
myOptionalClosure!()
151+
}
152+
"""
153+
),
154+
#line: (
155+
FunctionDeclSyntax(
156+
modifiers: [DeclModifier(name: .public), DeclModifier(name: .static)],
157+
identifier: Token.identifier("=="),
158+
signature: FunctionSignatureSyntax(
159+
input: ParameterClauseSyntax(
160+
parameterList: FunctionParameterListSyntax {
161+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("lhs"), colon: .colon, type: SimpleTypeIdentifier("String"))
162+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("rhs"), colon: .colon, type: SimpleTypeIdentifier("String"))
163+
}
164+
),
165+
output: ReturnClauseSyntax(
166+
returnType: SimpleTypeIdentifier(name: TokenSyntax.identifier("Bool"))
167+
)
168+
),
169+
bodyBuilder: {
170+
ReturnStmt(
171+
expression: SequenceExpr(
172+
elements: ExprListSyntax {
173+
IdentifierExprSyntax(identifier: .identifier("lhs"))
174+
BinaryOperatorExprSyntax(operatorToken: .spacedBinaryOperator("<"))
175+
IdentifierExprSyntax(identifier: .identifier("rhs"))
176+
}
177+
)
178+
)
179+
}
180+
),
181+
"""
182+
public static func ==(lhs: String, rhs: String) -> Bool {
183+
return lhs < rhs
184+
}
185+
"""
186+
),
187+
#line: (
188+
FunctionDeclSyntax(
189+
modifiers: [DeclModifierSyntax(name: .public), DeclModifierSyntax(name: .static)],
190+
identifier: Token.identifier("=="),
191+
signature: FunctionSignatureSyntax(
192+
input: ParameterClauseSyntax(
193+
parameterList: FunctionParameterListSyntax {
194+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("lhs1"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
195+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("lhs2"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
196+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("rhs1"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
197+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("rhs2"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
198+
}
199+
),
200+
output: ReturnClauseSyntax(
201+
returnType: SimpleTypeIdentifierSyntax(name: TokenSyntax.identifier("Bool"))
202+
)
203+
),
204+
bodyBuilder: {
205+
ReturnStmt(
206+
expression: SequenceExprSyntax(
207+
elements: ExprListSyntax {
208+
ExprSyntax("(lhs1, lhs2)")
209+
BinaryOperatorExprSyntax(operatorToken: .spacedBinaryOperator("<"))
210+
ExprSyntax("(rhs1, rhs2)")
211+
}
212+
)
213+
)
214+
}
215+
),
216+
"""
217+
public static func ==(lhs1: String, lhs2: String, rhs1: String, rhs2: String) -> Bool {
218+
return (lhs1, lhs2) < (rhs1, rhs2)
219+
}
220+
"""
221+
),
222+
]
48223

49-
AssertBuildResult(
50-
buildable,
51-
"""
52-
@available(*, deprecated, message: "Use function on Baz")
53-
private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
54-
}
55-
"""
56-
)
224+
for (line, testCase) in testCases {
225+
let (builder, expected) = testCase
226+
AssertBuildResult(builder, expected, line: line)
227+
}
57228
}
58229

59230
func testArguments() {

Tests/SwiftSyntaxBuilderTest/IfStmtTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ final class IfStmtTests: XCTestCase {
8585
}
8686
}
8787

88+
func testIfStmtSpacing() {
89+
let testCases: [UInt: (IfStmt, String)] = [
90+
#line: (
91+
IfStmt(conditions: ConditionElementList { Expr("!(true)") }) {},
92+
"""
93+
if !(true) {
94+
}
95+
"""
96+
),
97+
#line: (
98+
IfStmt(
99+
"""
100+
if !(false) {
101+
}
102+
"""
103+
),
104+
"""
105+
if !(false) {
106+
}
107+
"""
108+
),
109+
]
110+
111+
for (line, testCase) in testCases {
112+
let (builder, expected) = testCase
113+
AssertBuildResult(builder, expected, line: line)
114+
}
115+
}
116+
88117
func testIfElseStmt() {
89118
// Use the convenience initializer from IfStmtConvenienceInitializers
90119
// with an else branch expressed by a second trailing closure.

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ final class VariableTests: XCTestCase {
3939
VariableDecl("var newLayout: ContiguousArray<RawSyntax?>?"),
4040
"var newLayout: ContiguousArray<RawSyntax?>?"
4141
),
42+
#line: (
43+
VariableDecl("var foo: String { myOptional!.someProperty }"),
44+
"""
45+
var foo: String {
46+
myOptional!.someProperty
47+
}
48+
"""
49+
),
4250
]
4351

4452
for (line, testCase) in testCases {

0 commit comments

Comments
 (0)