|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 | + |
| 13 | +import SwiftDiagnostics |
| 14 | +import SwiftParser |
| 15 | +import SwiftParserDiagnostics |
| 16 | +import SwiftSyntax |
| 17 | +import XCTest |
| 18 | +import _SwiftSyntaxTestSupport |
| 19 | + |
| 20 | +/// A typealias representing a location marker. |
| 21 | +/// |
| 22 | +/// This string serves to pinpoint the exact location of a particular token in the SwiftSyntax tree. |
| 23 | +/// Once the token location is identified, it can be leveraged for various test-specific operations such as inserting diagnostics, notes, or fix-its, |
| 24 | +/// or for closer examination of the syntax tree. |
| 25 | +/// |
| 26 | +/// Markers are instrumental in writing unit tests that require precise location data. They are commonly represented using emojis like 1️⃣, 2️⃣, 3️⃣, etc., to improve readability. |
| 27 | +/// |
| 28 | +/// ### Example |
| 29 | +/// |
| 30 | +/// In the following test code snippet, the emojis 1️⃣ and 2️⃣ are used as location markers: |
| 31 | +/// |
| 32 | +/// ```swift |
| 33 | +/// func foo() -> Int { |
| 34 | +/// if 1️⃣1 != 0 2️⃣{ |
| 35 | +/// return 0 |
| 36 | +/// } |
| 37 | +/// return 1 |
| 38 | +/// } |
| 39 | +/// ``` |
| 40 | +typealias LocationMarker = String |
| 41 | + |
| 42 | +/// Represents a descriptor for constructing a diagnostic in testing. |
| 43 | +struct DiagnosticDescriptor { |
| 44 | + /// Represents errors that can occur while creating a `Diagnostic` instance. |
| 45 | + private struct DiagnosticCreationError: Error, LocalizedError { |
| 46 | + /// A human-readable message describing what went wrong. |
| 47 | + let message: String |
| 48 | + |
| 49 | + /// A localized message describing what went wrong. Required by `LocalizedError`. |
| 50 | + var errorDescription: String? { message } |
| 51 | + } |
| 52 | + |
| 53 | + /// The marker pointing to location in source code. |
| 54 | + let locationMarker: LocationMarker |
| 55 | + |
| 56 | + /// The ID associated with the message, used for categorizing or referencing it. |
| 57 | + let id: MessageID |
| 58 | + |
| 59 | + /// The textual content of the message to be displayed. |
| 60 | + let message: String |
| 61 | + |
| 62 | + /// The severity level of the diagnostic message. |
| 63 | + let severity: DiagnosticSeverity |
| 64 | + |
| 65 | + /// The syntax elements to be highlighted for this diagnostic message. |
| 66 | + let highlight: [Syntax] // TODO: How to create an abstract model for this? |
| 67 | + |
| 68 | + /// Descriptors for any accompanying notes for this diagnostic message. |
| 69 | + let noteDescriptors: [NoteDescriptor] |
| 70 | + |
| 71 | + /// Descriptors for any Fix-Its that can be applied for this diagnostic message. |
| 72 | + let fixIts: [FixIt] // TODO: How to create an abstract model for this? |
| 73 | + |
| 74 | + /// Initializes a new `DiagnosticDescriptor`. |
| 75 | + /// |
| 76 | + /// - Parameters: |
| 77 | + /// - locationMarker: The marker pointing to location in source code. |
| 78 | + /// - id: The message ID of the diagnostic. |
| 79 | + /// - message: The textual message to display for the diagnostic. |
| 80 | + /// - severity: The severity level of the diagnostic. Default is `.error`. |
| 81 | + /// - highlight: The syntax elements to be highlighted. Default is an empty array. |
| 82 | + /// - noteDescriptors: An array of note descriptors for additional context. Default is an empty array. |
| 83 | + /// - fixIts: An array of Fix-It descriptors for quick fixes. Default is an empty array. |
| 84 | + init( |
| 85 | + locationMarker: LocationMarker, |
| 86 | + id: MessageID = MessageID(domain: "test", id: "conjured"), |
| 87 | + message: String, |
| 88 | + severity: DiagnosticSeverity = .error, |
| 89 | + highlight: [Syntax] = [], |
| 90 | + noteDescriptors: [NoteDescriptor] = [], |
| 91 | + fixIts: [FixIt] = [] |
| 92 | + ) { |
| 93 | + self.locationMarker = locationMarker |
| 94 | + self.id = id |
| 95 | + self.message = message |
| 96 | + self.severity = severity |
| 97 | + self.highlight = highlight |
| 98 | + self.noteDescriptors = noteDescriptors |
| 99 | + self.fixIts = fixIts |
| 100 | + } |
| 101 | + |
| 102 | + /// Creates a ``Diagnostic`` instance from a given ``DiagnosticDescriptor``, syntax tree, and location markers. |
| 103 | + /// |
| 104 | + /// - Parameters: |
| 105 | + /// - tree: The syntax tree where the diagnostic is rooted. |
| 106 | + /// - markers: A dictionary mapping location markers to their respective offsets in the source code. |
| 107 | + /// |
| 108 | + /// - Throws: |
| 109 | + /// - Error if the location marker is not found in the source code. |
| 110 | + /// - Error if a node corresponding to a given marker is not found in the syntax tree. |
| 111 | + /// |
| 112 | + /// - Returns: A ``Diagnostic`` instance populated with details from the ``DiagnosticDescriptor``. |
| 113 | + func createDiagnostic( |
| 114 | + inSyntaxTree tree: some SyntaxProtocol, |
| 115 | + usingLocationMarkers markers: [LocationMarker: Int] |
| 116 | + ) throws -> Diagnostic { |
| 117 | + func node(at marker: LocationMarker) throws -> Syntax { |
| 118 | + guard let markedOffset = markers[marker] else { |
| 119 | + throw DiagnosticCreationError(message: "Marker \(marker) not found in the marked source") |
| 120 | + } |
| 121 | + let markedPosition = AbsolutePosition(utf8Offset: markedOffset) |
| 122 | + guard let token = tree.token(at: markedPosition) else { |
| 123 | + throw DiagnosticCreationError(message: "Node not found at marker \(marker)") |
| 124 | + } |
| 125 | + return Syntax(token) |
| 126 | + } |
| 127 | + |
| 128 | + let diagnosticNode = try node(at: self.locationMarker) |
| 129 | + |
| 130 | + let notes = try self.noteDescriptors.map { noteDescriptor in |
| 131 | + let noteNode = try node(at: noteDescriptor.locationMarker) |
| 132 | + |
| 133 | + return Note( |
| 134 | + node: noteNode, |
| 135 | + message: SimpleNoteMessage(message: noteDescriptor.message, fixItID: noteDescriptor.id) |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + return Diagnostic( |
| 140 | + node: diagnosticNode, |
| 141 | + message: SimpleDiagnosticMessage( |
| 142 | + message: self.message, |
| 143 | + diagnosticID: self.id, |
| 144 | + severity: self.severity |
| 145 | + ), |
| 146 | + highlights: self.highlight, |
| 147 | + notes: notes, |
| 148 | + fixIts: self.fixIts |
| 149 | + ) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/// Represents a descriptor for constructing a note message in testing. |
| 154 | +struct NoteDescriptor { |
| 155 | + /// The marker pointing to location in source code. |
| 156 | + let locationMarker: LocationMarker |
| 157 | + |
| 158 | + /// The ID associated with the note message. |
| 159 | + let id: MessageID |
| 160 | + |
| 161 | + /// The textual content of the note to be displayed. |
| 162 | + let message: String |
| 163 | +} |
| 164 | + |
| 165 | +/// A simple implementation of the `NoteMessage` protocol for testing. |
| 166 | +/// This struct holds the message text and a fix-it ID for a note. |
| 167 | +struct SimpleNoteMessage: NoteMessage { |
| 168 | + /// The textual content of the note to be displayed. |
| 169 | + let message: String |
| 170 | + |
| 171 | + /// The ID associated with the fix-it, if applicable. |
| 172 | + let fixItID: MessageID |
| 173 | +} |
| 174 | + |
| 175 | +/// A simple implementation of the `DiagnosticMessage` protocol for testing. |
| 176 | +/// This struct holds the message text, diagnostic ID, and severity for a diagnostic. |
| 177 | +struct SimpleDiagnosticMessage: DiagnosticMessage { |
| 178 | + /// The textual content of the diagnostic message to be displayed. |
| 179 | + let message: String |
| 180 | + |
| 181 | + /// The ID associated with the diagnostic message for categorization or referencing. |
| 182 | + let diagnosticID: MessageID |
| 183 | + |
| 184 | + /// The severity level of the diagnostic message. |
| 185 | + let severity: DiagnosticSeverity |
| 186 | +} |
| 187 | + |
| 188 | +/// Asserts that the annotated source generated from diagnostics matches an expected annotated source. |
| 189 | +/// |
| 190 | +/// - Parameters: |
| 191 | +/// - markedSource: The source code with location markers `LocationMarker` for diagnostics. |
| 192 | +/// - withDiagnostics: An array of diagnostic descriptors to generate diagnostics. |
| 193 | +/// - matches: The expected annotated source after applying the diagnostics. |
| 194 | +/// - file: The file in which failure occurred. |
| 195 | +/// - line: The line number on which failure occurred. |
| 196 | +func assertAnnotated( |
| 197 | + markedSource: String, |
| 198 | + withDiagnostics diagnosticDescriptors: [DiagnosticDescriptor], |
| 199 | + matches expectedAnnotatedSource: String, |
| 200 | + file: StaticString = #file, |
| 201 | + line: UInt = #line |
| 202 | +) { |
| 203 | + let (markers, source) = extractMarkers(markedSource) |
| 204 | + let tree = Parser.parse(source: source) |
| 205 | + |
| 206 | + var diagnostics: [Diagnostic] = [] |
| 207 | + |
| 208 | + do { |
| 209 | + diagnostics = try diagnosticDescriptors.map { |
| 210 | + try $0.createDiagnostic(inSyntaxTree: tree, usingLocationMarkers: markers) |
| 211 | + } |
| 212 | + } catch { |
| 213 | + XCTFail(error.localizedDescription, file: file, line: line) |
| 214 | + } |
| 215 | + |
| 216 | + let annotatedSource = DiagnosticsFormatter.annotatedSource(tree: tree, diags: diagnostics) |
| 217 | + |
| 218 | + assertStringsEqualWithDiff( |
| 219 | + annotatedSource, |
| 220 | + expectedAnnotatedSource, |
| 221 | + file: file, |
| 222 | + line: line |
| 223 | + ) |
| 224 | +} |
0 commit comments