From f82260b15fb1500964ec6737d835f2cbbe884dbf Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 18 Apr 2024 13:30:50 -0500 Subject: [PATCH] fix(snap)!: Remove indent support Rather than dealing with the special cases where this doesn't exist, let's just not do this. This also aligns with the general style within Cargo's tests. Fixes #267 --- crates/snapbox/src/data/mod.rs | 1 - crates/snapbox/src/data/runtime.rs | 99 ++++++++++-------------------- crates/snapbox/src/data/source.rs | 30 --------- crates/snapbox/tests/assert.rs | 15 +---- 4 files changed, 32 insertions(+), 113 deletions(-) diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index d4c7941f..552cc112 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -85,7 +85,6 @@ macro_rules! str { let inline = $crate::data::Inline { position, data: $data, - indent: true, }; inline }}; diff --git a/crates/snapbox/src/data/runtime.rs b/crates/snapbox/src/data/runtime.rs index 52dad661..4c370798 100644 --- a/crates/snapbox/src/data/runtime.rs +++ b/crates/snapbox/src/data/runtime.rs @@ -73,12 +73,7 @@ impl SourceFileRuntime { } fn update(&mut self, actual: &str, inline: &Inline) -> std::io::Result<()> { let span = Span::from_pos(&inline.position, &self.original_text); - let desired_indent = if inline.indent { - Some(span.line_indent) - } else { - None - }; - let patch = format_patch(desired_indent, actual); + let patch = format_patch(actual); self.patchwork.patch(span.literal_range, &patch); std::fs::write(&inline.position.file, &self.patchwork.text) } @@ -135,9 +130,8 @@ fn lit_kind_for_patch(patch: &str) -> StrLitKind { StrLitKind::Raw(max_hashes + 1) } -fn format_patch(desired_indent: Option, patch: &str) -> String { +fn format_patch(patch: &str) -> String { let lit_kind = lit_kind_for_patch(patch); - let indent = desired_indent.map(|it| " ".repeat(it)); let is_multiline = patch.contains('\n'); let mut buf = String::new(); @@ -148,21 +142,9 @@ fn format_patch(desired_indent: Option, patch: &str) -> String { if is_multiline { buf.push('\n'); } - let mut final_newline = false; for line in crate::utils::LinesWithTerminator::new(patch) { - if is_multiline && !line.trim().is_empty() { - if let Some(indent) = &indent { - buf.push_str(indent); - buf.push_str(" "); - } - } + if is_multiline && !line.trim().is_empty() {} buf.push_str(line); - final_newline = line.ends_with('\n'); - } - if final_newline { - if let Some(indent) = &indent { - buf.push_str(indent); - } } lit_kind.write_end(&mut buf).unwrap(); if matches!(lit_kind, StrLitKind::Raw(_)) { @@ -173,8 +155,6 @@ fn format_patch(desired_indent: Option, patch: &str) -> String { #[derive(Clone, Debug)] struct Span { - line_indent: usize, - /// The byte range of the argument to `expect!`, including the inner `[]` if it exists. literal_range: std::ops::Range, } @@ -207,13 +187,12 @@ impl Span { .0; let literal_start = line_start + byte_offset; - let indent = line.chars().take_while(|&it| it == ' ').count(); - target_line = Some((literal_start, indent)); + target_line = Some(literal_start); break; } line_start += line.len(); } - let (literal_start, line_indent) = target_line.unwrap(); + let literal_start = target_line.unwrap(); let lit_to_eof = &file[literal_start..]; let lit_to_eof_trimmed = lit_to_eof.trim_start(); @@ -223,10 +202,7 @@ impl Span { let literal_len = locate_end(lit_to_eof_trimmed).expect("Couldn't find closing delimiter for `expect!`."); let literal_range = literal_start..literal_start + literal_len; - Span { - line_indent, - literal_range, - } + Span { literal_range } } } @@ -378,35 +354,22 @@ mod tests { #[test] fn test_format_patch() { - let patch = format_patch(None, "hello\nworld\n"); + let patch = format_patch("hello\nworld\n"); assert_eq( str![[r##" - [r#" - hello - world - "#]"##]], +[r#" +hello +world +"#]"##]], patch, ); - let patch = format_patch(None, r"hello\tworld"); + let patch = format_patch(r"hello\tworld"); assert_eq(str![[r##"[r#"hello\tworld"#]"##]], patch); - let patch = format_patch(None, "{\"foo\": 42}"); + let patch = format_patch("{\"foo\": 42}"); assert_eq(str![[r##"[r#"{"foo": 42}"#]"##]], patch); - - let patch = format_patch(Some(0), "hello\nworld\n"); - assert_eq( - str![[r##" - [r#" - hello - world - "#]"##]], - patch, - ); - - let patch = format_patch(Some(4), "single line"); - assert_eq(str![[r#""single line""#]], patch); } #[test] @@ -417,24 +380,24 @@ mod tests { patchwork.patch(8..13, "3"); assert_eq( str![[r#" - Patchwork { - text: "один zwei 3", - indels: [ - ( - 0..3, - 8, - ), - ( - 4..7, - 4, - ), - ( - 8..13, - 1, - ), - ], - } - "#]], +Patchwork { + text: "один zwei 3", + indels: [ + ( + 0..3, + 8, + ), + ( + 4..7, + 4, + ), + ( + 8..13, + 1, + ), + ], +} +"#]], patchwork.to_debug(), ); } diff --git a/crates/snapbox/src/data/source.rs b/crates/snapbox/src/data/source.rs index 5dd3aaa1..d47a89a2 100644 --- a/crates/snapbox/src/data/source.rs +++ b/crates/snapbox/src/data/source.rs @@ -75,17 +75,9 @@ pub struct Inline { pub position: Position, #[doc(hidden)] pub data: &'static str, - #[doc(hidden)] - pub indent: bool, } impl Inline { - /// Indent to quote-level when overwriting the string literal (default) - pub fn indent(mut self, yes: bool) -> Self { - self.indent = yes; - self - } - /// Initialize `Self` as [`format`][crate::data::DataFormat] or [`Error`][crate::data::DataFormat::Error] /// /// This is generally used for `expected` data @@ -107,33 +99,11 @@ impl Inline { if data.starts_with('\n') { data = &data[1..] } - if self.indent { - return trim_indent(data); - } } data.to_owned() } } -fn trim_indent(text: &str) -> String { - let indent = text - .lines() - .filter(|it| !it.trim().is_empty()) - .map(|it| it.len() - it.trim_start().len()) - .min() - .unwrap_or(0); - - crate::utils::LinesWithTerminator::new(text) - .map(|line| { - if line.len() <= indent { - line.trim_start_matches(' ') - } else { - &line[indent..] - } - }) - .collect() -} - impl std::fmt::Display for Inline { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.position.fmt(f) diff --git a/crates/snapbox/tests/assert.rs b/crates/snapbox/tests/assert.rs index 82dcc6c1..60927b19 100644 --- a/crates/snapbox/tests/assert.rs +++ b/crates/snapbox/tests/assert.rs @@ -9,24 +9,11 @@ fn test_trivial_assert() { #[test] fn smoke_test_indent() { - assert_eq( - str![[r#" - line1 - line2 - "#]] - .indent(true), - "\ -line1 - line2 -", - ); - assert_eq( str![[r#" line1 line2 -"#]] - .indent(false), +"#]], "\ line1 line2