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 a test for source highlighting
  • Loading branch information
Muscraft committed Apr 16, 2025
commit 57340c8c00e0b238df11f27fe4f116c561f9be8f
34 changes: 34 additions & 0 deletions examples/highlight_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};

fn main() {
let source = r#"//@ compile-flags: -Z teach

#![allow(warnings)]

const CON: Vec<i32> = vec![1, 2, 3]; //~ ERROR E0010
//~| ERROR cannot call non-const method
fn main() {}
"#;
let message = Level::Error
.message("allocations are not allowed in constants")
.id("E0010")
.group(
Group::new()
.element(
Snippet::source(source)
.fold(true)
.origin("$DIR/E0010-teach.rs")
.annotation(
AnnotationKind::Primary
.span(72..85)
.label("allocation not allowed in constants")
),
)
.element(
Level::Note.title("The runtime heap is not yet available at compile-time, so no runtime heap allocations can be created."),
),
);

let renderer = Renderer::styled().anonymized_line_numbers(true);
anstream::println!("{}", renderer.render(message));
}
38 changes: 38 additions & 0 deletions examples/highlight_source.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ fn format() {
assert_example(target, expected);
}

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

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