|
| 1 | +use annotate_snippets::{ |
| 2 | + renderer::DecorStyle, AnnotationKind, Level, Origin, Padding, Patch, Renderer, Snippet, |
| 3 | +}; |
| 4 | + |
| 5 | +use snapbox::{assert_data_eq, file}; |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn case() { |
| 9 | + // https://github.com/rust-lang/rust/blob/4b94758d2ba7d0ef71ccf5fde29ce4bc5d6fe2a4/tests/ui/suggestions/multi-suggestion.rs |
| 10 | + |
| 11 | + let source = r#"#![allow(dead_code)] |
| 12 | +struct U <T> { |
| 13 | + wtf: Option<Box<U<T>>>, |
| 14 | + x: T, |
| 15 | +} |
| 16 | +fn main() { |
| 17 | + U { |
| 18 | + wtf: Some(Box(U { |
| 19 | + wtf: None, |
| 20 | + x: (), |
| 21 | + })), |
| 22 | + x: () |
| 23 | + }; |
| 24 | + let _ = std::collections::HashMap(); |
| 25 | + let _ = std::collections::HashMap {}; |
| 26 | + let _ = Box {}; |
| 27 | +} |
| 28 | +"#; |
| 29 | + let path = "$DIR/suggest-box-new.rs"; |
| 30 | + let secondary_path = "$SRC_DIR/alloc/src/boxed.rs"; |
| 31 | + |
| 32 | + let report = &[ |
| 33 | + Level::ERROR |
| 34 | + .primary_title("cannot initialize a tuple struct which contains private fields") |
| 35 | + .id("E0423") |
| 36 | + .element( |
| 37 | + Snippet::source(source) |
| 38 | + .path(path) |
| 39 | + .annotation(AnnotationKind::Primary.span(114..117)), |
| 40 | + ), |
| 41 | + Level::NOTE |
| 42 | + .secondary_title("constructor is not visible here due to private fields") |
| 43 | + .element(Origin::path(secondary_path).line(234).char_column(2)) |
| 44 | + .element(Padding) |
| 45 | + .element(Level::NOTE.message("private field")) |
| 46 | + .element(Padding) |
| 47 | + .element(Level::NOTE.message("private field")), |
| 48 | + Level::HELP |
| 49 | + .secondary_title( |
| 50 | + "you might have meant to use an associated function to build this type", |
| 51 | + ) |
| 52 | + .element( |
| 53 | + Snippet::source(source) |
| 54 | + .path(path) |
| 55 | + .patch(Patch::new(117..174, "::new(_)")), |
| 56 | + ) |
| 57 | + .element( |
| 58 | + Snippet::source(source) |
| 59 | + .path(path) |
| 60 | + .patch(Patch::new(117..174, "::new_uninit()")), |
| 61 | + ) |
| 62 | + .element( |
| 63 | + Snippet::source(source) |
| 64 | + .path(path) |
| 65 | + .patch(Patch::new(117..174, "::new_zeroed()")), |
| 66 | + ) |
| 67 | + .element( |
| 68 | + Snippet::source(source) |
| 69 | + .path(path) |
| 70 | + .patch(Patch::new(117..174, "::new_in(_, _)")), |
| 71 | + ) |
| 72 | + .element(Level::NOTE.no_name().message("and 12 other candidates")), |
| 73 | + Level::HELP |
| 74 | + .secondary_title("consider using the `Default` trait") |
| 75 | + .element( |
| 76 | + Snippet::source(source) |
| 77 | + .path(path) |
| 78 | + .patch(Patch::new(114..114, "<")) |
| 79 | + .patch(Patch::new( |
| 80 | + 117..174, |
| 81 | + " as std::default::Default>::default()", |
| 82 | + )), |
| 83 | + ), |
| 84 | + ]; |
| 85 | + |
| 86 | + let expected_ascii = file!["multiple_multiline_removal.ascii.term.svg": TermSvg]; |
| 87 | + let renderer = Renderer::styled(); |
| 88 | + assert_data_eq!(renderer.render(report), expected_ascii); |
| 89 | + |
| 90 | + let expected_unicode = file!["multiple_multiline_removal.unicode.term.svg": TermSvg]; |
| 91 | + let renderer = renderer.decor_style(DecorStyle::Unicode); |
| 92 | + assert_data_eq!(renderer.render(report), expected_unicode); |
| 93 | +} |
0 commit comments