Skip to content

Commit 3b11861

Browse files
committed
Add some test cases
1 parent d49d218 commit 3b11861

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ let package = Package(
151151
name: "SwiftBasicFormatTest",
152152
dependencies: ["SwiftBasicFormat"]
153153
),
154+
.testTarget(
155+
name: "SwiftDiagnosticsTest",
156+
dependencies: ["SwiftDiagnostics", "SwiftParser"]
157+
),
154158
.testTarget(
155159
name: "SwiftSyntaxTest",
156160
dependencies: ["SwiftSyntax", "_SwiftSyntaxTestSupport"]

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public struct DiagnosticsFormatter {
7777
// print the source line
7878
annotatedSource.append("\(linePrefix)\(annotatedLine.sourceString)")
7979

80+
// If the line did not end with \n (e.g. the last line), append it manually
81+
if annotatedSource.last != "\n" {
82+
annotatedSource.append("\n")
83+
}
84+
8085
let columnsWithDiagnostics = Set(annotatedLine.diagnostics.map { $0.location(converter: slc).column ?? 0 })
8186
let diagsPerColumn = Dictionary(grouping: annotatedLine.diagnostics) { diag in
8287
diag.location(converter: slc).column ?? 0
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===------------------ DiagnosticsFormatterTests.swift -------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
import XCTest
13+
@_spi(Testing) import SwiftDiagnostics
14+
import SwiftParser
15+
16+
final class DiagnosticsFormatterTests: XCTestCase {
17+
18+
func annotate(source: String) throws -> String {
19+
let tree = try Parser.parse(source: source)
20+
let diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
21+
return DiagnosticsFormatter.annotatedSource(tree: tree, diags: diags)
22+
}
23+
24+
func testSingleDiagnostic() throws {
25+
let source = """
26+
var foo = bar +
27+
"""
28+
let expectedOutput = """
29+
1 │ var foo = bar +
30+
∣ ╰─ expected expression in variable
31+
32+
"""
33+
XCTAssertEqual(expectedOutput, try annotate(source: source))
34+
}
35+
36+
func testMultipleDiagnosticsInOneLine() throws {
37+
let source = """
38+
foo.[].[].[]
39+
"""
40+
let expectedOutput = """
41+
1 │ foo.[].[].[]
42+
∣ │ │ ╰─ expected identifier in member access
43+
∣ │ ╰─ expected identifier in member access
44+
∣ ╰─ expected identifier in member access
45+
46+
"""
47+
XCTAssertEqual(expectedOutput, try annotate(source: source))
48+
}
49+
50+
func testLineSkipping() throws {
51+
let source = """
52+
var i = 1
53+
i = 2
54+
i = foo(
55+
i = 4
56+
i = 5
57+
i = 6
58+
i = 7
59+
i = 8
60+
i = 9
61+
i = 10
62+
i = bar(
63+
"""
64+
let expectedOutput = """
65+
2 │ i = 2
66+
3 │ i = foo(
67+
4 │ i = 4
68+
∣ ╰─ expected ')' to end function call
69+
5 │ i = 5
70+
6 │ i = 6
71+
72+
9 │ i = 9
73+
10 │ i = 10
74+
11 │ i = bar(
75+
∣ ├─ expected value in function call
76+
∣ ╰─ expected ')' to end function call
77+
78+
"""
79+
XCTAssertEqual(expectedOutput, try annotate(source: source))
80+
}
81+
}

0 commit comments

Comments
 (0)