Skip to content

Commit

Permalink
fix(snap)!: Remove indent support
Browse files Browse the repository at this point in the history
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 assert-rs#267
  • Loading branch information
epage committed Apr 18, 2024
1 parent 2769ed8 commit f82260b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 113 deletions.
1 change: 0 additions & 1 deletion crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ macro_rules! str {
let inline = $crate::data::Inline {
position,
data: $data,
indent: true,
};
inline
}};
Expand Down
99 changes: 31 additions & 68 deletions crates/snapbox/src/data/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -135,9 +130,8 @@ fn lit_kind_for_patch(patch: &str) -> StrLitKind {
StrLitKind::Raw(max_hashes + 1)
}

fn format_patch(desired_indent: Option<usize>, 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();
Expand All @@ -148,21 +142,9 @@ fn format_patch(desired_indent: Option<usize>, 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(_)) {
Expand All @@ -173,8 +155,6 @@ fn format_patch(desired_indent: Option<usize>, 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<usize>,
}
Expand Down Expand Up @@ -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();
Expand All @@ -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 }
}
}

Expand Down Expand Up @@ -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]
Expand All @@ -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(),
);
}
Expand Down
30 changes: 0 additions & 30 deletions crates/snapbox/src/data/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
15 changes: 1 addition & 14 deletions crates/snapbox/tests/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f82260b

Please sign in to comment.