-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathDiagnosticsTests.swift
More file actions
182 lines (170 loc) · 7.58 KB
/
Copy pathDiagnosticsTests.swift
File metadata and controls
182 lines (170 loc) · 7.58 KB
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
import SwiftParser
import SwiftSyntax
import Testing
@testable import BridgeJSCore
@Suite struct DiagnosticsTests {
/// Returns the first parameter's type node from a function in the source (the first `@JS func`-like decl), for pinpointing diagnostics.
private func firstParameterTypeNode(source: String) -> TypeSyntax? {
let tree = Parser.parse(source: source)
for stmt in tree.statements {
if let funcDecl = stmt.item.as(FunctionDeclSyntax.self),
let firstParam = funcDecl.signature.parameterClause.parameters.first
{
return firstParam.type
}
}
return nil
}
@Test
func diagnosticIncludesLocationSourceAndHint() throws {
let source = "@JS func foo(_ bar: A<X>) {}\n"
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(
node: typeNode,
message: "Unsupported type 'A<X>'.",
hint: "Only primitive types and types defined in the same module are allowed"
)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
let expectedPrefix = """
<stdin>:1:21: error: Unsupported type 'A<X>'.
1 | @JS func foo(_ bar: A<X>) {}
| `- error: Unsupported type 'A<X>'.
2 |
""".trimmingCharacters(in: .whitespacesAndNewlines)
#expect(description.hasPrefix(expectedPrefix))
#expect(description.contains("Hint: Only primitive types and types defined in the same module are allowed"))
}
@Test
func diagnosticOmitsHintWhenNotProvided() throws {
let source = "@JS static func foo() {}\n"
let tree = Parser.parse(source: source)
let diagnostic = DiagnosticError(
node: tree,
message: "Top-level functions cannot be static",
hint: nil
)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
let expectedPrefix = """
<stdin>:1:1: error: Top-level functions cannot be static
1 | @JS static func foo() {}
| `- error: Top-level functions cannot be static
2 |
""".trimmingCharacters(in: .whitespacesAndNewlines)
#expect(description.hasPrefix(expectedPrefix))
#expect(!description.contains("Hint:"))
}
@Test
func diagnosticUsesGivenFileNameNotStdin() throws {
let source = "@JS func foo(_ bar: A<X>) {}\n"
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(
node: typeNode,
message: "Unsupported type 'A<X>'.",
hint: nil
)
let description = diagnostic.formattedDescription(fileName: "Sources/Foo.swift", colorize: false)
#expect(description.hasPrefix("Sources/Foo.swift:1:21: error: Unsupported type 'A<X>'."))
}
@Test
func diagnosticWithColorizeTrueIncludesANSISequences() throws {
let source = "@JS func foo(_ bar: A<X>) {}\n"
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(
node: typeNode,
message: "Unsupported type 'A<X>'.",
hint: nil
)
let description = diagnostic.formattedDescription(fileName: "-", colorize: true)
let esc = "\u{001B}"
let boldRed = "\(esc)[1;31m"
let boldDefault = "\(esc)[1;39m"
let reset = "\(esc)[0;0m"
let cyan = "\(esc)[0;36m"
let underline = "\(esc)[4;39m"
let expected =
"<stdin>:1:21: \(boldRed)error: \(boldDefault)Unsupported type 'A<X>'.\(reset)\n"
+ "\(cyan) 1\(reset) | @JS func foo(_ bar: \(underline)A<X>\(reset)) {}\n"
+ " | `- \(boldRed)error: \(boldDefault)Unsupported type 'A<X>'.\(reset)\n"
+ "\(cyan) 2\(reset) | "
#expect(description == expected)
}
// MARK: - Context source lines
@Test
func showsOnePreviousLineWhenErrorNotOnFirstLine() throws {
let source = """
preamble
@JS func foo(_ bar: A<X>) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 1 | preamble"))
#expect(description.contains(" 2 | @JS func foo(_ bar: A<X>) {}"))
#expect(description.contains("<stdin>:2:"))
}
@Test
func showsThreePreviousLinesWhenAvailable() throws {
let source = """
first
second
third
@JS func foo(_ bar: A<X>) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 1 | first"))
#expect(description.contains(" 2 | second"))
#expect(description.contains(" 3 | third"))
#expect(description.contains(" 4 | @JS func foo(_ bar: A<X>) {}"))
#expect(description.contains("<stdin>:4:"))
}
@Test
func capsContextAtThreePreviousLines() throws {
let source = """
line0
line1
line2
line3
@JS func foo(_ bar: A<X>) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(!description.contains(" 1 | line0"))
#expect(description.contains(" 2 | line1"))
#expect(description.contains(" 3 | line2"))
#expect(description.contains(" 4 | line3"))
#expect(description.contains(" 5 | @JS func foo(_ bar: A<X>) {}"))
#expect(description.contains("<stdin>:5:"))
}
@Test
func includesNextLineAfterErrorLine() throws {
let source = """
@JS func foo(
_ bar: A<X>
) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 1 | @JS func foo("))
#expect(description.contains(" 2 | _ bar: A<X>"))
#expect(description.contains(" 3 | ) {}"))
#expect(description.contains("<stdin>:2:"))
}
@Test
func omitsNextLineWhenErrorIsOnLastLine() throws {
let source = """
preamble
@JS func foo(_ bar: A<X>)
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 2 | @JS func foo(_ bar: A<X>)"))
#expect(description.contains("<stdin>:2:"))
// No line 3 in source, so output must not show a " 3 |" context line after the pointer
#expect(!description.contains(" 3 |"))
}
}