Matthew Maurer | 597ccd5 | 2023-06-12 16:37:41 +0000 | [diff] [blame] | 1 | use annotate_snippets::display_list::DisplayList; |
| 2 | use annotate_snippets::{display_list as dl, formatter::get_term_style, snippet}; |
| 3 | |
| 4 | #[test] |
| 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] |
| 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] |
| 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] |
| 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] |
| 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![ |
| 266 | dl::DisplayTextFragment { |
| 267 | content: "This ", |
| 268 | style: dl::DisplayTextStyle::Regular, |
| 269 | }, |
| 270 | dl::DisplayTextFragment { |
| 271 | content: "is", |
| 272 | style: dl::DisplayTextStyle::Emphasis, |
| 273 | }, |
| 274 | dl::DisplayTextFragment { |
| 275 | content: " a title", |
| 276 | style: dl::DisplayTextStyle::Regular, |
| 277 | }, |
| 278 | ], |
| 279 | }, |
| 280 | source_aligned: true, |
| 281 | continuation: false, |
| 282 | })], |
| 283 | stylesheet: get_term_style(input.opt.color), |
| 284 | anonymized_line_numbers: input.opt.anonymized_line_numbers, |
| 285 | margin: None, |
| 286 | }; |
| 287 | assert_eq!(dl::DisplayList::from(input), output); |
| 288 | } |
| 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, |
| 403 | margin: None, |
| 404 | }; |
| 405 | |
| 406 | assert_eq!(DisplayList::from(snippets), expected); |
| 407 | } |