Skip to content

New api #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: Add tests for custom Levels
  • Loading branch information
Muscraft committed Apr 16, 2025
commit ab5ca1802efc7d34a861da156133ff0ba57f0878
35 changes: 35 additions & 0 deletions examples/custom_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use annotate_snippets::renderer::OutputTheme;
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};

fn main() {
let source = r#"//@ compile-flags: -Ztreat-err-as-bug
//@ failure-status: 101
//@ error-pattern: aborting due to `-Z treat-err-as-bug=1`
//@ error-pattern: [eval_static_initializer] evaluating initializer of static `C`
//@ normalize-stderr: "note: .*\n\n" -> ""
//@ normalize-stderr: "thread 'rustc' panicked.*:\n.*\n" -> ""
//@ rustc-env:RUST_BACKTRACE=0

#![crate_type = "rlib"]

pub static C: u32 = 0 - 1;
//~^ ERROR could not evaluate static initializer
"#;
let message = Level::None
.message("error: internal compiler error[E0080]: could not evaluate static initializer")
.group(
Group::new().element(
Snippet::source(source)
.origin("$DIR/err.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(386..391)
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
),
),
);

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
}
35 changes: 35 additions & 0 deletions examples/custom_error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions examples/custom_level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use annotate_snippets::renderer::OutputTheme;
use annotate_snippets::{AnnotationKind, Group, Level, Patch, Renderer, Snippet};

fn main() {
let source = r#"// Regression test for issue #114529
// Tests that we do not ICE during const eval for a
// break-with-value in contexts where it is illegal

#[allow(while_true)]
fn main() {
[(); {
while true {
break 9; //~ ERROR `break` with value from a `while` loop
};
51
}];

[(); {
while let Some(v) = Some(9) {
break v; //~ ERROR `break` with value from a `while` loop
};
51
}];

while true {
break (|| { //~ ERROR `break` with value from a `while` loop
let local = 9;
});
}
}
"#;
let message = Level::Error
.message("`break` with value from a `while` loop")
.id("E0571")
.group(
Group::new().element(
Snippet::source(source)
.line_start(1)
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(483..581)
.label("can only break with a value inside `loop` or breakable block"),
)
.annotation(
AnnotationKind::Context
.span(462..472)
.label("you can't `break` with a value in a `while` loop"),
),
),
)
.group(
Group::new()
.element(Level::None.title(
"suggestion: use `break` on its own without a value inside this `while` loop",
))
.element(
Snippet::source(source)
.line_start(1)
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
.fold(true)
.patch(Patch::new(483..581, "break")),
),
);

let renderer = Renderer::styled().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
}
61 changes: 61 additions & 0 deletions examples/custom_level.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
#[test]
fn custom_error() {
let target = "custom_error";
let expected = snapbox::file!["../examples/custom_error.svg": TermSvg];
assert_example(target, expected);
}

#[test]
fn custom_level() {
let target = "custom_level";
let expected = snapbox::file!["../examples/custom_level.svg": TermSvg];
assert_example(target, expected);
}

#[test]
fn expected_type() {
let target = "expected_type";
Expand Down