Skip to content

Commit

Permalink
Use inline block format also for strings of form "foo\n" (#225)
Browse files Browse the repository at this point in the history
This changes the formatting for strings with a trailing newline to block format.
  • Loading branch information
martinvonz committed Apr 27, 2022
1 parent abde903 commit 56504f1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/snapshot.rs
Expand Up @@ -317,12 +317,12 @@ impl SnapshotContents {
pub fn to_inline(&self, indentation: usize) -> String {
let contents = &self.0;
let mut out = String::new();
let is_escape = contents.lines().count() > 1 || contents.contains(&['\\', '"'][..]);
let is_escape = contents.contains(&['\n', '\\', '"'][..]);

out.push_str(if is_escape { "r###\"" } else { "\"" });
// if we have more than one line we want to change into the block
// representation mode
if contents.lines().count() > 1 {
if contents.contains('\n') {
out.extend(
contents
.lines()
Expand Down Expand Up @@ -512,6 +512,16 @@ b"[1..];
\"###"
);

let t = &"
ab
"[1..];
assert_eq!(
SnapshotContents(t.to_string()).to_inline(0),
"r###\"
ab
\"###"
);

let t = "ab";
assert_eq!(SnapshotContents(t.to_string()).to_inline(0), r##""ab""##);
}
Expand Down

0 comments on commit 56504f1

Please sign in to comment.