Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 21 additions & 4 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3009,9 +3009,7 @@ fn max_line_number(groups: &[Group<'_>]) -> usize {
fn newline_count(body: &str) -> usize {
#[cfg(feature = "simd")]
{
memchr::memchr_iter(b'\n', body.as_bytes())
.count()
.saturating_sub(1)
memchr::memchr_iter(b'\n', body.as_bytes()).count()
}
#[cfg(not(feature = "simd"))]
{
Expand All @@ -3021,7 +3019,7 @@ fn newline_count(body: &str) -> usize {

#[cfg(test)]
mod test {
use super::OUTPUT_REPLACEMENTS;
use super::{newline_count, OUTPUT_REPLACEMENTS};
use snapbox::IntoData;

fn format_replacements(replacements: Vec<(char, &str)>) -> String {
Expand All @@ -3043,4 +3041,23 @@ mod test {
let actual = format_replacements(OUTPUT_REPLACEMENTS.to_owned());
snapbox::assert_data_eq!(actual, expected.into_data().raw());
}

#[test]
fn ensure_newline_count_correct() {
let source = r#"
cargo-features = ["path-bases"]

[package]
name = "foo"
version = "0.5.0"
authors = ["[email protected]"]

[dependencies]
bar = { base = '^^not-valid^^', path = 'bar' }
"#;
let actual_count = newline_count(source);
let expected_count = 10;

assert_eq!(expected_count, actual_count);
}
}
48 changes: 48 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3227,3 +3227,51 @@ LL │ t.field;
let renderer = renderer.decor_style(DecorStyle::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}

#[test]
fn multiple_line_num_widths() {
let source = r#"
cargo-features = ["path-bases"]

[package]
name = "foo"
version = "0.5.0"
authors = ["[email protected]"]

[dependencies]
bar = { base = '^^not-valid^^', path = 'bar' }
"#;

let title = "invalid character `^` in path base name: `^^not-valid^^`, the first character must be a Unicode XID start character (most letters or `_`)";

let input = &[
Group::with_title(Level::ERROR.primary_title(title)).element(
Snippet::source(source)
.path("Cargo.toml")
.annotation(AnnotationKind::Primary.span(243..282))
.annotation(AnnotationKind::Visible.span(206..219)),
),
];

let expected_ascii = str![[r#"
error: invalid character `^` in path base name: `^^not-valid^^`, the first character must be a Unicode XID start character (most letters or `_`)
--> Cargo.toml:10:24
|
9 | [dependencies]
10 | bar = { base = '^^not-valid^^', path = 'bar' }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input), expected_ascii);

let expected_unicode = str![[r#"
error: invalid character `^` in path base name: `^^not-valid^^`, the first character must be a Unicode XID start character (most letters or `_`)
╭▸ Cargo.toml:10:24
9 │ [dependencies]
10 │ bar = { base = '^^not-valid^^', path = 'bar' }
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
"#]];
let renderer = renderer.decor_style(DecorStyle::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}
Loading