Skip to content

Commit e7abba8

Browse files
committed
Add more tests cases
1 parent a94f590 commit e7abba8

File tree

5 files changed

+197
-25
lines changed

5 files changed

+197
-25
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ 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+
(.postfixQuestionMark, .leftAngle),
130+
(.postfixQuestionMark, .rightAngle):
130131
return false
131-
case (.spacedBinaryOperator(let `operator`), .leftParen):
132+
case (.leftParen, .spacedBinaryOperator(let `operator`)):
132133
return `operator` != "*"
133134
default:
134135
break
@@ -154,12 +155,14 @@ let basicFormatFile = SourceFile {
154155
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
155156
case (.asKeyword, .exclamationMark),
156157
(.asKeyword, .postfixQuestionMark),
158+
(.exclamationMark, .period),
157159
(.initKeyword, .leftParen),
158160
(.initKeyword, .postfixQuestionMark),
159161
(.leftAngle, .identifier(_)),
160162
(.rightAngle, .leftParen),
161163
(.rightAngle, .postfixQuestionMark),
162164
(.postfixQuestionMark, .leftParen),
165+
(.postfixQuestionMark, .rightAngle),
163166
(.postfixQuestionMark, .rightParen),
164167
(.tryKeyword, .exclamationMark),
165168
(.tryKeyword, .postfixQuestionMark):

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,13 @@ 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+
(.postfixQuestionMark, .leftAngle),
131+
(.postfixQuestionMark, .rightAngle):
131132
return false
132-
case (.spacedBinaryOperator(let `operator`), .leftParen):
133+
case (.leftParen, .spacedBinaryOperator(let `operator`)):
133134
return `operator` != "*"
134135
default:
135136
break
@@ -162,12 +163,14 @@ open class BasicFormat: SyntaxRewriter {
162163
switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
163164
case (.asKeyword, .exclamationMark),
164165
(.asKeyword, .postfixQuestionMark),
166+
(.exclamationMark, .period),
165167
(.initKeyword, .leftParen),
166168
(.initKeyword, .postfixQuestionMark),
167169
(.leftAngle, .identifier(_ )),
168170
(.rightAngle, .leftParen),
169171
(.rightAngle, .postfixQuestionMark),
170172
(.postfixQuestionMark, .leftParen),
173+
(.postfixQuestionMark, .rightAngle),
171174
(.postfixQuestionMark, .rightParen),
172175
(.tryKeyword, .exclamationMark),
173176
(.tryKeyword, .postfixQuestionMark):

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 144 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,151 @@ 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+
FunctionDeclSyntax(
114+
modifiers: [DeclModifier(name: .public), DeclModifier(name: .static)],
115+
identifier: Token.identifier("=="),
116+
signature: FunctionSignatureSyntax(
117+
input: ParameterClauseSyntax(
118+
parameterList: FunctionParameterListSyntax {
119+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("lhs"), colon: .colon, type: SimpleTypeIdentifier("String"))
120+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("rhs"), colon: .colon, type: SimpleTypeIdentifier("String"))
121+
}
122+
),
123+
output: ReturnClauseSyntax(
124+
returnType: SimpleTypeIdentifier(name: TokenSyntax.identifier("Bool"))
125+
)
126+
),
127+
bodyBuilder: {
128+
ReturnStmt(
129+
expression: SequenceExpr(
130+
elements: ExprListSyntax {
131+
IdentifierExprSyntax(identifier: .identifier("lhs"))
132+
BinaryOperatorExprSyntax(operatorToken: .spacedBinaryOperator("<"))
133+
IdentifierExprSyntax(identifier: .identifier("rhs"))
134+
}
135+
)
136+
)
137+
}
138+
),
139+
"""
140+
public static func ==(lhs: String, rhs: String) -> Bool {
141+
return lhs < rhs
142+
}
143+
"""
144+
),
145+
#line: (
146+
FunctionDeclSyntax(
147+
modifiers: [DeclModifierSyntax(name: .public), DeclModifierSyntax(name: .static)],
148+
identifier: Token.identifier("=="),
149+
signature: FunctionSignatureSyntax(
150+
input: ParameterClauseSyntax(
151+
parameterList: FunctionParameterListSyntax {
152+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("lhs1"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
153+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("lhs2"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
154+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("rhs1"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
155+
FunctionParameterSyntax(firstName: TokenSyntax.identifier("rhs2"), colon: .colon, type: SimpleTypeIdentifierSyntax("String"))
156+
}
157+
),
158+
output: ReturnClauseSyntax(
159+
returnType: SimpleTypeIdentifierSyntax(name: TokenSyntax.identifier("Bool"))
160+
)
161+
),
162+
bodyBuilder: {
163+
ReturnStmt(
164+
expression: SequenceExprSyntax(
165+
elements: ExprListSyntax {
166+
ExprSyntax("(lhs1, lhs2)")
167+
BinaryOperatorExprSyntax(operatorToken: .spacedBinaryOperator("<"))
168+
ExprSyntax("(rhs1, rhs2)")
169+
}
170+
)
171+
)
172+
}
173+
),
174+
"""
175+
public static func ==(lhs1: String, lhs2: String, rhs1: String, rhs2: String) -> Bool {
176+
return (lhs1, lhs2) < (rhs1, rhs2)
177+
}
178+
"""
179+
),
180+
]
48181

49-
AssertBuildResult(
50-
buildable,
51-
"""
52-
@available(*, deprecated, message: "Use function on Baz")
53-
private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
54-
}
55-
"""
56-
)
182+
for (line, testCase) in testCases {
183+
let (builder, expected) = testCase
184+
AssertBuildResult(builder, expected, line: line)
185+
}
57186
}
58187

59188
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)