File: SerializedDiagnosticsTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (196 lines) | stat: -rw-r--r-- 8,474 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2020 Apple Inc. and the Swift project authors
 Licensed under Apache License v2.0 with Runtime Library Exception

 See http://swift.org/LICENSE.txt for license information
 See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest
import Foundation
import TSCBasic
import TSCUtility

final class SerializedDiagnosticsTests: XCTestCase {
  func testReadSwiftDiagnosticWithNote() throws {
    let serializedDiagnosticsPath = AbsolutePath(#file).parentDirectory
      .appending(components: "Inputs", "multiblock.dia")
    let contents = try localFileSystem.readFileContents(serializedDiagnosticsPath)
    let serializedDiags = try SerializedDiagnostics(bytes: contents)

    XCTAssertEqual(serializedDiags.versionNumber, 2)
    XCTAssertEqual(serializedDiags.diagnostics.count, 7)

    struct TestSrcLoc: Equatable {
        var filename: String
        var line: UInt64
        var column: UInt64
        var offset: UInt64

        init(filename: String, line: UInt64, column: UInt64, offset: UInt64) {
            self.filename = filename
            self.line = line
            self.column = column
            self.offset = offset
        }

        init(_ original: SerializedDiagnostics.SourceLocation) {
            self.filename = original.filename
            self.line = original.line
            self.column = original.column
            self.offset = original.offset
        }
    }

    struct TestDiag {
        var text: String
        var level: SerializedDiagnostics.Diagnostic.Level
        var location: TestSrcLoc?
        var category: String?
        var flag: String?

    }

    let expectedResults = [
        TestDiag(text: "type 'A' does not conform to protocol 'P'",
                 level: .error,
                 location: TestSrcLoc(filename: "test.swift",
                                      line: 5, column: 7, offset: 35)),
        TestDiag(text: "candidate is 'async', but protocol requirement is not",
                 level: .note,
                 location: TestSrcLoc(filename: "test.swift",
                                      line: 6, column: 10, offset: 51)),
        TestDiag(text: "do you want to add protocol stubs?",
                 level: .note,
                 location: TestSrcLoc(filename: "test.swift",
                                      line: 5, column: 7, offset: 35)),
        TestDiag(text: "initialization of immutable value 'a' was never used",
                 level: .warning,
                 location: TestSrcLoc(filename: "test.swift",
                                      line: 7, column: 13, offset: 75)),
        TestDiag(text: "consider replacing with '_' or removing it",
                 level: .note,
                 location: TestSrcLoc(filename: "test.swift",
                                      line: 7, column: 13, offset: 75)),
        TestDiag(text: "initialization of immutable value 'b' was never used",
                 level: .warning,
                 location: TestSrcLoc(filename: "test.swift",
                                      line: 8, column: 13, offset: 94)),
        TestDiag(text: "consider replacing with '_' or removing it",
                 level: .note,
                 location: TestSrcLoc(filename: "test.swift",
                                      line: 8, column: 13, offset: 94))
    ]

    for case let (diag, expected) in zip(serializedDiags.diagnostics,
        expectedResults) {
        XCTAssertEqual(diag.text, expected.text, "Mismatched Diagnostic Text")
        XCTAssertEqual(diag.level, expected.level, "Mismatched Diagnostic Level")

        XCTAssertEqual((diag.location == nil), (expected.location == nil), "Unexpected Diagnostic Location")
        if let diagLoc = diag.location, let expectedLoc = expected.location {
            XCTAssertEqual(TestSrcLoc(diagLoc), expectedLoc, "Mismatched Diagnostic Location")
        }
    }
  }

  func testReadSwiftSerializedDiags() throws {
    let serializedDiagnosticsPath = AbsolutePath(#file).parentDirectory
        .appending(components: "Inputs", "serialized.dia")
    let contents = try localFileSystem.readFileContents(serializedDiagnosticsPath)
    let serializedDiags = try SerializedDiagnostics(bytes: contents)

    XCTAssertEqual(serializedDiags.versionNumber, 1)
    XCTAssertEqual(serializedDiags.diagnostics.count, 17)

    let one = serializedDiags.diagnostics[5]
    XCTAssertEqual(one.text, "expected ',' separator")
    XCTAssertEqual(one.level, .error)
    XCTAssertEqual(one.location?.filename.hasSuffix("/StoreSearchCoordinator.swift"), true)
    XCTAssertEqual(one.location?.line, 21)
    XCTAssertEqual(one.location?.column, 69)
    XCTAssertEqual(one.location?.offset, 0)
    XCTAssertNil(one.category)
    XCTAssertNil(one.flag)
    XCTAssertEqual(one.ranges.count, 0)
    XCTAssertEqual(one.fixIts.count, 1)
    XCTAssertEqual(one.fixIts[0].text, ",")
    XCTAssertEqual(one.fixIts[0].start, one.fixIts[0].end)
    XCTAssertEqual(one.fixIts[0].start.filename.hasSuffix("/StoreSearchCoordinator.swift"), true)
    XCTAssertEqual(one.fixIts[0].start.line, 21)
    XCTAssertEqual(one.fixIts[0].start.column, 69)
    XCTAssertEqual(one.fixIts[0].start.offset, 0)

    let two = serializedDiags.diagnostics[16]
    XCTAssertEqual(two.text, "use of unresolved identifier 'DispatchQueue'")
    XCTAssertEqual(two.level, .error)
    XCTAssertEqual(two.location?.filename.hasSuffix("/Observable.swift"), true)
    XCTAssertEqual(two.location?.line, 34)
    XCTAssertEqual(two.location?.column, 13)
    XCTAssertEqual(two.location?.offset, 0)
    XCTAssertNil(two.category)
    XCTAssertNil(two.flag)
    XCTAssertEqual(two.ranges.count, 1)
    XCTAssertEqual(two.ranges[0].0.filename.hasSuffix("/Observable.swift"), true)
    XCTAssertEqual(two.ranges[0].0.line, 34)
    XCTAssertEqual(two.ranges[0].0.column, 13)
    XCTAssertEqual(two.ranges[0].0.offset, 0)
    XCTAssertEqual(two.ranges[0].1.filename.hasSuffix("/Observable.swift"), true)
    XCTAssertEqual(two.ranges[0].1.line, 34)
    XCTAssertEqual(two.ranges[0].1.column, 26)
    XCTAssertEqual(two.ranges[0].1.offset, 0)
    XCTAssertEqual(two.fixIts.count, 0)
  }

  func testReadDiagsWithNoLocation() throws {
    let serializedDiagnosticsPath = AbsolutePath(#file).parentDirectory
        .appending(components: "Inputs", "no-location.dia")
    let contents = try localFileSystem.readFileContents(serializedDiagnosticsPath)
    let serializedDiags = try SerializedDiagnostics(bytes: contents)

    XCTAssertEqual(serializedDiags.versionNumber, 2)
    XCTAssertEqual(serializedDiags.diagnostics.count, 1)

    let diag = serializedDiags.diagnostics[0]
    XCTAssertEqual(diag.text, "API breakage: func foo() has been removed")
    XCTAssertEqual(diag.level, .error)
    XCTAssertNil(diag.location)
    XCTAssertEqual(diag.category, "api-digester-breaking-change")
    XCTAssertNil(diag.flag)
    XCTAssertEqual(diag.ranges.count, 0)
    XCTAssertEqual(diag.fixIts.count, 0)
  }

  func testReadDiagsWithUnknownRecord() throws {
    let serializedDiagnosticsPath = AbsolutePath(#file).parentDirectory
        .appending(components: "Inputs", "string_init_ambig.dia")
    let contents = try localFileSystem.readFileContents(serializedDiagnosticsPath)
    let serializedDiags = try SerializedDiagnostics(bytes: contents)

    XCTAssertEqual(serializedDiags.versionNumber, 2)
    XCTAssertEqual(serializedDiags.diagnostics.count, 35)
  }

  func testReadClangSerializedDiags() throws {
    let serializedDiagnosticsPath = AbsolutePath(#file).parentDirectory
        .appending(components: "Inputs", "clang.dia")
    let contents = try localFileSystem.readFileContents(serializedDiagnosticsPath)
    let serializedDiags = try SerializedDiagnostics(bytes: contents)

    XCTAssertEqual(serializedDiags.versionNumber, 1)
    XCTAssertEqual(serializedDiags.diagnostics.count, 4)

    let one = serializedDiags.diagnostics[1]
    XCTAssertEqual(one.text, "values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead")
    XCTAssertEqual(one.level, .warning)
    XCTAssertEqual(one.location?.line, 252)
    XCTAssertEqual(one.location?.column, 137)
    XCTAssertEqual(one.location?.offset, 10046)
    XCTAssertEqual(one.category, "Format String Issue")
    XCTAssertEqual(one.flag, "format")
    XCTAssertEqual(one.ranges.count, 4)
    XCTAssertEqual(one.fixIts.count, 2)
  }
}