diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 25e9508b..f776c232 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -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"))] { @@ -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 { @@ -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 = ["wycats@example.com"] + + [dependencies] + bar = { base = '^^not-valid^^', path = 'bar' } + "#; + let actual_count = newline_count(source); + let expected_count = 10; + + assert_eq!(expected_count, actual_count); + } } diff --git a/tests/formatter.rs b/tests/formatter.rs index b08167bd..9843ef12 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -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 = ["wycats@example.com"] + + [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); +}