diff --git a/insta/src/snapshot.rs b/insta/src/snapshot.rs index df3f2957..8b00a78e 100644 --- a/insta/src/snapshot.rs +++ b/insta/src/snapshot.rs @@ -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 != '#') @@ -939,21 +940,23 @@ 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 = &" @@ -961,10 +964,10 @@ b"[1..]; b"[1..]; assert_eq!( SnapshotContents(t.to_string()).to_inline(0), - "r#\" + r##"r#" a b -\"#" +"#"## ); let t = &" @@ -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""#); } @@ -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 "####"##### ); }