Skip to content

Commit 481920d

Browse files
committed
test: Add a test for passing precolored text
1 parent 222c013 commit 481920d

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

examples/highlight_title.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
2+
use anstyle::Effects;
3+
4+
fn main() {
5+
let source = r#"// Make sure "highlighted" code is colored purple
6+
7+
//@ compile-flags: --error-format=human --color=always
8+
//@ error-pattern:for<'a> 
9+
//@ edition:2018
10+
11+
use core::pin::Pin;
12+
use core::future::Future;
13+
use core::any::Any;
14+
15+
fn query(_: fn(Box<(dyn Any + Send + '_)>) -> Pin<Box<(
16+
dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static
17+
)>>) {}
18+
19+
fn wrapped_fn<'a>(_: Box<(dyn Any + Send)>) -> Pin<Box<(
20+
dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static
21+
)>> {
22+
Box::pin(async { Err("nope".into()) })
23+
}
24+
25+
fn main() {
26+
query(wrapped_fn);
27+
}
28+
"#;
29+
30+
let magenta = annotate_snippets::renderer::AnsiColor::Magenta
31+
.on_default()
32+
.effects(Effects::BOLD);
33+
let title = format!(
34+
"expected fn pointer `{}for<'a>{} fn(Box<{}(dyn Any + Send + 'a){}>) -> Pin<_>`
35+
found fn item `fn(Box<{}(dyn Any + Send + 'static){}>) -> Pin<_> {}{{wrapped_fn}}{}`",
36+
magenta.render(),
37+
magenta.render_reset(),
38+
magenta.render(),
39+
magenta.render_reset(),
40+
magenta.render(),
41+
magenta.render_reset(),
42+
magenta.render(),
43+
magenta.render_reset()
44+
);
45+
46+
let message = Level::Error.message("mismatched types").id("E0308").group(
47+
Group::new()
48+
.element(
49+
Snippet::source(source)
50+
.fold(true)
51+
.origin("$DIR/highlighting.rs")
52+
.annotation(
53+
AnnotationKind::Primary
54+
.span(589..599)
55+
.label("one type is more general than the other"),
56+
)
57+
.annotation(
58+
AnnotationKind::Context
59+
.span(583..588)
60+
.label("arguments to this function are incorrect"),
61+
),
62+
)
63+
.element(Level::Note.title(&title)),
64+
);
65+
66+
let renderer = Renderer::styled().anonymized_line_numbers(true);
67+
anstream::println!("{}", renderer.render(message));
68+
}

examples/highlight_title.svg

+44
Loading

tests/examples.rs

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ fn highlight_source() {
2626
assert_example(target, expected);
2727
}
2828

29+
#[test]
30+
fn highlight_title() {
31+
let target = "highlight_title";
32+
let expected = snapbox::file!["../examples/highlight_title.svg": TermSvg];
33+
assert_example(target, expected);
34+
}
35+
2936
#[test]
3037
fn multislice() {
3138
let target = "multislice";

0 commit comments

Comments
 (0)