From 2e6d0ac2ea5d6d3ce521ef81c77c970b47eaacf3 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Mon, 1 Sep 2025 20:58:18 -0600 Subject: [PATCH 1/2] test: Newline count and width --- src/renderer/mod.rs | 24 +++++++++++++- tests/formatter.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 25e9508b..5afd1d0f 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -3021,7 +3021,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 +3043,26 @@ 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); + #[cfg(feature = "simd")] + let expected_count = 9; + #[cfg(not(feature = "simd"))] + let expected_count = 10; + + assert_eq!(expected_count, actual_count); + } } diff --git a/tests/formatter.rs b/tests/formatter.rs index b08167bd..9e2b2a46 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -3227,3 +3227,79 @@ 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)), + ), + ]; + + #[cfg(feature = "simd")] + { + 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); + } + + #[cfg(not(feature = "simd"))] + { + 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); + } +} From b43f04fda4876446956a6e6c1287e5a59b6df101 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Mon, 1 Sep 2025 21:40:58 -0600 Subject: [PATCH 2/2] fix: Make SIMD newline count match non-SIMD --- src/renderer/mod.rs | 7 +------ tests/formatter.rs | 28 ---------------------------- 2 files changed, 1 insertion(+), 34 deletions(-) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 5afd1d0f..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"))] { @@ -3058,9 +3056,6 @@ mod test { bar = { base = '^^not-valid^^', path = 'bar' } "#; let actual_count = newline_count(source); - #[cfg(feature = "simd")] - let expected_count = 9; - #[cfg(not(feature = "simd"))] let expected_count = 10; assert_eq!(expected_count, actual_count); diff --git a/tests/formatter.rs b/tests/formatter.rs index 9e2b2a46..9843ef12 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -3253,33 +3253,6 @@ fn multiple_line_num_widths() { ), ]; - #[cfg(feature = "simd")] - { - 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); - } - - #[cfg(not(feature = "simd"))] - { 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 @@ -3301,5 +3274,4 @@ error: invalid character `^` in path base name: `^^not-valid^^`, the first chara "#]]; let renderer = renderer.decor_style(DecorStyle::Unicode); assert_data_eq!(renderer.render(input), expected_unicode); - } }