Skip to content

Commit

Permalink
Merge pull request #271 from epage/break
Browse files Browse the repository at this point in the history
fix(snap)!: Remove inline snapshot indent support
  • Loading branch information
epage authored Apr 18, 2024
2 parents 2769ed8 + 20d65cd commit 79b304a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 120 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
102 changes: 31 additions & 71 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,22 +142,7 @@ 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(" ");
}
}
buf.push_str(line);
final_newline = line.ends_with('\n');
}
if final_newline {
if let Some(indent) = &indent {
buf.push_str(indent);
}
}
buf.push_str(patch);
lit_kind.write_end(&mut buf).unwrap();
if matches!(lit_kind, StrLitKind::Raw(_)) {
buf.push(']');
Expand All @@ -173,8 +152,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 +184,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 +199,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 +351,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 +377,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
36 changes: 2 additions & 34 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 @@ -103,37 +95,13 @@ impl Inline {

fn trimmed(&self) -> String {
let mut data = self.data;
if data.contains('\n') {
if data.starts_with('\n') {
data = &data[1..]
}
if self.indent {
return trim_indent(data);
}
if data.contains('\n') && data.starts_with('\n') {
data = &data[1..]
}
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 79b304a

Please sign in to comment.