]> git.proxmox.com Git - rustc.git/blame - vendor/annotate-snippets-0.6.1/tests/formatter.rs
Update upstream source from tag 'upstream/1.52.1+dfsg1'
[rustc.git] / vendor / annotate-snippets-0.6.1 / tests / formatter.rs
CommitLineData
f20569fa
XL
1use annotate_snippets::display_list::*;
2use annotate_snippets::formatter::DisplayListFormatter;
3
4#[test]
5fn test_source_empty() {
6 let dl = DisplayList::from(vec![DisplayLine::Source {
7 lineno: None,
8 inline_marks: vec![],
9 line: DisplaySourceLine::Empty,
10 }]);
11
12 let dlf = DisplayListFormatter::new(false, false);
13
14 assert_eq!(dlf.format(&dl), " |");
15}
16
17#[test]
18fn test_source_content() {
19 let dl = DisplayList::from(vec![
20 DisplayLine::Source {
21 lineno: Some(56),
22 inline_marks: vec![],
23 line: DisplaySourceLine::Content {
24 text: "This is an example".to_string(),
25 range: (0, 19),
26 },
27 },
28 DisplayLine::Source {
29 lineno: Some(57),
30 inline_marks: vec![],
31 line: DisplaySourceLine::Content {
32 text: "of content lines".to_string(),
33 range: (0, 19),
34 },
35 },
36 ]);
37
38 let dlf = DisplayListFormatter::new(false, false);
39
40 assert_eq!(
41 dlf.format(&dl),
42 "56 | This is an example\n57 | of content lines"
43 );
44}
45
46#[test]
47fn test_source_annotation_standalone_singleline() {
48 let dl = DisplayList::from(vec![DisplayLine::Source {
49 lineno: None,
50 inline_marks: vec![],
51 line: DisplaySourceLine::Annotation {
52 range: (0, 5),
53 annotation: Annotation {
54 annotation_type: DisplayAnnotationType::None,
55 id: None,
56 label: vec![DisplayTextFragment {
57 content: String::from("Example string"),
58 style: DisplayTextStyle::Regular,
59 }],
60 },
61 annotation_type: DisplayAnnotationType::Error,
62 annotation_part: DisplayAnnotationPart::Standalone,
63 },
64 }]);
65
66 let dlf = DisplayListFormatter::new(false, false);
67
68 assert_eq!(dlf.format(&dl), " | ^^^^^ Example string");
69}
70
71#[test]
72fn test_source_annotation_standalone_multiline() {
73 let dl = DisplayList::from(vec![
74 DisplayLine::Source {
75 lineno: None,
76 inline_marks: vec![],
77 line: DisplaySourceLine::Annotation {
78 range: (0, 5),
79 annotation: Annotation {
80 annotation_type: DisplayAnnotationType::Help,
81 id: None,
82 label: vec![DisplayTextFragment {
83 content: String::from("Example string"),
84 style: DisplayTextStyle::Regular,
85 }],
86 },
87 annotation_type: DisplayAnnotationType::Warning,
88 annotation_part: DisplayAnnotationPart::Standalone,
89 },
90 },
91 DisplayLine::Source {
92 lineno: None,
93 inline_marks: vec![],
94 line: DisplaySourceLine::Annotation {
95 range: (0, 5),
96 annotation: Annotation {
97 annotation_type: DisplayAnnotationType::Help,
98 id: None,
99 label: vec![DisplayTextFragment {
100 content: String::from("Second line"),
101 style: DisplayTextStyle::Regular,
102 }],
103 },
104 annotation_type: DisplayAnnotationType::Warning,
105 annotation_part: DisplayAnnotationPart::LabelContinuation,
106 },
107 },
108 ]);
109
110 let dlf = DisplayListFormatter::new(false, false);
111
112 assert_eq!(
113 dlf.format(&dl),
114 " | ----- help: Example string\n | Second line"
115 );
116}
117
118#[test]
119fn test_source_annotation_standalone_multi_annotation() {
120 let dl = DisplayList::from(vec![
121 DisplayLine::Source {
122 lineno: None,
123 inline_marks: vec![],
124 line: DisplaySourceLine::Annotation {
125 range: (0, 5),
126 annotation: Annotation {
127 annotation_type: DisplayAnnotationType::Info,
128 id: None,
129 label: vec![DisplayTextFragment {
130 content: String::from("Example string"),
131 style: DisplayTextStyle::Regular,
132 }],
133 },
134 annotation_type: DisplayAnnotationType::Note,
135 annotation_part: DisplayAnnotationPart::Standalone,
136 },
137 },
138 DisplayLine::Source {
139 lineno: None,
140 inline_marks: vec![],
141 line: DisplaySourceLine::Annotation {
142 range: (0, 5),
143 annotation: Annotation {
144 annotation_type: DisplayAnnotationType::Info,
145 id: None,
146 label: vec![DisplayTextFragment {
147 content: String::from("Second line"),
148 style: DisplayTextStyle::Regular,
149 }],
150 },
151 annotation_type: DisplayAnnotationType::Note,
152 annotation_part: DisplayAnnotationPart::LabelContinuation,
153 },
154 },
155 DisplayLine::Source {
156 lineno: None,
157 inline_marks: vec![],
158 line: DisplaySourceLine::Annotation {
159 range: (0, 5),
160 annotation: Annotation {
161 annotation_type: DisplayAnnotationType::Warning,
162 id: None,
163 label: vec![DisplayTextFragment {
164 content: String::from("This is a note"),
165 style: DisplayTextStyle::Regular,
166 }],
167 },
168 annotation_type: DisplayAnnotationType::Note,
169 annotation_part: DisplayAnnotationPart::Consequitive,
170 },
171 },
172 DisplayLine::Source {
173 lineno: None,
174 inline_marks: vec![],
175 line: DisplaySourceLine::Annotation {
176 range: (0, 5),
177 annotation: Annotation {
178 annotation_type: DisplayAnnotationType::Warning,
179 id: None,
180 label: vec![DisplayTextFragment {
181 content: String::from("Second line of the warning"),
182 style: DisplayTextStyle::Regular,
183 }],
184 },
185 annotation_type: DisplayAnnotationType::Note,
186 annotation_part: DisplayAnnotationPart::LabelContinuation,
187 },
188 },
189 DisplayLine::Source {
190 lineno: None,
191 inline_marks: vec![],
192 line: DisplaySourceLine::Annotation {
193 range: (0, 5),
194 annotation: Annotation {
195 annotation_type: DisplayAnnotationType::Info,
196 id: None,
197 label: vec![DisplayTextFragment {
198 content: String::from("This is an info"),
199 style: DisplayTextStyle::Regular,
200 }],
201 },
202 annotation_type: DisplayAnnotationType::Info,
203 annotation_part: DisplayAnnotationPart::Standalone,
204 },
205 },
206 DisplayLine::Source {
207 lineno: None,
208 inline_marks: vec![],
209 line: DisplaySourceLine::Annotation {
210 range: (0, 5),
211 annotation: Annotation {
212 annotation_type: DisplayAnnotationType::Help,
213 id: None,
214 label: vec![DisplayTextFragment {
215 content: String::from("This is help"),
216 style: DisplayTextStyle::Regular,
217 }],
218 },
219 annotation_type: DisplayAnnotationType::Help,
220 annotation_part: DisplayAnnotationPart::Standalone,
221 },
222 },
223 DisplayLine::Source {
224 lineno: None,
225 inline_marks: vec![],
226 line: DisplaySourceLine::Annotation {
227 range: (0, 0),
228 annotation: Annotation {
229 annotation_type: DisplayAnnotationType::None,
230 id: None,
231 label: vec![DisplayTextFragment {
232 content: String::from("This is an annotation of type none"),
233 style: DisplayTextStyle::Regular,
234 }],
235 },
236 annotation_type: DisplayAnnotationType::None,
237 annotation_part: DisplayAnnotationPart::Standalone,
238 },
239 },
240 ]);
241
242 let dlf = DisplayListFormatter::new(false, false);
243
244 assert_eq!(dlf.format(&dl), " | ----- info: Example string\n | Second line\n | warning: This is a note\n | Second line of the warning\n | ----- info: This is an info\n | ----- help: This is help\n | This is an annotation of type none");
245}
246
247#[test]
248fn test_fold_line() {
249 let dl = DisplayList::from(vec![
250 DisplayLine::Source {
251 lineno: Some(5),
252 inline_marks: vec![],
253 line: DisplaySourceLine::Content {
254 text: "This is line 5".to_string(),
255 range: (0, 19),
256 },
257 },
258 DisplayLine::Fold {
259 inline_marks: vec![],
260 },
261 DisplayLine::Source {
262 lineno: Some(10021),
263 inline_marks: vec![],
264 line: DisplaySourceLine::Content {
265 text: "... and now we're at line 10021".to_string(),
266 range: (0, 19),
267 },
268 },
269 ]);
270
271 let dlf = DisplayListFormatter::new(false, false);
272
273 assert_eq!(
274 dlf.format(&dl),
275 " 5 | This is line 5\n...\n10021 | ... and now we're at line 10021"
276 );
277}
278
279#[test]
280fn test_raw_origin_initial_nopos() {
281 let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Origin {
282 path: "src/test.rs".to_string(),
283 pos: None,
284 header_type: DisplayHeaderType::Initial,
285 })]);
286
287 let dlf = DisplayListFormatter::new(false, false);
288
289 assert_eq!(dlf.format(&dl), "--> src/test.rs");
290}
291
292#[test]
293fn test_raw_origin_initial_pos() {
294 let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Origin {
295 path: "src/test.rs".to_string(),
296 pos: Some((23, 15)),
297 header_type: DisplayHeaderType::Initial,
298 })]);
299
300 let dlf = DisplayListFormatter::new(false, false);
301
302 assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
303}
304
305#[test]
306fn test_raw_origin_continuation() {
307 let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Origin {
308 path: "src/test.rs".to_string(),
309 pos: Some((23, 15)),
310 header_type: DisplayHeaderType::Continuation,
311 })]);
312
313 let dlf = DisplayListFormatter::new(false, false);
314
315 assert_eq!(dlf.format(&dl), "::: src/test.rs:23:15");
316}
317
318#[test]
319fn test_raw_annotation_unaligned() {
320 let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Annotation {
321 annotation: Annotation {
322 annotation_type: DisplayAnnotationType::Error,
323 id: Some("E0001".to_string()),
324 label: vec![DisplayTextFragment {
325 content: String::from("This is an error"),
326 style: DisplayTextStyle::Regular,
327 }],
328 },
329 source_aligned: false,
330 continuation: false,
331 })]);
332
333 let dlf = DisplayListFormatter::new(false, false);
334
335 assert_eq!(dlf.format(&dl), "error[E0001]: This is an error");
336}
337
338#[test]
339fn test_raw_annotation_unaligned_multiline() {
340 let dl = DisplayList::from(vec![
341 DisplayLine::Raw(DisplayRawLine::Annotation {
342 annotation: Annotation {
343 annotation_type: DisplayAnnotationType::Warning,
344 id: Some("E0001".to_string()),
345 label: vec![DisplayTextFragment {
346 content: String::from("This is an error"),
347 style: DisplayTextStyle::Regular,
348 }],
349 },
350 source_aligned: false,
351 continuation: false,
352 }),
353 DisplayLine::Raw(DisplayRawLine::Annotation {
354 annotation: Annotation {
355 annotation_type: DisplayAnnotationType::Warning,
356 id: Some("E0001".to_string()),
357 label: vec![DisplayTextFragment {
358 content: String::from("Second line of the error"),
359 style: DisplayTextStyle::Regular,
360 }],
361 },
362 source_aligned: false,
363 continuation: true,
364 }),
365 ]);
366
367 let dlf = DisplayListFormatter::new(false, false);
368
369 assert_eq!(
370 dlf.format(&dl),
371 "warning[E0001]: This is an error\n Second line of the error"
372 );
373}
374
375#[test]
376fn test_raw_annotation_aligned() {
377 let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Annotation {
378 annotation: Annotation {
379 annotation_type: DisplayAnnotationType::Error,
380 id: Some("E0001".to_string()),
381 label: vec![DisplayTextFragment {
382 content: String::from("This is an error"),
383 style: DisplayTextStyle::Regular,
384 }],
385 },
386 source_aligned: true,
387 continuation: false,
388 })]);
389
390 let dlf = DisplayListFormatter::new(false, false);
391
392 assert_eq!(dlf.format(&dl), " = error[E0001]: This is an error");
393}
394
395#[test]
396fn test_raw_annotation_aligned_multiline() {
397 let dl = DisplayList::from(vec![
398 DisplayLine::Raw(DisplayRawLine::Annotation {
399 annotation: Annotation {
400 annotation_type: DisplayAnnotationType::Warning,
401 id: Some("E0001".to_string()),
402 label: vec![DisplayTextFragment {
403 content: String::from("This is an error"),
404 style: DisplayTextStyle::Regular,
405 }],
406 },
407 source_aligned: true,
408 continuation: false,
409 }),
410 DisplayLine::Raw(DisplayRawLine::Annotation {
411 annotation: Annotation {
412 annotation_type: DisplayAnnotationType::Warning,
413 id: Some("E0001".to_string()),
414 label: vec![DisplayTextFragment {
415 content: String::from("Second line of the error"),
416 style: DisplayTextStyle::Regular,
417 }],
418 },
419 source_aligned: true,
420 continuation: true,
421 }),
422 ]);
423
424 let dlf = DisplayListFormatter::new(false, false);
425
426 assert_eq!(
427 dlf.format(&dl),
428 " = warning[E0001]: This is an error\n Second line of the error"
429 );
430}
431
432#[test]
433fn test_different_annotation_types() {
434 let dl = DisplayList::from(vec![
435 DisplayLine::Raw(DisplayRawLine::Annotation {
436 annotation: Annotation {
437 annotation_type: DisplayAnnotationType::Note,
438 id: None,
439 label: vec![DisplayTextFragment {
440 content: String::from("This is a note"),
441 style: DisplayTextStyle::Regular,
442 }],
443 },
444 source_aligned: false,
445 continuation: false,
446 }),
447 DisplayLine::Raw(DisplayRawLine::Annotation {
448 annotation: Annotation {
449 annotation_type: DisplayAnnotationType::None,
450 id: None,
451 label: vec![DisplayTextFragment {
452 content: String::from("This is just a string"),
453 style: DisplayTextStyle::Regular,
454 }],
455 },
456 source_aligned: false,
457 continuation: false,
458 }),
459 DisplayLine::Raw(DisplayRawLine::Annotation {
460 annotation: Annotation {
461 annotation_type: DisplayAnnotationType::None,
462 id: None,
463 label: vec![DisplayTextFragment {
464 content: String::from("Second line of none type annotation"),
465 style: DisplayTextStyle::Regular,
466 }],
467 },
468 source_aligned: false,
469 continuation: true,
470 }),
471 ]);
472
473 let dlf = DisplayListFormatter::new(false, false);
474
475 assert_eq!(
476 dlf.format(&dl),
477 "note: This is a note\nThis is just a string\n Second line of none type annotation",
478 );
479}
480
481#[test]
482fn test_inline_marks_empty_line() {
483 let dl = DisplayList::from(vec![DisplayLine::Source {
484 lineno: None,
485 inline_marks: vec![DisplayMark {
486 mark_type: DisplayMarkType::AnnotationThrough,
487 annotation_type: DisplayAnnotationType::Error,
488 }],
489 line: DisplaySourceLine::Empty,
490 }]);
491
492 let dlf = DisplayListFormatter::new(false, false);
493
494 assert_eq!(dlf.format(&dl), " | |",);
495}
496
497#[test]
498fn test_anon_lines() {
499 let dl = DisplayList::from(vec![
500 DisplayLine::Source {
501 lineno: Some(56),
502 inline_marks: vec![],
503 line: DisplaySourceLine::Content {
504 text: "This is an example".to_string(),
505 range: (0, 19),
506 },
507 },
508 DisplayLine::Source {
509 lineno: Some(57),
510 inline_marks: vec![],
511 line: DisplaySourceLine::Content {
512 text: "of content lines".to_string(),
513 range: (0, 19),
514 },
515 },
516 DisplayLine::Source {
517 lineno: None,
518 inline_marks: vec![],
519 line: DisplaySourceLine::Empty,
520 },
521 DisplayLine::Source {
522 lineno: None,
523 inline_marks: vec![],
524 line: DisplaySourceLine::Content {
525 text: "abc".to_string(),
526 range: (0, 19),
527 },
528 },
529 ]);
530
531 let dlf = DisplayListFormatter::new(false, true);
532
533 assert_eq!(
534 dlf.format(&dl),
535 "LL | This is an example\nLL | of content lines\n |\n | abc"
536 );
537}
538
539#[test]
540fn test_raw_origin_initial_pos_anon_lines() {
541 let dl = DisplayList::from(vec![DisplayLine::Raw(DisplayRawLine::Origin {
542 path: "src/test.rs".to_string(),
543 pos: Some((23, 15)),
544 header_type: DisplayHeaderType::Initial,
545 })]);
546
547 let dlf = DisplayListFormatter::new(false, true);
548
549 // Using anonymized_line_numbers should not affect the inital position
550 assert_eq!(dlf.format(&dl), "--> src/test.rs:23:15");
551}