-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathtest_error_rendering.cpp
More file actions
335 lines (310 loc) · 8.19 KB
/
test_error_rendering.cpp
File metadata and controls
335 lines (310 loc) · 8.19 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <tests/doctest.h>
#include <iostream>
#include <fstream>
#include <libasr/diagnostics.h>
namespace {
// Strips the first character (\n)
std::string S(const std::string &s) {
return s.substr(1, s.size());
}
}
using LCompilers::diag::Diagnostic;
using LCompilers::diag::Level;
using LCompilers::diag::Stage;
using LCompilers::diag::Label;
using LCompilers::Location;
using LCompilers::LocationManager;
TEST_CASE("Error Render: no labels") {
std::string out, ref;
out = render_diagnostic_human(Diagnostic(
"Error no label",
Level::Error, Stage::Parser
), false
);
ref = S(R"""(
syntax error: Error no label
)""");
CHECK(out == ref);
out = render_diagnostic_human(Diagnostic(
"Error no label",
Level::Error, Stage::Semantic
), false
);
ref = S(R"""(
semantic error: Error no label
)""");
CHECK(out == ref);
out = render_diagnostic_human(Diagnostic(
"Error no label",
Level::Warning, Stage::Semantic
), false
);
ref = S(R"""(
warning: Error no label
)""");
CHECK(out == ref);
}
TEST_CASE("Error Render: primary/secondary labels, single line") {
// All combinations of primary error labels
std::string out, ref, input;
Location loc1, loc2, loc3;
LocationManager lm;
input = "One line text\n";
{
std::ofstream out("input.txt");
out << input;
LocationManager::FileLocations fl;
fl.in_filename = "input.txt";
lm.get_newlines(input, fl.in_newlines);
fl.out_start.push_back(0);
fl.in_start.push_back(0);
fl.in_start.push_back(input.size());
fl.out_start.push_back(input.size());
lm.files.push_back(fl);
lm.file_ends.push_back(input.size());
}
loc1.first = 4;
loc1.last = 7;
loc2.first = 9;
loc2.last = 12;
loc3.first = 0;
loc3.last = 2;
// 1 Label 1 Span
auto d = Diagnostic(
"Error with label no message",
Level::Error, Stage::Semantic, {
Label("", {loc1})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with label no message
--> input.txt:1:5
|
1 | One line text
| ^^^^
)""");
CHECK(out == ref);
// 1 Label 2 Spans
d = Diagnostic(
"Error with label and message",
Level::Error, Stage::Semantic, {
Label("label message", {loc1, loc2})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with label and message
--> input.txt:1:5
|
1 | One line text
| ^^^^ ^^^^ label message
)""");
CHECK(out == ref);
// 1 Label 3 Spans
d = Diagnostic(
"Error with label and message",
Level::Error, Stage::Semantic, {
Label("label message", {loc1, loc2, loc3})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with label and message
--> input.txt:1:5
|
1 | One line text
| ^^^^ ^^^^ label message
|
1 | One line text
| ^^^ label message
)""");
CHECK(out == ref);
// 2 Label 1 Span
d = Diagnostic(
"Error with two labels and message",
Level::Error, Stage::Semantic, {
Label("label1 message", {loc1}),
Label("label2 message", {loc2})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with two labels and message
--> input.txt:1:5
|
1 | One line text
| ^^^^ label1 message
|
1 | One line text
| ^^^^ label2 message
)""");
CHECK(out == ref);
// 3 Label 1 Span
d = Diagnostic(
"Error with two labels and message",
Level::Error, Stage::Semantic, {
Label("label1 message", {loc1}),
Label("label2 message", {loc2}),
Label("label3 message", {loc3})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with two labels and message
--> input.txt:1:5
|
1 | One line text
| ^^^^ label1 message
|
1 | One line text
| ^^^^ label2 message
|
1 | One line text
| ^^^ label3 message
)""");
CHECK(out == ref);
// 3 Label 1 Span, one primary, two secondary
d = Diagnostic(
"Error with two labels and message",
Level::Error, Stage::Semantic, {
Label("label1 primary message", {loc1}),
Label("label2 secondary message", {loc2}, false),
Label("label3 secondary message", {loc3}, false)
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with two labels and message
--> input.txt:1:5
|
1 | One line text
| ^^^^ label1 primary message
|
1 | One line text
| ~~~~ label2 secondary message
|
1 | One line text
| ~~~ label3 secondary message
)""");
CHECK(out == ref);
// 3 Label 2 Spans, secondary, primary, secondary
d = Diagnostic(
"Error with three labels and message, two spans",
Level::Error, Stage::Semantic, {
Label("label1 secondary message", {loc1, loc2}, false),
Label("label2 primary message", {loc3, loc2}),
Label("label3 secondary message", {loc3}, false)
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with three labels and message, two spans
--> input.txt:1:5
|
1 | One line text
| ~~~~ ~~~~ label1 secondary message
|
1 | One line text
| ^^^ ^^^^ label2 primary message
|
1 | One line text
| ~~~ label3 secondary message
)""");
CHECK(out == ref);
}
TEST_CASE("Error Render: primary/secondary labels, multi line") {
std::string out, ref, input;
Location loc1, loc2, loc3;
LocationManager lm;
input = "One line text\nSecond line text\nThird line text\n";
{
std::ofstream out("input.txt");
out << input;
LocationManager::FileLocations fl;
fl.in_filename = "input.txt";
lm.get_newlines(input, fl.in_newlines);
fl.out_start.push_back(0);
fl.in_start.push_back(0);
fl.in_start.push_back(input.size());
fl.out_start.push_back(input.size());
lm.files.push_back(fl);
lm.file_ends.push_back(input.size());
}
loc1.first = 4; // 1 line
loc1.last = 24; // 2 line
loc2.first = 9; // 1 text
loc2.last = 35; // 3 Third
loc3.first = 0; // 1 One
loc3.last = 2; // 3 Third
// 1 Label 1 Span
auto d = Diagnostic(
"Error with label no message",
Level::Error, Stage::Semantic, {
Label("Multilines", {loc1})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with label no message
--> input.txt:1:5 - 2:11
|
1 | One line text
| ^^^^^^^^^...
...
|
2 | Second line text
| ...^^^^^^^^^^^ Multilines
)""");
CHECK(out == ref);
// 1 Label 2 Span
d = Diagnostic(
"Error with label, two spans",
Level::Error, Stage::Semantic, {
Label("Two spans", {loc1, loc2})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with label, two spans
--> input.txt:1:5 - 2:11
|
1 | One line text
| ^^^^^^^^^...
...
|
2 | Second line text
| ...^^^^^^^^^^^ Two spans
|
1 | One line text
| ^^^^...
...
|
3 | Third line text
| ...^^^^^ Two spans
)""");
CHECK(out == ref);
// 1 Label 2 Span
d = Diagnostic(
"Error with label, two spans",
Level::Error, Stage::Semantic, {
Label("Two spans", {loc3, loc2})
}
);
out = render_diagnostic_human(d, lm, false, false);
ref = S(R"""(
semantic error: Error with label, two spans
--> input.txt:1:1
|
1 | One line text
| ^^^ Two spans
|
1 | One line text
| ^^^^...
...
|
3 | Third line text
| ...^^^^^ Two spans
)""");
CHECK(out == ref);
}