Skip to content

Commit 60da6f4

Browse files
committed
Add notes to assertion
1 parent 4357755 commit 60da6f4

13 files changed

+341
-129
lines changed

Tests/SwiftParserTest/Assertions.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ struct DiagnosticSpec {
239239
/// If not `nil`, assert that the highlighted range has this content.
240240
let highlight: String?
241241
/// If not `nil`, assert that the diagnostic contains notes with these messages.
242-
let notes: [NoteSpec]?
242+
let notes: [NoteSpec]
243243
/// If not `nil`, assert that the diagnostic contains fix-its with these messages.
244244
/// Use the `fixedSource` parameter on `AssertParse` to check that applying the Fix-It yields the expected result.
245245
let fixIts: [String]
@@ -254,7 +254,7 @@ struct DiagnosticSpec {
254254
message: String?,
255255
severity: DiagnosticSeverity = .error,
256256
highlight: String? = nil,
257-
notes: [NoteSpec]? = nil,
257+
notes: [NoteSpec] = [],
258258
fixIts: [String] = [],
259259
file: StaticString = #file,
260260
line: UInt = #line
@@ -418,20 +418,18 @@ func assertDiagnostic<T: SyntaxProtocol>(
418418
line: line
419419
)
420420
}
421-
if let notes = spec.notes {
422-
if diag.notes.count != notes.count {
423-
XCTFail(
424-
"""
425-
Expected \(notes.count) notes but received \(diag.notes.count):
426-
\(diag.notes.map(\.debugDescription).joined(separator: "\n"))
427-
""",
428-
file: file,
429-
line: line
430-
)
431-
} else {
432-
for (note, expectedNote) in zip(diag.notes, notes) {
433-
assertNote(note, in: tree, markerLocations: markerLocations, expected: expectedNote, file: expectedNote.file, line: expectedNote.line)
434-
}
421+
if diag.notes.count != spec.notes.count {
422+
XCTFail(
423+
"""
424+
Expected \(spec.notes.count) notes but received \(diag.notes.count):
425+
\(diag.notes.map(\.debugDescription).joined(separator: "\n"))
426+
""",
427+
file: file,
428+
line: line
429+
)
430+
} else {
431+
for (note, expectedNote) in zip(diag.notes, spec.notes) {
432+
assertNote(note, in: tree, markerLocations: markerLocations, expected: expectedNote, file: expectedNote.file, line: expectedNote.line)
435433
}
436434
}
437435

Tests/SwiftParserTest/AttributeTests.swift

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ final class AttributeTests: XCTestCase {
1818
func testMissingArgumentToAttribute() {
1919
assertParse(
2020
"""
21-
@_dynamicReplacement(1️⃣
21+
@_dynamicReplacementℹ️(1️⃣
2222
func 2️⃣test_dynamic_replacement_for2() {
2323
}
2424
""",
2525
diagnostics: [
26-
DiagnosticSpec(message: "expected argument for '@_dynamicReplacement' attribute", fixIts: ["insert attribute argument"]),
27-
DiagnosticSpec(message: "expected ')' to end attribute", fixIts: ["insert ')'"]),
26+
DiagnosticSpec(
27+
message: "expected argument for '@_dynamicReplacement' attribute",
28+
fixIts: ["insert attribute argument"]
29+
),
30+
DiagnosticSpec(
31+
message: "expected ')' to end attribute",
32+
notes: [NoteSpec(message: "to match this opening '('")],
33+
fixIts: ["insert ')'"]
34+
),
2835
],
2936
fixedSource: """
3037
@_dynamicReplacement(for : <#identifier#>)
@@ -37,14 +44,23 @@ final class AttributeTests: XCTestCase {
3744
func testMissingGenericTypeToAttribute() {
3845
assertParse(
3946
"""
40-
@differentiable(reverse wrt1️⃣,where T2️⃣
47+
@differentiableℹ️(reverse wrt1️⃣,where T2️⃣
4148
func podcastPlaybackSpeed() {
4249
}
4350
""",
4451
diagnostics: [
45-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' and parameters in '@differentiable' argument", fixIts: ["insert ':' and parameters"]),
52+
DiagnosticSpec(
53+
locationMarker: "1️⃣",
54+
message: "expected ':' and parameters in '@differentiable' argument",
55+
fixIts: ["insert ':' and parameters"]
56+
),
4657
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' or '==' to indicate a conformance or same-type requirement"),
47-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end attribute", fixIts: ["insert ')'"]),
58+
DiagnosticSpec(
59+
locationMarker: "2️⃣",
60+
message: "expected ')' to end attribute",
61+
notes: [NoteSpec(message: "to match this opening '('")],
62+
fixIts: ["insert ')'"]
63+
),
4864
],
4965
fixedSource: """
5066
@differentiable(reverse wrt: <#identifier#>,where T)
@@ -57,12 +73,23 @@ final class AttributeTests: XCTestCase {
5773
func testMissingClosingParenToAttribute() {
5874
assertParse(
5975
"""
60-
@_specialize(e1️⃣
76+
@_specializeℹ️(e1️⃣
6177
""",
6278
diagnostics: [
63-
DiagnosticSpec(message: "expected ':' in attribute argument", fixIts: ["insert ':'"]),
64-
DiagnosticSpec(message: "expected ')' to end attribute", fixIts: ["insert ')'"]),
65-
DiagnosticSpec(message: "expected declaration after attribute", fixIts: ["insert declaration"]),
79+
DiagnosticSpec(
80+
message: "expected ':' in attribute argument",
81+
fixIts: ["insert ':'"]
82+
),
83+
DiagnosticSpec(
84+
message: "expected ')' to end attribute",
85+
notes: [NoteSpec(message: "to match this opening '('")],
86+
fixIts: ["insert ')'"]
87+
),
88+
DiagnosticSpec(
89+
message: "expected declaration after attribute",
90+
fixIts: ["insert declaration"]
91+
),
92+
6693
]
6794
)
6895
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,14 @@ final class DeclarationTests: XCTestCase {
128128
]
129129
)
130130
assertParse(
131-
"class B<where g1️⃣",
131+
"class Bℹ️<where g1️⃣",
132132
diagnostics: [
133133
DiagnosticSpec(message: "expected ':' or '==' to indicate a conformance or same-type requirement"),
134-
DiagnosticSpec(message: "expected '>' to end generic parameter clause", fixIts: ["insert '>'"]),
135-
DiagnosticSpec(message: "expected member block in class", fixIts: ["insert member block"]),
134+
DiagnosticSpec(message: "expected '>' to end generic parameter clause",
135+
notes: [NoteSpec(message: "to match this opening '<'")],
136+
fixIts: ["insert '>'"]),
137+
DiagnosticSpec(message: "expected member block in class",
138+
fixIts: ["insert member block"]),
136139
]
137140
)
138141
}
@@ -185,11 +188,14 @@ final class DeclarationTests: XCTestCase {
185188
)
186189

187190
assertParse(
188-
"protocol P{1️⃣{}case2️⃣",
191+
"protocol Pℹ️{1️⃣{}case2️⃣",
189192
diagnostics: [
190193
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '{}' before enum case"),
191-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in enum case", fixIts: ["insert identifier"]),
192-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end protocol", fixIts: ["insert '}'"]),
194+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in enum case",
195+
fixIts: ["insert identifier"]),
196+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end protocol",
197+
notes: [NoteSpec(message: "to match this opening '{'")],
198+
fixIts: ["insert '}'"]),
193199
]
194200
)
195201
}
@@ -626,9 +632,11 @@ final class DeclarationTests: XCTestCase {
626632

627633
func testMissingClosingParenInFunctionSignature() {
628634
assertParse(
629-
"func test(first second: Int1️⃣",
635+
"func testℹ️(first second: Int1️⃣",
630636
diagnostics: [
631-
DiagnosticSpec(message: "expected ')' to end parameter clause", fixIts: ["insert ')'"])
637+
DiagnosticSpec(message: "expected ')' to end parameter clause",
638+
notes: [NoteSpec(message: " to match this opening '('")],
639+
fixIts: ["insert ')'"])
632640
]
633641
)
634642
}
@@ -745,14 +753,18 @@ final class DeclarationTests: XCTestCase {
745753
func testExpressionMember() {
746754
assertParse(
747755
"""
748-
struct S {1️⃣
749-
/2️⃣ ###line 25 "line-directive.swift"3️⃣
756+
struct S ℹ️{1️⃣
757+
🔟/2️⃣ ###line 25 "line-directive.swift"3️⃣
750758
4️⃣}
751759
""",
752760
diagnostics: [
753-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '}' to end struct", fixIts: ["insert '}'"]),
761+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '}' to end struct",
762+
notes: [NoteSpec(message: "to match this opening '{'")],
763+
fixIts: ["insert '}'"]),
754764
DiagnosticSpec(locationMarker: "2️⃣", message: "bare slash regex literal may not start with space"),
755-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected '/' to end regex literal", fixIts: ["insert '/\'"]),
765+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected '/' to end regex literal",
766+
notes: [NoteSpec(locationMarker: "🔟", message: "to match this opening '/'")],
767+
fixIts: ["insert '/\'"]),
756768
DiagnosticSpec(locationMarker: "4️⃣", message: "extraneous brace at top level"),
757769
]
758770
)
@@ -837,7 +849,7 @@ final class DeclarationTests: XCTestCase {
837849

838850
func testDontRecoverFromDeclKeyword() {
839851
assertParse(
840-
"func foo(first second 1️⃣third 2️⃣struct3️⃣: Int4️⃣) {}",
852+
"func fooℹ️(first second 1️⃣third 2️⃣struct3️⃣: Int4️⃣) {}",
841853
substructure: Syntax(
842854
FunctionParameterSyntax(
843855
firstName: .identifier("first"),
@@ -847,9 +859,13 @@ final class DeclarationTests: XCTestCase {
847859
)
848860
),
849861
diagnostics: [
850-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' in parameter", fixIts: ["insert ':'"]),
851-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end parameter clause", fixIts: ["insert ')'"]),
852-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected identifier in struct", fixIts: ["insert identifier"]),
862+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' in parameter",
863+
fixIts: ["insert ':'"]),
864+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end parameter clause",
865+
notes: [NoteSpec(message: "to match this opening '('")],
866+
fixIts: ["insert ')'"]),
867+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected identifier in struct",
868+
fixIts: ["insert identifier"]),
853869
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code ')' in struct"),
854870
]
855871
)
@@ -1140,13 +1156,18 @@ final class DeclarationTests: XCTestCase {
11401156
func testStandaloneAtSignInGenericParameter() {
11411157
assertParse(
11421158
"""
1143-
struct U<@1️⃣
1159+
struct Uℹ️<@1️⃣
11441160
""",
11451161
diagnostics: [
1146-
DiagnosticSpec(message: "expected name in attribute", fixIts: ["insert name"]),
1147-
DiagnosticSpec(message: "expected name in generic parameter", fixIts: ["insert name"]),
1148-
DiagnosticSpec(message: "expected '>' to end generic parameter clause", fixIts: ["insert '>'"]),
1149-
DiagnosticSpec(message: "expected member block in struct", fixIts: ["insert member block"]),
1162+
DiagnosticSpec(message: "expected name in attribute",
1163+
fixIts: ["insert name"]),
1164+
DiagnosticSpec(message: "expected name in generic parameter",
1165+
fixIts: ["insert name"]),
1166+
DiagnosticSpec(message: "expected '>' to end generic parameter clause",
1167+
notes: [NoteSpec(message: "to match this opening '<'")],
1168+
fixIts: ["insert '>'"]),
1169+
DiagnosticSpec(message: "expected member block in struct",
1170+
fixIts: ["insert member block"]),
11501171
]
11511172
)
11521173
}
@@ -1374,20 +1395,27 @@ final class DeclarationTests: XCTestCase {
13741395
macro m1 1️⃣= A
13751396
""",
13761397
diagnostics: [
1377-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected parameter clause in function signature", fixIts: ["insert parameter clause"])
1398+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected parameter clause in function signature",
1399+
fixIts: ["insert parameter clause"])
13781400
]
13791401
)
13801402
}
13811403

13821404
func testPrimaryAssociatedTypeNotTerminatedWithAngleBracket() {
13831405
assertParse(
1384-
"protocol1️⃣<2️⃣:3️⃣",
1406+
"protocol1️⃣ℹ️<2️⃣:3️⃣",
13851407
diagnostics: [
1386-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in protocol", fixIts: ["insert identifier"]),
1387-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in primary associated type clause", fixIts: ["insert name"]),
1388-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '>' to end primary associated type clause", fixIts: ["insert '>'"]),
1389-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected type in inherited type", fixIts: ["insert type"]),
1390-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected member block in protocol", fixIts: ["insert member block"]),
1408+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in protocol",
1409+
fixIts: ["insert identifier"]),
1410+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in primary associated type clause",
1411+
fixIts: ["insert name"]),
1412+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '>' to end primary associated type clause",
1413+
notes: [NoteSpec(message: "to match this opening '<'")],
1414+
fixIts: ["insert '>'"]),
1415+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected type in inherited type",
1416+
fixIts: ["insert type"]),
1417+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected member block in protocol",
1418+
fixIts: ["insert member block"]),
13911419
]
13921420
)
13931421
}

Tests/SwiftParserTest/translated/ActorTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ final class ActorTests: XCTestCase {
2121
actor MyActor11️⃣
2222
""",
2323
diagnostics: [
24-
DiagnosticSpec(message: "expected member block in actor", fixIts: ["insert member block"])
24+
DiagnosticSpec(
25+
message: "expected member block in actor",
26+
fixIts: ["insert member block"]
27+
)
2528
]
2629
)
2730
}

0 commit comments

Comments
 (0)