Skip to content

Commit 9288922

Browse files
committed
test: Add a test for keeping lines with no annotations
1 parent 5cd6f5d commit 9288922

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

examples/struct_name_as_context.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
2+
fn main() {
3+
let source = r#"struct S {
4+
field1: usize,
5+
field2: usize,
6+
field3: usize,
7+
field4: usize,
8+
fn foo() {},
9+
field6: usize,
10+
}
11+
"#;
12+
let message =
13+
&[
14+
Group::with_title(
15+
Level::ERROR.title("functions are not allowed in struct definitions"),
16+
)
17+
.element(
18+
Snippet::source(source)
19+
.path("$DIR/struct_name_as_context.rs")
20+
.annotation(AnnotationKind::Primary.span(91..102)),
21+
)
22+
.element(
23+
Level::HELP.message(
24+
"unlike in C++, Java, and C#, functions are declared in `impl` blocks",
25+
),
26+
),
27+
];
28+
29+
let renderer = Renderer::styled();
30+
anstream::println!("{}", renderer.render(message));
31+
}

examples/struct_name_as_context.svg

Lines changed: 40 additions & 0 deletions
Loading

tests/examples.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ fn multislice() {
7070
assert_example(target, expected);
7171
}
7272

73+
#[test]
74+
fn struct_name_as_context() {
75+
let target = "struct_name_as_context";
76+
let expected = snapbox::file!["../examples/struct_name_as_context.svg": TermSvg];
77+
assert_example(target, expected);
78+
}
79+
7380
#[track_caller]
7481
fn assert_example(target: &str, expected: snapbox::Data) {
7582
let bin_path = snapbox::cmd::compile_example(target, ["--features=testing-colors"]).unwrap();

tests/formatter.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,3 +3005,67 @@ LL | .sum::<_>() //~ ERROR type annotations needed
30053005
let renderer = Renderer::plain().anonymized_line_numbers(true);
30063006
assert_data_eq!(renderer.render(input), expected);
30073007
}
3008+
3009+
#[test]
3010+
fn keep_lines1() {
3011+
let source = r#"
3012+
cargo
3013+
fuzzy
3014+
pizza
3015+
jumps
3016+
crazy
3017+
quack
3018+
zappy
3019+
"#;
3020+
3021+
let input_new = &[Group::with_title(
3022+
Level::ERROR
3023+
.title("the size for values of type `T` cannot be known at compilation time")
3024+
.id("E0277"),
3025+
)
3026+
.element(
3027+
Snippet::source(source)
3028+
.line_start(11)
3029+
.annotation(AnnotationKind::Primary.span(1..6)),
3030+
)];
3031+
let expected = str![[r#"
3032+
error[E0277]: the size for values of type `T` cannot be known at compilation time
3033+
|
3034+
12 | cargo
3035+
| ^^^^^
3036+
"#]];
3037+
let renderer = Renderer::plain();
3038+
assert_data_eq!(renderer.render(input_new), expected);
3039+
}
3040+
3041+
#[test]
3042+
fn keep_lines2() {
3043+
let source = r#"
3044+
cargo
3045+
fuzzy
3046+
pizza
3047+
jumps
3048+
crazy
3049+
quack
3050+
zappy
3051+
"#;
3052+
3053+
let input_new = &[Group::with_title(
3054+
Level::ERROR
3055+
.title("the size for values of type `T` cannot be known at compilation time")
3056+
.id("E0277"),
3057+
)
3058+
.element(
3059+
Snippet::source(source)
3060+
.line_start(11)
3061+
.annotation(AnnotationKind::Primary.span(1..6)),
3062+
)];
3063+
let expected = str![[r#"
3064+
error[E0277]: the size for values of type `T` cannot be known at compilation time
3065+
|
3066+
12 | cargo
3067+
| ^^^^^
3068+
"#]];
3069+
let renderer = Renderer::plain();
3070+
assert_data_eq!(renderer.render(input_new), expected);
3071+
}

0 commit comments

Comments
 (0)