Skip to content

Commit c9453c3

Browse files
committed
Reduce places in which we pass a custom parse function to assertParse
In many of these places, the test case tested the exact same thing if we didn’t pass a custom `parse` function to `assertParsse`. Default to `SourceFileSyntax.parse` because it produces the more natural diagnostics.
1 parent d171dc5 commit c9453c3

File tree

5 files changed

+19
-67
lines changed

5 files changed

+19
-67
lines changed

Tests/SwiftParserTest/Assertions.swift

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -529,35 +529,6 @@ public struct AssertParseOptions: OptionSet {
529529
}
530530

531531
extension ParserTestCase {
532-
/// Same as `assertParse` overload with a `(String) -> S` `parse`,
533-
/// parsing the resulting `String` as a ``SourceFileSyntax``.
534-
func assertParse(
535-
_ markedSource: String,
536-
substructure expectedSubstructure: (some SyntaxProtocol)? = Optional<Syntax>.none,
537-
substructureAfterMarker: String = "START",
538-
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
539-
applyFixIts: [String]? = nil,
540-
fixedSource expectedFixedSource: String? = nil,
541-
options: AssertParseOptions = [],
542-
experimentalFeatures: Parser.ExperimentalFeatures? = nil,
543-
file: StaticString = #file,
544-
line: UInt = #line
545-
) {
546-
assertParse(
547-
markedSource,
548-
{ SourceFileSyntax.parse(from: &$0) },
549-
substructure: expectedSubstructure,
550-
substructureAfterMarker: substructureAfterMarker,
551-
diagnostics: expectedDiagnostics,
552-
applyFixIts: applyFixIts,
553-
fixedSource: expectedFixedSource,
554-
options: options,
555-
experimentalFeatures: experimentalFeatures,
556-
file: file,
557-
line: line
558-
)
559-
}
560-
561532
/// After a test case has been mutated, assert that the mutated source
562533
/// round-trips and doesn’t hit any assertion failures in the parser.
563534
fileprivate func assertRoundTrip<S: SyntaxProtocol>(
@@ -615,7 +586,7 @@ extension ParserTestCase {
615586
/// `nil` to enable the default set of features provided by the test case.
616587
func assertParse<S: SyntaxProtocol>(
617588
_ markedSource: String,
618-
_ parse: (inout Parser) -> S,
589+
_ parse: (inout Parser) -> S = { SourceFileSyntax.parse(from: &$0) },
619590
substructure expectedSubstructure: (some SyntaxProtocol)? = Optional<Syntax>.none,
620591
substructureAfterMarker: String = "START",
621592
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ final class DeclarationTests: ParserTestCase {
118118
"""
119119
)
120120

121-
assertParse(
122-
"<@NSApplicationMain T: AnyObject>",
123-
{ GenericParameterClauseSyntax.parse(from: &$0) }
124-
)
121+
assertParse("struct A<@NSApplicationMain T: AnyObject> {}")
125122

126123
assertParse(
127124
"class T where t1️⃣",

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,6 @@ final class ExpressionTests: ParserTestCase {
10221022
func testParseArrowExpr() {
10231023
assertParse(
10241024
"Foo 1️⃣async ->2️⃣",
1025-
{ ExprSyntax.parse(from: &$0) },
10261025
substructure: TokenSyntax.keyword(.async),
10271026
substructureAfterMarker: "1️⃣",
10281027
diagnostics: [

Tests/SwiftParserTest/StatementTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ final class StatementTests: ParserTestCase {
138138
}
139139

140140
func testReturn() {
141-
assertParse("return actor", { StmtSyntax.parse(from: &$0) })
141+
assertParse("return actor")
142142

143143
assertParse(
144144
"{ 1️⃣return 0 }",
145-
{ ExprSyntax.parse(from: &$0) },
146145
substructure: ReturnStmtSyntax(
147146
returnKeyword: .keyword(.return),
148147
expression: IntegerLiteralExprSyntax(literal: .integerLiteral("0"))

Tests/SwiftParserTest/TypeTests.swift

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,22 @@ final class TypeTests: ParserTestCase {
3030
}
3131

3232
func testClosureParsing() {
33-
assertParse(
34-
"(a, b) -> c",
35-
{ TypeSyntax.parse(from: &$0) }
36-
)
33+
assertParse("let a: (a, b) -> c")
3734

38-
assertParse(
39-
"@MainActor (a, b) async throws -> c",
40-
{ TypeSyntax.parse(from: &$0) }
41-
)
35+
assertParse("let a: @MainActor (a, b) async throws -> c")
4236

4337
assertParse("() -> (\u{feff})")
4438
}
4539

4640
func testGenericTypeWithTrivia() {
47-
// N.B. Whitespace is significant here.
41+
// Whitespace is significant here.
4842
assertParse(
4943
"""
44+
let a:
5045
Foo<Bar<
5146
V, Baz<Quux>
5247
>>
53-
""",
54-
{ TypeSyntax.parse(from: &$0) }
48+
"""
5549
)
5650
}
5751

@@ -82,20 +76,17 @@ final class TypeTests: ParserTestCase {
8276
"""
8377
{ ()
8478
throws -> Void in }
85-
""",
86-
{ ExprSyntax.parse(from: &$0) }
79+
"""
8780
)
8881

8982
assertParse(
9083
"""
9184
{ [weak a, unowned(safe) self, b = 3] (a: Int, b: Int, _: Int) -> Int in }
92-
""",
93-
{ ExprSyntax.parse(from: &$0) }
85+
"""
9486
)
9587

9688
assertParse(
9789
"ℹ️{[1️⃣class]in2️⃣",
98-
{ ExprSyntax.parse(from: &$0) },
9990
diagnostics: [
10091
DiagnosticSpec(
10192
locationMarker: "1️⃣",
@@ -121,15 +112,13 @@ final class TypeTests: ParserTestCase {
121112

122113
assertParse(
123114
"{[n1️⃣`]in}",
124-
{ ExprSyntax.parse(from: &$0) },
125115
diagnostics: [
126116
DiagnosticSpec(message: "unexpected code '`' in closure capture clause")
127117
]
128118
)
129119

130120
assertParse(
131121
"{[weak1️⃣^]in}",
132-
{ ExprSyntax.parse(from: &$0) },
133122
diagnostics: [
134123
DiagnosticSpec(message: "expected identifier in closure capture", fixIts: ["insert identifier"]),
135124
DiagnosticSpec(message: "unexpected code '^' in closure capture clause"),
@@ -195,37 +184,34 @@ final class TypeTests: ParserTestCase {
195184

196185
func testLowercaseSelf() {
197186
assertParse(
198-
"1️⃣self2️⃣",
199-
{ TypeSyntax.parse(from: &$0) },
187+
"let a: 1️⃣self",
200188
diagnostics: [
201-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected 'self' keyword before type"),
202-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected type", fixIts: ["insert type"]),
189+
DiagnosticSpec(message: "expected type in type annotation", fixIts: ["insert type"]),
190+
DiagnosticSpec(message: "expected '=' in variable", fixIts: ["insert '='"]),
203191
],
204-
fixedSource: "<#type#>self"
192+
fixedSource: "let a: <#type#> = self"
205193
)
206194
}
207195

208196
func testUppercaseSelf() {
209197
assertParse(
210-
"Self",
211-
{ TypeSyntax.parse(from: &$0) },
212-
substructure: Syntax(TokenSyntax.keyword(.Self))
198+
"let a: 1️⃣Self",
199+
substructure: Syntax(TokenSyntax.keyword(.Self)),
200+
substructureAfterMarker: "1️⃣"
213201
)
214202
}
215203

216204
func testNestedLowercaseSelf() {
217205
assertParse(
218-
"Foo.1️⃣self",
219-
{ TypeSyntax.parse(from: &$0) },
206+
"let a: Foo.1️⃣self",
220207
substructure: Syntax(TokenSyntax.keyword(.`self`)),
221208
substructureAfterMarker: "1️⃣"
222209
)
223210
}
224211

225212
func testNestedUppercaseSelf() {
226213
assertParse(
227-
"Foo.1️⃣Self",
228-
{ TypeSyntax.parse(from: &$0) },
214+
"let a: Foo.1️⃣Self",
229215
substructure: Syntax(TokenSyntax.identifier("Self")),
230216
substructureAfterMarker: "1️⃣"
231217
)

0 commit comments

Comments
 (0)