]>
Commit | Line | Data |
---|---|---|
f035d41b XL |
1 | use annotate_snippets::display_list::DisplayList; |
2 | use annotate_snippets::{display_list as dl, formatter::get_term_style, snippet}; | |
dc9dc135 XL |
3 | |
4 | #[test] | |
5 | fn test_format_title() { | |
6 | let input = snippet::Snippet { | |
7 | title: Some(snippet::Annotation { | |
f035d41b XL |
8 | id: Some("E0001"), |
9 | label: Some("This is a title"), | |
dc9dc135 XL |
10 | annotation_type: snippet::AnnotationType::Error, |
11 | }), | |
12 | footer: vec![], | |
13 | slices: vec![], | |
f035d41b | 14 | opt: Default::default(), |
dc9dc135 XL |
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, | |
f035d41b | 20 | id: Some("E0001"), |
dc9dc135 | 21 | label: vec![dl::DisplayTextFragment { |
f035d41b | 22 | content: "This is a title", |
dc9dc135 XL |
23 | style: dl::DisplayTextStyle::Emphasis, |
24 | }], | |
25 | }, | |
26 | source_aligned: false, | |
27 | continuation: false, | |
28 | })], | |
f035d41b XL |
29 | stylesheet: get_term_style(input.opt.color), |
30 | anonymized_line_numbers: input.opt.anonymized_line_numbers, | |
923072b8 | 31 | margin: None, |
dc9dc135 XL |
32 | }; |
33 | assert_eq!(dl::DisplayList::from(input), output); | |
34 | } | |
35 | ||
36 | #[test] | |
37 | fn test_format_slice() { | |
f035d41b XL |
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"); | |
dc9dc135 XL |
41 | let input = snippet::Snippet { |
42 | title: None, | |
43 | footer: vec![], | |
44 | slices: vec![snippet::Slice { | |
f035d41b | 45 | source: &source, |
dc9dc135 XL |
46 | line_start: 5402, |
47 | origin: None, | |
48 | annotations: vec![], | |
49 | fold: false, | |
50 | }], | |
f035d41b | 51 | opt: Default::default(), |
dc9dc135 XL |
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 { | |
f035d41b XL |
64 | text: line_1, |
65 | range: (0, line_1.len()), | |
dc9dc135 XL |
66 | }, |
67 | }, | |
68 | dl::DisplayLine::Source { | |
69 | lineno: Some(5403), | |
70 | inline_marks: vec![], | |
71 | line: dl::DisplaySourceLine::Content { | |
f035d41b XL |
72 | range: (line_1.len() + 1, source.len()), |
73 | text: line_2, | |
dc9dc135 XL |
74 | }, |
75 | }, | |
76 | dl::DisplayLine::Source { | |
77 | lineno: None, | |
78 | inline_marks: vec![], | |
79 | line: dl::DisplaySourceLine::Empty, | |
80 | }, | |
81 | ], | |
f035d41b XL |
82 | stylesheet: get_term_style(input.opt.color), |
83 | anonymized_line_numbers: input.opt.anonymized_line_numbers, | |
923072b8 | 84 | margin: None, |
dc9dc135 XL |
85 | }; |
86 | assert_eq!(dl::DisplayList::from(input), output); | |
87 | } | |
88 | ||
89 | #[test] | |
90 | fn test_format_slices_continuation() { | |
f035d41b XL |
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(); | |
dc9dc135 XL |
95 | let input = snippet::Snippet { |
96 | title: None, | |
97 | footer: vec![], | |
98 | slices: vec![ | |
99 | snippet::Slice { | |
f035d41b | 100 | source: src_0, |
dc9dc135 | 101 | line_start: 5402, |
f035d41b | 102 | origin: Some("file1.rs"), |
dc9dc135 XL |
103 | annotations: vec![], |
104 | fold: false, | |
105 | }, | |
106 | snippet::Slice { | |
f035d41b | 107 | source: src_1, |
dc9dc135 | 108 | line_start: 2, |
f035d41b | 109 | origin: Some("file2.rs"), |
dc9dc135 XL |
110 | annotations: vec![], |
111 | fold: false, | |
112 | }, | |
113 | ], | |
f035d41b | 114 | opt: Default::default(), |
dc9dc135 XL |
115 | }; |
116 | let output = dl::DisplayList { | |
117 | body: vec![ | |
118 | dl::DisplayLine::Raw(dl::DisplayRawLine::Origin { | |
f035d41b | 119 | path: "file1.rs", |
dc9dc135 XL |
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 { | |
f035d41b XL |
132 | text: src_0, |
133 | range: (0, src_0_len), | |
dc9dc135 XL |
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 { | |
f035d41b | 142 | path: "file2.rs", |
dc9dc135 XL |
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 { | |
f035d41b XL |
155 | text: src_1, |
156 | range: (0, src_1_len), | |
dc9dc135 XL |
157 | }, |
158 | }, | |
159 | dl::DisplayLine::Source { | |
160 | lineno: None, | |
161 | inline_marks: vec![], | |
162 | line: dl::DisplaySourceLine::Empty, | |
163 | }, | |
164 | ], | |
f035d41b XL |
165 | stylesheet: get_term_style(input.opt.color), |
166 | anonymized_line_numbers: input.opt.anonymized_line_numbers, | |
923072b8 | 167 | margin: None, |
dc9dc135 XL |
168 | }; |
169 | assert_eq!(dl::DisplayList::from(input), output); | |
170 | } | |
171 | ||
172 | #[test] | |
173 | fn test_format_slice_annotation_standalone() { | |
f035d41b XL |
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); | |
dc9dc135 XL |
179 | let input = snippet::Snippet { |
180 | title: None, | |
181 | footer: vec![], | |
182 | slices: vec![snippet::Slice { | |
f035d41b | 183 | source: &source, |
dc9dc135 XL |
184 | line_start: 5402, |
185 | origin: None, | |
186 | annotations: vec![snippet::SourceAnnotation { | |
f035d41b XL |
187 | range, |
188 | label: "Test annotation", | |
dc9dc135 XL |
189 | annotation_type: snippet::AnnotationType::Info, |
190 | }], | |
191 | fold: false, | |
192 | }], | |
f035d41b | 193 | opt: Default::default(), |
dc9dc135 XL |
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 { | |
f035d41b XL |
206 | range: (0, line_1.len()), |
207 | text: line_1, | |
dc9dc135 XL |
208 | }, |
209 | }, | |
210 | dl::DisplayLine::Source { | |
211 | lineno: Some(5403), | |
212 | inline_marks: vec![], | |
213 | line: dl::DisplaySourceLine::Content { | |
f035d41b XL |
214 | range: (line_1.len() + 1, source.len()), |
215 | text: line_2, | |
dc9dc135 XL |
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 { | |
f035d41b | 226 | content: "Test annotation", |
dc9dc135 XL |
227 | style: dl::DisplayTextStyle::Regular, |
228 | }], | |
229 | }, | |
f035d41b | 230 | range: (range.0 - (line_1.len() + 1), range.1 - (line_1.len() + 1)), |
dc9dc135 XL |
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 | ], | |
f035d41b XL |
241 | stylesheet: get_term_style(input.opt.color), |
242 | anonymized_line_numbers: input.opt.anonymized_line_numbers, | |
923072b8 | 243 | margin: None, |
dc9dc135 XL |
244 | }; |
245 | assert_eq!(dl::DisplayList::from(input), output); | |
246 | } | |
247 | ||
248 | #[test] | |
249 | fn test_format_label() { | |
250 | let input = snippet::Snippet { | |
251 | title: None, | |
252 | footer: vec![snippet::Annotation { | |
253 | id: None, | |
f035d41b | 254 | label: Some("This __is__ a title"), |
dc9dc135 XL |
255 | annotation_type: snippet::AnnotationType::Error, |
256 | }], | |
257 | slices: vec![], | |
f035d41b | 258 | opt: Default::default(), |
dc9dc135 XL |
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![ | |
266 | dl::DisplayTextFragment { | |
f035d41b | 267 | content: "This ", |
dc9dc135 XL |
268 | style: dl::DisplayTextStyle::Regular, |
269 | }, | |
270 | dl::DisplayTextFragment { | |
f035d41b | 271 | content: "is", |
dc9dc135 XL |
272 | style: dl::DisplayTextStyle::Emphasis, |
273 | }, | |
274 | dl::DisplayTextFragment { | |
f035d41b | 275 | content: " a title", |
dc9dc135 XL |
276 | style: dl::DisplayTextStyle::Regular, |
277 | }, | |
278 | ], | |
279 | }, | |
280 | source_aligned: true, | |
281 | continuation: false, | |
282 | })], | |
f035d41b XL |
283 | stylesheet: get_term_style(input.opt.color), |
284 | anonymized_line_numbers: input.opt.anonymized_line_numbers, | |
923072b8 | 285 | margin: None, |
dc9dc135 XL |
286 | }; |
287 | assert_eq!(dl::DisplayList::from(input), output); | |
288 | } | |
f035d41b XL |
289 | |
290 | #[test] | |
291 | #[should_panic] | |
292 | fn test_i26() { | |
293 | let source = "short"; | |
294 | let label = "label"; | |
295 | let input = snippet::Snippet { | |
296 | title: None, | |
297 | footer: vec![], | |
298 | slices: vec![snippet::Slice { | |
299 | annotations: vec![snippet::SourceAnnotation { | |
300 | range: (0, source.len() + 1), | |
301 | label, | |
302 | annotation_type: snippet::AnnotationType::Error, | |
303 | }], | |
304 | source, | |
305 | line_start: 0, | |
306 | origin: None, | |
307 | fold: false, | |
308 | }], | |
309 | opt: Default::default(), | |
310 | }; | |
311 | ||
312 | let _ = dl::DisplayList::from(input); | |
313 | } | |
314 | ||
315 | #[test] | |
316 | fn test_i_29() { | |
317 | let snippets = snippet::Snippet { | |
318 | title: Some(snippet::Annotation { | |
319 | id: None, | |
320 | label: Some("oops"), | |
321 | annotation_type: snippet::AnnotationType::Error, | |
322 | }), | |
323 | footer: vec![], | |
324 | slices: vec![snippet::Slice { | |
325 | source: "First line\r\nSecond oops line", | |
326 | line_start: 1, | |
327 | origin: Some("<current file>"), | |
328 | annotations: vec![snippet::SourceAnnotation { | |
329 | range: (19, 23), | |
330 | label: "oops", | |
331 | annotation_type: snippet::AnnotationType::Error, | |
332 | }], | |
333 | fold: true, | |
334 | }], | |
335 | opt: Default::default(), | |
336 | }; | |
337 | ||
338 | let expected = DisplayList { | |
339 | body: vec![ | |
340 | dl::DisplayLine::Raw(dl::DisplayRawLine::Annotation { | |
341 | annotation: dl::Annotation { | |
342 | annotation_type: dl::DisplayAnnotationType::Error, | |
343 | id: None, | |
344 | label: vec![dl::DisplayTextFragment { | |
345 | content: "oops", | |
346 | style: dl::DisplayTextStyle::Emphasis, | |
347 | }], | |
348 | }, | |
349 | source_aligned: false, | |
350 | continuation: false, | |
351 | }), | |
352 | dl::DisplayLine::Raw(dl::DisplayRawLine::Origin { | |
353 | path: "<current file>", | |
354 | pos: Some((2, 8)), | |
355 | header_type: dl::DisplayHeaderType::Initial, | |
356 | }), | |
357 | dl::DisplayLine::Source { | |
358 | lineno: None, | |
359 | inline_marks: vec![], | |
360 | line: dl::DisplaySourceLine::Empty, | |
361 | }, | |
362 | dl::DisplayLine::Source { | |
363 | lineno: Some(1), | |
364 | inline_marks: vec![], | |
365 | line: dl::DisplaySourceLine::Content { | |
366 | text: "First line", | |
367 | range: (0, 10), | |
368 | }, | |
369 | }, | |
370 | dl::DisplayLine::Source { | |
371 | lineno: Some(2), | |
372 | inline_marks: vec![], | |
373 | line: dl::DisplaySourceLine::Content { | |
374 | text: "Second oops line", | |
375 | range: (12, 28), | |
376 | }, | |
377 | }, | |
378 | dl::DisplayLine::Source { | |
379 | lineno: None, | |
380 | inline_marks: vec![], | |
381 | line: dl::DisplaySourceLine::Annotation { | |
382 | annotation: dl::Annotation { | |
383 | annotation_type: dl::DisplayAnnotationType::None, | |
384 | id: None, | |
385 | label: vec![dl::DisplayTextFragment { | |
386 | content: "oops", | |
387 | style: dl::DisplayTextStyle::Regular, | |
388 | }], | |
389 | }, | |
390 | range: (7, 11), | |
391 | annotation_type: dl::DisplayAnnotationType::Error, | |
392 | annotation_part: dl::DisplayAnnotationPart::Standalone, | |
393 | }, | |
394 | }, | |
395 | dl::DisplayLine::Source { | |
396 | lineno: None, | |
397 | inline_marks: vec![], | |
398 | line: dl::DisplaySourceLine::Empty, | |
399 | }, | |
400 | ], | |
401 | stylesheet: get_term_style(false), | |
402 | anonymized_line_numbers: false, | |
923072b8 | 403 | margin: None, |
f035d41b XL |
404 | }; |
405 | ||
406 | assert_eq!(DisplayList::from(snippets), expected); | |
407 | } |