Skip to content

Commit 22c225f

Browse files
committed
test: Add tests for custom Levels
1 parent b61153d commit 22c225f

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed

examples/custom_error.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use annotate_snippets::renderer::OutputTheme;
2+
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
3+
4+
fn main() {
5+
let source = r#"//@ compile-flags: -Ztreat-err-as-bug
6+
//@ failure-status: 101
7+
//@ error-pattern: aborting due to `-Z treat-err-as-bug=1`
8+
//@ error-pattern: [eval_static_initializer] evaluating initializer of static `C`
9+
//@ normalize-stderr: "note: .*\n\n" -> ""
10+
//@ normalize-stderr: "thread 'rustc' panicked.*:\n.*\n" -> ""
11+
//@ rustc-env:RUST_BACKTRACE=0
12+
13+
#![crate_type = "rlib"]
14+
15+
pub static C: u32 = 0 - 1;
16+
//~^ ERROR could not evaluate static initializer
17+
"#;
18+
let message = Level::None
19+
.message("error: internal compiler error[E0080]: could not evaluate static initializer")
20+
.group(
21+
Group::new().element(
22+
Snippet::source(source)
23+
.origin("$DIR/err.rs")
24+
.fold(true)
25+
.annotation(
26+
AnnotationKind::Primary
27+
.span(386..391)
28+
.label("attempt to compute `0_u32 - 1_u32`, which would overflow"),
29+
),
30+
),
31+
);
32+
33+
let renderer = Renderer::styled().theme(OutputTheme::Unicode);
34+
anstream::println!("{}", renderer.render(message));
35+
}

examples/custom_error.svg

+35
Loading

examples/custom_level.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use annotate_snippets::renderer::OutputTheme;
2+
use annotate_snippets::{AnnotationKind, Group, Level, Patch, Renderer, Snippet};
3+
4+
fn main() {
5+
let source = r#"// Regression test for issue #114529
6+
// Tests that we do not ICE during const eval for a
7+
// break-with-value in contexts where it is illegal
8+
9+
#[allow(while_true)]
10+
fn main() {
11+
[(); {
12+
while true {
13+
break 9; //~ ERROR `break` with value from a `while` loop
14+
};
15+
51
16+
}];
17+
18+
[(); {
19+
while let Some(v) = Some(9) {
20+
break v; //~ ERROR `break` with value from a `while` loop
21+
};
22+
51
23+
}];
24+
25+
while true {
26+
break (|| { //~ ERROR `break` with value from a `while` loop
27+
let local = 9;
28+
});
29+
}
30+
}
31+
"#;
32+
let message = Level::Error
33+
.message("`break` with value from a `while` loop")
34+
.id("E0571")
35+
.group(
36+
Group::new().element(
37+
Snippet::source(source)
38+
.line_start(1)
39+
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
40+
.fold(true)
41+
.annotation(
42+
AnnotationKind::Primary
43+
.span(483..581)
44+
.label("can only break with a value inside `loop` or breakable block"),
45+
)
46+
.annotation(
47+
AnnotationKind::Context
48+
.span(462..472)
49+
.label("you can't `break` with a value in a `while` loop"),
50+
),
51+
),
52+
)
53+
.group(
54+
Group::new()
55+
.element(Level::None.title(
56+
"suggestion: use `break` on its own without a value inside this `while` loop",
57+
))
58+
.element(
59+
Snippet::source(source)
60+
.line_start(1)
61+
.origin("$DIR/issue-114529-illegal-break-with-value.rs")
62+
.fold(true)
63+
.patch(Patch::new(483..581, "break")),
64+
),
65+
);
66+
67+
let renderer = Renderer::styled().theme(OutputTheme::Unicode);
68+
anstream::println!("{}", renderer.render(message));
69+
}

examples/custom_level.svg

+61
Loading

tests/examples.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
#[test]
2+
fn custom_error() {
3+
let target = "custom_error";
4+
let expected = snapbox::file!["../examples/custom_error.svg": TermSvg];
5+
assert_example(target, expected);
6+
}
7+
8+
#[test]
9+
fn custom_level() {
10+
let target = "custom_level";
11+
let expected = snapbox::file!["../examples/custom_level.svg": TermSvg];
12+
assert_example(target, expected);
13+
}
14+
115
#[test]
216
fn expected_type() {
317
let target = "expected_type";

0 commit comments

Comments
 (0)