1 use annotate_snippets::display_list::DisplayList;
2 use annotate_snippets::{display_list as dl, formatter::get_term_style, snippet};
3
4 #[test]
test_format_title()5 fn test_format_title() {
6 let input = snippet::Snippet {
7 title: Some(snippet::Annotation {
8 id: Some("E0001"),
9 label: Some("This is a title"),
10 annotation_type: snippet::AnnotationType::Error,
11 }),
12 footer: vec![],
13 slices: vec![],
14 opt: Default::default(),
15 };
16 let output = dl::DisplayList {
17 body: vec![dl::DisplayLine::Raw(dl::DisplayRawLine::Annotation {
18 annotation: dl::Annotation {
19 annotation_type: dl::DisplayAnnotationType::Error,
20 id: Some("E0001"),
21 label: vec![dl::DisplayTextFragment {
22 content: "This is a title",
23 style: dl::DisplayTextStyle::Emphasis,
24 }],
25 },
26 source_aligned: false,
27 continuation: false,
28 })],
29 stylesheet: get_term_style(input.opt.color),
30 anonymized_line_numbers: input.opt.anonymized_line_numbers,
31 margin: None,
32 };
33 assert_eq!(dl::DisplayList::from(input), output);
34 }
35
36 #[test]
test_format_slice()37 fn test_format_slice() {
38 let line_1 = "This is line 1";
39 let line_2 = "This is line 2";
40 let source = vec![line_1, line_2].join("\n");
41 let input = snippet::Snippet {
42 title: None,
43 footer: vec![],
44 slices: vec![snippet::Slice {
45 source: &source,
46 line_start: 5402,
47 origin: None,
48 annotations: vec![],
49 fold: false,
50 }],
51 opt: Default::default(),
52 };
53 let output = dl::DisplayList {
54 body: vec![
55 dl::DisplayLine::Source {
56 lineno: None,
57 inline_marks: vec![],
58 line: dl::DisplaySourceLine::Empty,
59 },
60 dl::DisplayLine::Source {
61 lineno: Some(5402),
62 inline_marks: vec![],
63 line: dl::DisplaySourceLine::Content {
64 text: line_1,
65 range: (0, line_1.len()),
66 },
67 },
68 dl::DisplayLine::Source {
69 lineno: Some(5403),
70 inline_marks: vec![],
71 line: dl::DisplaySourceLine::Content {
72 range: (line_1.len() + 1, source.len()),
73 text: line_2,
74 },
75 },
76 dl::DisplayLine::Source {
77 lineno: None,
78 inline_marks: vec![],
79 line: dl::DisplaySourceLine::Empty,
80 },
81 ],
82 stylesheet: get_term_style(input.opt.color),
83 anonymized_line_numbers: input.opt.anonymized_line_numbers,
84 margin: None,
85 };
86 assert_eq!(dl::DisplayList::from(input), output);
87 }
88
89 #[test]
test_format_slices_continuation()90 fn test_format_slices_continuation() {
91 let src_0 = "This is slice 1";
92 let src_0_len = src_0.len();
93 let src_1 = "This is slice 2";
94 let src_1_len = src_1.len();
95 let input = snippet::Snippet {
96 title: None,
97 footer: vec![],
98 slices: vec![
99 snippet::Slice {
100 source: src_0,
101 line_start: 5402,
102 origin: Some("file1.rs"),
103 annotations: vec![],
104 fold: false,
105 },
106 snippet::Slice {
107 source: src_1,
108 line_start: 2,
109 origin: Some("file2.rs"),
110 annotations: vec![],
111 fold: false,
112 },
113 ],
114 opt: Default::default(),
115 };
116 let output = dl::DisplayList {
117 body: vec![
118 dl::DisplayLine::Raw(dl::DisplayRawLine::Origin {
119 path: "file1.rs",
120 pos: None,
121 header_type: dl::DisplayHeaderType::Initial,
122 }),
123 dl::DisplayLine::Source {
124 lineno: None,
125 inline_marks: vec![],
126 line: dl::DisplaySourceLine::Empty,
127 },
128 dl::DisplayLine::Source {
129 lineno: Some(5402),
130 inline_marks: vec![],
131 line: dl::DisplaySourceLine::Content {
132 text: src_0,
133 range: (0, src_0_len),
134 },
135 },
136 dl::DisplayLine::Source {
137 lineno: None,
138 inline_marks: vec![],
139 line: dl::DisplaySourceLine::Empty,
140 },
141 dl::DisplayLine::Raw(dl::DisplayRawLine::Origin {
142 path: "file2.rs",
143 pos: None,
144 header_type: dl::DisplayHeaderType::Continuation,
145 }),
146 dl::DisplayLine::Source {
147 lineno: None,
148 inline_marks: vec![],
149 line: dl::DisplaySourceLine::Empty,
150 },
151 dl::DisplayLine::Source {
152 lineno: Some(2),
153 inline_marks: vec![],
154 line: dl::DisplaySourceLine::Content {
155 text: src_1,
156 range: (0, src_1_len),
157 },
158 },
159 dl::DisplayLine::Source {
160 lineno: None,
161 inline_marks: vec![],
162 line: dl::DisplaySourceLine::Empty,
163 },
164 ],
165 stylesheet: get_term_style(input.opt.color),
166 anonymized_line_numbers: input.opt.anonymized_line_numbers,
167 margin: None,
168 };
169 assert_eq!(dl::DisplayList::from(input), output);
170 }
171
172 #[test]
test_format_slice_annotation_standalone()173 fn test_format_slice_annotation_standalone() {
174 let line_1 = "This is line 1";
175 let line_2 = "This is line 2";
176 let source = vec![line_1, line_2].join("\n");
177 // In line 2
178 let range = (22, 24);
179 let input = snippet::Snippet {
180 title: None,
181 footer: vec![],
182 slices: vec![snippet::Slice {
183 source: &source,
184 line_start: 5402,
185 origin: None,
186 annotations: vec![snippet::SourceAnnotation {
187 range,
188 label: "Test annotation",
189 annotation_type: snippet::AnnotationType::Info,
190 }],
191 fold: false,
192 }],
193 opt: Default::default(),
194 };
195 let output = dl::DisplayList {
196 body: vec![
197 dl::DisplayLine::Source {
198 lineno: None,
199 inline_marks: vec![],
200 line: dl::DisplaySourceLine::Empty,
201 },
202 dl::DisplayLine::Source {
203 lineno: Some(5402),
204 inline_marks: vec![],
205 line: dl::DisplaySourceLine::Content {
206 range: (0, line_1.len()),
207 text: line_1,
208 },
209 },
210 dl::DisplayLine::Source {
211 lineno: Some(5403),
212 inline_marks: vec![],
213 line: dl::DisplaySourceLine::Content {
214 range: (line_1.len() + 1, source.len()),
215 text: line_2,
216 },
217 },
218 dl::DisplayLine::Source {
219 lineno: None,
220 inline_marks: vec![],
221 line: dl::DisplaySourceLine::Annotation {
222 annotation: dl::Annotation {
223 annotation_type: dl::DisplayAnnotationType::Info,
224 id: None,
225 label: vec![dl::DisplayTextFragment {
226 content: "Test annotation",
227 style: dl::DisplayTextStyle::Regular,
228 }],
229 },
230 range: (range.0 - (line_1.len() + 1), range.1 - (line_1.len() + 1)),
231 annotation_type: dl::DisplayAnnotationType::Info,
232 annotation_part: dl::DisplayAnnotationPart::Standalone,
233 },
234 },
235 dl::DisplayLine::Source {
236 lineno: None,
237 inline_marks: vec![],
238 line: dl::DisplaySourceLine::Empty,
239 },
240 ],
241 stylesheet: get_term_style(input.opt.color),
242 anonymized_line_numbers: input.opt.anonymized_line_numbers,
243 margin: None,
244 };
245 assert_eq!(dl::DisplayList::from(input), output);
246 }
247
248 #[test]
test_format_label()249 fn test_format_label() {
250 let input = snippet::Snippet {
251 title: None,
252 footer: vec![snippet::Annotation {
253 id: None,
254 label: Some("This __is__ a title"),
255 annotation_type: snippet::AnnotationType::Error,
256 }],
257 slices: vec![],
258 opt: Default::default(),
259 };
260 let output = dl::DisplayList {
261 body: vec![dl::DisplayLine::Raw(dl::DisplayRawLine::Annotation {
262 annotation: dl::Annotation {
263 annotation_type: dl::DisplayAnnotationType::Error,
264 id: None,
265 label: vec![dl::DisplayTextFragment {
266 content: "This __is__ a title",
267 style: dl::DisplayTextStyle::Regular,
268 }],
269 },
270 source_aligned: true,
271 continuation: false,
272 })],
273 stylesheet: get_term_style(input.opt.color),
274 anonymized_line_numbers: input.opt.anonymized_line_numbers,
275 margin: None,
276 };
277 assert_eq!(dl::DisplayList::from(input), output);
278 }
279
280 #[test]
281 #[should_panic]
test_i26()282 fn test_i26() {
283 let source = "short";
284 let label = "label";
285 let input = snippet::Snippet {
286 title: None,
287 footer: vec![],
288 slices: vec![snippet::Slice {
289 annotations: vec![snippet::SourceAnnotation {
290 range: (0, source.len() + 1),
291 label,
292 annotation_type: snippet::AnnotationType::Error,
293 }],
294 source,
295 line_start: 0,
296 origin: None,
297 fold: false,
298 }],
299 opt: Default::default(),
300 };
301
302 let _ = dl::DisplayList::from(input);
303 }
304
305 #[test]
test_i_29()306 fn test_i_29() {
307 let snippets = snippet::Snippet {
308 title: Some(snippet::Annotation {
309 id: None,
310 label: Some("oops"),
311 annotation_type: snippet::AnnotationType::Error,
312 }),
313 footer: vec![],
314 slices: vec![snippet::Slice {
315 source: "First line\r\nSecond oops line",
316 line_start: 1,
317 origin: Some("<current file>"),
318 annotations: vec![snippet::SourceAnnotation {
319 range: (19, 23),
320 label: "oops",
321 annotation_type: snippet::AnnotationType::Error,
322 }],
323 fold: true,
324 }],
325 opt: Default::default(),
326 };
327
328 let expected = DisplayList {
329 body: vec![
330 dl::DisplayLine::Raw(dl::DisplayRawLine::Annotation {
331 annotation: dl::Annotation {
332 annotation_type: dl::DisplayAnnotationType::Error,
333 id: None,
334 label: vec![dl::DisplayTextFragment {
335 content: "oops",
336 style: dl::DisplayTextStyle::Emphasis,
337 }],
338 },
339 source_aligned: false,
340 continuation: false,
341 }),
342 dl::DisplayLine::Raw(dl::DisplayRawLine::Origin {
343 path: "<current file>",
344 pos: Some((2, 8)),
345 header_type: dl::DisplayHeaderType::Initial,
346 }),
347 dl::DisplayLine::Source {
348 lineno: None,
349 inline_marks: vec![],
350 line: dl::DisplaySourceLine::Empty,
351 },
352 dl::DisplayLine::Source {
353 lineno: Some(1),
354 inline_marks: vec![],
355 line: dl::DisplaySourceLine::Content {
356 text: "First line",
357 range: (0, 10),
358 },
359 },
360 dl::DisplayLine::Source {
361 lineno: Some(2),
362 inline_marks: vec![],
363 line: dl::DisplaySourceLine::Content {
364 text: "Second oops line",
365 range: (12, 28),
366 },
367 },
368 dl::DisplayLine::Source {
369 lineno: None,
370 inline_marks: vec![],
371 line: dl::DisplaySourceLine::Annotation {
372 annotation: dl::Annotation {
373 annotation_type: dl::DisplayAnnotationType::None,
374 id: None,
375 label: vec![dl::DisplayTextFragment {
376 content: "oops",
377 style: dl::DisplayTextStyle::Regular,
378 }],
379 },
380 range: (7, 11),
381 annotation_type: dl::DisplayAnnotationType::Error,
382 annotation_part: dl::DisplayAnnotationPart::Standalone,
383 },
384 },
385 dl::DisplayLine::Source {
386 lineno: None,
387 inline_marks: vec![],
388 line: dl::DisplaySourceLine::Empty,
389 },
390 ],
391 stylesheet: get_term_style(false),
392 anonymized_line_numbers: false,
393 margin: None,
394 };
395
396 assert_eq!(DisplayList::from(snippets), expected);
397 }
398