1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftDiagnostics
import SwiftParser
import SwiftParserDiagnostics
import XCTest
import _SwiftSyntaxTestSupport
final class ParserDiagnosticsFormatterIntegrationTests: XCTestCase {
func annotate(source: String, colorize: Bool = false) -> String {
let tree = Parser.parse(source: source)
let diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
return DiagnosticsFormatter.annotatedSource(tree: tree, diags: diags, colorize: colorize)
}
func testSingleDiagnostic() {
let source = """
var foo = bar +
"""
let expectedOutput = """
1 | var foo = bar +
| `- error: expected expression after operator
"""
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
func testMultipleDiagnosticsInOneLine() {
let source = """
foo.[].[].[]
"""
let expectedOutput = """
1 | foo.[].[].[]
| | | `- error: expected name in member access
| | `- error: expected name in member access
| `- error: expected name in member access
"""
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
func testLineSkipping() {
let source = """
var i = 1
i = 2
i = foo(
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
i = bar(
"""
let expectedOutput = """
2 | i = 2
3 | i = foo(
4 | i = 4
| `- error: expected ')' to end function call
5 | i = 5
6 | i = 6
:
9 | i = 9
10 | i = 10
11 | i = bar(
| `- error: expected value and ')' to end function call
"""
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
func testTwoDiagnosticsAtSameLocation() throws {
let source = "t as (..)"
let expectedOutput = """
1 | t as (..)
| |- error: expected type in tuple type
| `- error: unexpected code '..' in tuple type
"""
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
func testAddsColoringToSingleErrorDiagnostic() {
let source = """
var foo = bar +
"""
let expectedOutput = """
\u{001B}[0;36m1 |\u{001B}[0;0m var foo = bar +
\u{001B}[0;36m|\u{001B}[0;0m `- \u{001B}[1;31merror: \u{001B}[1;39mexpected expression after operator\u{001B}[0;0m
"""
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
}
func testAddsColoringToMultipleDiagnosticsInOneLine() {
let source = """
foo.[].[].[]
"""
let expectedOutput = """
\u{001B}[0;36m1 |\u{001B}[0;0m foo.[].[].[]
\u{001B}[0;36m|\u{001B}[0;0m | | `- \u{001B}[1;31merror: \u{001B}[1;39mexpected name in member access\u{001B}[0;0m
\u{001B}[0;36m|\u{001B}[0;0m | `- \u{001B}[1;31merror: \u{001B}[1;39mexpected name in member access\u{001B}[0;0m
\u{001B}[0;36m|\u{001B}[0;0m `- \u{001B}[1;31merror: \u{001B}[1;39mexpected name in member access\u{001B}[0;0m
"""
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
}
func testColoringWithHighlights() {
let source = """
for (i = ๐ฎ; i != ๐ฉโ๐ฉโ๐ฆโ๐ฆ; i += 1) { }
"""
let expectedOutput = """
\u{001B}[0;36m1 |\u{001B}[0;0m for \u{001B}[4;39m(i\u{001B}[0;0m \u{001B}[4;39m= ๐ฎ; i != ๐ฉโ๐ฉโ๐ฆโ๐ฆ; i += 1)\u{001B}[0;0m { }
\u{001B}[0;36m|\u{001B}[0;0m | `- \u{001B}[1;31merror: \u{001B}[1;39mexpected ')' to end tuple pattern\u{001B}[0;0m
\u{001B}[0;36m|\u{001B}[0;0m `- \u{001B}[1;31merror: \u{001B}[1;39mC-style for statement has been removed in Swift 3\u{001B}[0;0m
"""
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
}
func testRighParenLocation() {
let source = """
let _ : Float -> Int
"""
let expectedOutput = """
1 | let _ : Float -> Int
| | `- error: expected ')' in function type
| `- error: expected '(' to start function type
"""
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
func testDontCrashIfFullLineHighlightContainsEmoji() {
let source = """
func o() {
}๐จโ๐ฉโ๐งโ๐ฆ}
}
"""
let expectedOutput = """
1 | func o() {
2 | }๐จโ๐ฉโ๐งโ๐ฆ}
| |`- error: extraneous braces at top level
| `- error: consecutive statements on a line must be separated by newline or ';'
3 | }
"""
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
func testEmojiInSourceCode() {
let source = """
let ๐จโ๐ฉโ๐งโ๐ฆ = ;
"""
let expectedOutput = """
1 | let ๐จโ๐ฉโ๐งโ๐ฆ = ;
| `- error: expected expression in variable
"""
assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
}
|