Skip to content

Commit

Permalink
fix(snap)!: Include a trailing newline in snapshots
Browse files Browse the repository at this point in the history
In using snapshots, I found the leading newline so good that I feel like
we should have a trailing newline as well!

Cherry pick 7fbcc9c (assert-rs#272)
  • Loading branch information
epage committed May 23, 2024
1 parent ffca2b6 commit 0b77043
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion crates/snapbox/src/data/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ fn format_patch(patch: &str) -> String {
buf.push('\n');
}
buf.push_str(patch);
if is_multiline {
buf.push('\n');
}
lit_kind.write_end(&mut buf).unwrap();
if matches!(lit_kind, StrLitKind::Raw(_)) {
buf.push(']');
Expand Down Expand Up @@ -359,7 +362,9 @@ mod tests {
[r#"
hello
world
"#]"##]],
"#]
"##]],
);

let patch = format_patch(r"hello\tworld");
Expand Down Expand Up @@ -395,6 +400,7 @@ Patchwork {
),
],
}
"#]],
);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/snapbox/src/data/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ pub struct Inline {
impl Inline {
pub(crate) fn trimmed(&self) -> String {
let mut data = self.data;
if data.contains('\n') && data.starts_with('\n') {
data = &data[1..]
if data.contains('\n') {
data = data.strip_prefix('\n').unwrap_or(data);
data = data.strip_suffix('\n').unwrap_or(data);
}
data.to_owned()
}
Expand Down
1 change: 1 addition & 0 deletions crates/snapbox/tests/testsuite/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ line1
str![[r#"
line1
line2
"#]],
);
}
Expand Down

0 comments on commit 0b77043

Please sign in to comment.