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