Skip to content

Commit

Permalink
Merge branch 'master' into trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty authored Aug 12, 2024
2 parents 543258c + 9132d04 commit f5c50d4
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions insta/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,10 @@ impl SnapshotContents {
let contents = &self.as_str();
let mut out = String::new();

// We don't technically need to escape on newlines, but it reduces diffs
let is_escape = contents.contains(['\\', '"', '\n']);
// Escape the string if needed, with `r#`, using with 1 more `#` than
// the maximum number of existing contiguous `#`.
let is_escape = contents.contains(&['\n', '\\', '"'][..]);
let delimiter = if is_escape {
let max_contiguous_hash = contents
.split(|c| c != '#')
Expand Down Expand Up @@ -939,32 +940,34 @@ a
b"[1..];
assert_eq!(
SnapshotContents(t.to_string()).to_inline(0),
"r#\"
r##"r#"
a
b
\"#"
"#"##
);

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

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

let t = &"
Expand All @@ -973,17 +976,28 @@ a
b"[1..];
assert_eq!(
SnapshotContents(t.to_string()).to_inline(4),
"r#\"
r##"r#"
a
b
\"#"
"#"##
);


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

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

let t = "ab";
assert_eq!(SnapshotContents(t.to_string()).to_inline(0), r#""ab""#);
}
Expand All @@ -993,12 +1007,12 @@ fn test_snapshot_contents_hashes() {
let t = "a###b";
assert_eq!(SnapshotContents(t.to_string()).to_inline(0), r#""a###b""#);

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

0 comments on commit f5c50d4

Please sign in to comment.