Skip to content

Add notes to assertion #1548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions Tests/SwiftParserTest/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ struct DiagnosticSpec {
/// If not `nil`, assert that the highlighted range has this content.
let highlight: String?
/// If not `nil`, assert that the diagnostic contains notes with these messages.
let notes: [NoteSpec]?
let notes: [NoteSpec]
/// If not `nil`, assert that the diagnostic contains fix-its with these messages.
/// Use the `fixedSource` parameter on `AssertParse` to check that applying the Fix-It yields the expected result.
let fixIts: [String]
Expand All @@ -254,7 +254,7 @@ struct DiagnosticSpec {
message: String?,
severity: DiagnosticSeverity = .error,
highlight: String? = nil,
notes: [NoteSpec]? = nil,
notes: [NoteSpec] = [],
fixIts: [String] = [],
file: StaticString = #file,
line: UInt = #line
Expand Down Expand Up @@ -406,20 +406,18 @@ func assertDiagnostic<T: SyntaxProtocol>(
line: spec.line
)
}
if let notes = spec.notes {
if diag.notes.count != notes.count {
XCTFail(
"""
Expected \(notes.count) notes but received \(diag.notes.count):
\(diag.notes.map(\.debugDescription).joined(separator: "\n"))
""",
file: spec.file,
line: spec.line
)
} else {
for (note, expectedNote) in zip(diag.notes, notes) {
assertNote(note, in: tree, markerLocations: markerLocations, expected: expectedNote)
}
if diag.notes.count != spec.notes.count {
XCTFail(
"""
Expected \(spec.notes.count) notes but received \(diag.notes.count):
\(diag.notes.map(\.debugDescription).joined(separator: "\n"))
""",
file: spec.file,
line: spec.line
)
} else {
for (note, expectedNote) in zip(diag.notes, spec.notes) {
assertNote(note, in: tree, markerLocations: markerLocations, expected: expectedNote)
}
}

Expand Down
46 changes: 36 additions & 10 deletions Tests/SwiftParserTest/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ final class AttributeTests: XCTestCase {
func testMissingArgumentToAttribute() {
assertParse(
"""
@_dynamicReplacement(1️⃣
@_dynamicReplacementℹ️(1️⃣
func 2️⃣test_dynamic_replacement_for2() {
}
""",
diagnostics: [
DiagnosticSpec(message: "expected argument for '@_dynamicReplacement' attribute", fixIts: ["insert attribute argument"]),
DiagnosticSpec(message: "expected ')' to end attribute", fixIts: ["insert ')'"]),
DiagnosticSpec(
message: "expected argument for '@_dynamicReplacement' attribute",
fixIts: ["insert attribute argument"]
),
DiagnosticSpec(
message: "expected ')' to end attribute",
notes: [NoteSpec(message: "to match this opening '('")],
fixIts: ["insert ')'"]
),
],
fixedSource: """
@_dynamicReplacement(for: <#identifier#>)
Expand All @@ -37,14 +44,23 @@ final class AttributeTests: XCTestCase {
func testMissingGenericTypeToAttribute() {
assertParse(
"""
@differentiable(reverse wrt1️⃣,where T2️⃣
@differentiableℹ️(reverse wrt1️⃣,where T2️⃣
func podcastPlaybackSpeed() {
}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' and parameters in '@differentiable' argument", fixIts: ["insert ':' and parameters"]),
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected ':' and parameters in '@differentiable' argument",
fixIts: ["insert ':' and parameters"]
),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ':' or '==' to indicate a conformance or same-type requirement"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end attribute", fixIts: ["insert ')'"]),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected ')' to end attribute",
notes: [NoteSpec(message: "to match this opening '('")],
fixIts: ["insert ')'"]
),
],
fixedSource: """
@differentiable(reverse wrt: <#identifier#>,where T)
Expand All @@ -57,12 +73,22 @@ final class AttributeTests: XCTestCase {
func testMissingClosingParenToAttribute() {
assertParse(
"""
@_specialize(e1️⃣
@_specializeℹ️(e1️⃣
""",
diagnostics: [
DiagnosticSpec(message: "expected ':' in attribute argument", fixIts: ["insert ':'"]),
DiagnosticSpec(message: "expected ')' to end attribute", fixIts: ["insert ')'"]),
DiagnosticSpec(message: "expected declaration after attribute", fixIts: ["insert declaration"]),
DiagnosticSpec(
message: "expected ':' in attribute argument",
fixIts: ["insert ':'"]
),
DiagnosticSpec(
message: "expected ')' to end attribute",
notes: [NoteSpec(message: "to match this opening '('")],
fixIts: ["insert ')'"]
),
DiagnosticSpec(
message: "expected declaration after attribute",
fixIts: ["insert declaration"]
),
],
fixedSource: """
@_specialize(e:) <#declaration#>
Expand Down
166 changes: 132 additions & 34 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,20 @@ final class DeclarationTests: XCTestCase {
"""
)
assertParse(
"class B<where g1️⃣",
"class Bℹ️<where g1️⃣",
diagnostics: [
DiagnosticSpec(message: "expected ':' or '==' to indicate a conformance or same-type requirement"),
DiagnosticSpec(message: "expected '>' to end generic parameter clause", fixIts: ["insert '>'"]),
DiagnosticSpec(message: "expected member block in class", fixIts: ["insert member block"]),
DiagnosticSpec(
message: "expected ':' or '==' to indicate a conformance or same-type requirement"
),
DiagnosticSpec(
message: "expected '>' to end generic parameter clause",
notes: [NoteSpec(message: "to match this opening '<'")],
fixIts: ["insert '>'"]
),
DiagnosticSpec(
message: "expected member block in class",
fixIts: ["insert member block"]
),
],
fixedSource: """
class B<where g> {
Expand Down Expand Up @@ -193,11 +202,23 @@ final class DeclarationTests: XCTestCase {
)

assertParse(
"protocol P{1️⃣{}case2️⃣",
"protocol Pℹ️{1️⃣{}case2️⃣",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '{}' before enum case"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected identifier in enum case", fixIts: ["insert identifier"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end protocol", fixIts: ["insert '}'"]),
DiagnosticSpec(
locationMarker: "1️⃣",
message: "unexpected code '{}' before enum case"
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected identifier in enum case",
fixIts: ["insert identifier"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected '}' to end protocol",
notes: [NoteSpec(message: "to match this opening '{'")],
fixIts: ["insert '}'"]
),
],
fixedSource: """
protocol P{{}case <#identifier#>
Expand Down Expand Up @@ -672,9 +693,13 @@ final class DeclarationTests: XCTestCase {

func testMissingClosingParenInFunctionSignature() {
assertParse(
"func test(first second: Int1️⃣",
"func testℹ️(first second: Int1️⃣",
diagnostics: [
DiagnosticSpec(message: "expected ')' to end parameter clause", fixIts: ["insert ')'"])
DiagnosticSpec(
message: "expected ')' to end parameter clause",
notes: [NoteSpec(message: "to match this opening '('")],
fixIts: ["insert ')'"]
)
],
fixedSource: """
func test(first second: Int)
Expand Down Expand Up @@ -814,15 +839,31 @@ final class DeclarationTests: XCTestCase {
func testExpressionMember() {
assertParse(
"""
struct S {1️⃣
/2️⃣ ###line 25 "line-directive.swift"3️⃣
4️⃣}
struct S 1️⃣{2️⃣
3️⃣/4️⃣ ###line 25 "line-directive.swift"5️⃣
6️⃣}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '}' to end struct", fixIts: ["insert '}'"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "bare slash regex literal may not start with space"),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected '/' to end regex literal", fixIts: ["insert '/\'"]),
DiagnosticSpec(locationMarker: "4️⃣", message: "extraneous brace at top level"),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected '}' to end struct",
notes: [NoteSpec(locationMarker: "1️⃣", message: "to match this opening '{'")],
fixIts: ["insert '}'"]
),
DiagnosticSpec(
locationMarker: "4️⃣",
message: "bare slash regex literal may not start with space"
),
DiagnosticSpec(
locationMarker: "5️⃣",
message: "expected '/' to end regex literal",
notes: [NoteSpec(locationMarker: "3️⃣", message: "to match this opening '/'")],
fixIts: ["insert '/\'"]
),
DiagnosticSpec(
locationMarker: "6️⃣",
message: "extraneous brace at top level"
),
],
fixedSource: """
struct S {
Expand Down Expand Up @@ -915,7 +956,7 @@ final class DeclarationTests: XCTestCase {

func testDontRecoverFromDeclKeyword() {
assertParse(
"func foo(first second 1️⃣third 2️⃣struct3️⃣: Int4️⃣) {}",
"func fooℹ️(first second 1️⃣third 2️⃣struct3️⃣: Int4️⃣) {}",
substructure: Syntax(
FunctionParameterSyntax(
firstName: .identifier("first"),
Expand All @@ -925,10 +966,26 @@ final class DeclarationTests: XCTestCase {
)
),
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' in parameter", fixIts: ["insert ':'"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end parameter clause", fixIts: ["insert ')'"]),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected identifier in struct", fixIts: ["insert identifier"]),
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code ')' in struct"),
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected ':' in parameter",
fixIts: ["insert ':'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected ')' to end parameter clause",
notes: [NoteSpec(message: "to match this opening '('")],
fixIts: ["insert ')'"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
message: "expected identifier in struct",
fixIts: ["insert identifier"]
),
DiagnosticSpec(
locationMarker: "4️⃣",
message: "unexpected code ')' in struct"
),
],
fixedSource: """
func foo(first second: third)struct <#identifier#>: Int) {}
Expand Down Expand Up @@ -975,9 +1032,21 @@ final class DeclarationTests: XCTestCase {
)
),
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' in parameter", fixIts: ["insert ':'"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ']' to end array type", fixIts: ["insert ']'"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code 'fourth: Int' in parameter clause"),
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected ':' in parameter",
fixIts: ["insert ':'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected ']' to end array type",
notes: [NoteSpec(locationMarker: "1️⃣", message: "to match this opening '['")],
fixIts: ["insert ']'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "unexpected code 'fourth: Int' in parameter clause"
),
],
fixedSource: """
func foo(first second: [third]fourth: Int) {}
Expand All @@ -988,7 +1057,7 @@ final class DeclarationTests: XCTestCase {
func testDontRecoverIfNewlineIsBeforeColon() {
assertParse(
"""
func foo(first second 1️⃣third2️⃣
func fooℹ️(first second 1️⃣third2️⃣
3️⃣: Int) {}
""",
substructure: Syntax(
Expand All @@ -1000,9 +1069,21 @@ final class DeclarationTests: XCTestCase {
)
),
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected ':' in parameter", fixIts: ["insert ':'"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected ')' to end parameter clause", fixIts: ["insert ')'"]),
DiagnosticSpec(locationMarker: "3️⃣", message: "extraneous code ': Int) {}' at top level"),
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected ':' in parameter",
fixIts: ["insert ':'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected ')' to end parameter clause",
notes: [NoteSpec(message: "to match this opening '('")],
fixIts: ["insert ')'"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
message: "extraneous code ': Int) {}' at top level"
),
],
fixedSource: """
func foo(first second: third)
Expand Down Expand Up @@ -1261,13 +1342,30 @@ final class DeclarationTests: XCTestCase {
func testStandaloneAtSignInGenericParameter() {
assertParse(
"""
struct U<@1️⃣
struct U1️⃣<@2️⃣
""",
diagnostics: [
DiagnosticSpec(message: "expected name in attribute", fixIts: ["insert name"]),
DiagnosticSpec(message: "expected name in generic parameter", fixIts: ["insert name"]),
DiagnosticSpec(message: "expected '>' to end generic parameter clause", fixIts: ["insert '>'"]),
DiagnosticSpec(message: "expected member block in struct", fixIts: ["insert member block"]),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected name in attribute",
fixIts: ["insert name"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected name in generic parameter",
fixIts: ["insert name"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected '>' to end generic parameter clause",
notes: [NoteSpec(locationMarker: "1️⃣", message: "to match this opening '<'")],
fixIts: ["insert '>'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected member block in struct",
fixIts: ["insert member block"]
),
],
fixedSource: """
struct U<@<#type#> <#identifier#>> {
Expand Down
Loading