Repository: mitsuhiko/insta
Pull request #62 (2019-08-23 15:32:20)

Fix #56
Between the whitespace PR and https://github.com/mitsuhiko/insta/pull/56, I combine incompatible code. Shouldn't have left out the tests!

Now fixed. I also hand-tested an Accept for an inline test

mitsuhiko: Also since I have you: i fixed a regression here which caused the serialization system to change which made cargo-insta incomaptible with non master builds.
max-sixty: OK great - apologies assuming that was me. I can't see the break though - the changes I see there are a) changing `SnapshotContents` to a tuple struct and b) making the `SnapshotContents` field private. 
Where was the regression? Anything I can do here to confirm a fix with a test?
max-sixty: (also any other ideas for features / stuff to work on - I think this library is awesome. I have more time & interest atm than I have ideas for features)
mitsuhiko: `SnapshotContents` is serialized to disk. So the change i merged earlier changed the disk serialization format causing cargo review to fail when it used data created from earlier insta versions.

Patch:
diff --git a/cargo-insta/src/inline.rs b/cargo-insta/src/inline.rs
index b808fd0..4344e3e 100644
--- a/cargo-insta/src/inline.rs
+++ b/cargo-insta/src/inline.rs
@@ -82,6 +82,11 @@ impl FilePatcher {
         let snapshot_line_contents =
             vec![prefix, snapshot.to_inline(inline.indentation), suffix].join("");
 
+        self.lines.splice(
+            inline.start.0..=inline.end.0,
+            snapshot_line_contents.lines().map(|l| l.to_string()),
+        );
+
         // update other snapshot locations
         let old_lines_count = inline.end.0 - inline.start.0 + 1;
         let line_count_diff = snapshot_line_contents.lines().count() - old_lines_count;
diff --git a/src/snapshot.rs b/src/snapshot.rs
index de45137..f7da59c 100644
--- a/src/snapshot.rs
+++ b/src/snapshot.rs
@@ -1,4 +1,3 @@
-use std::borrow::Cow;
 use std::error::Error;
 use std::fs;
 use std::io::{BufRead, BufReader, Write};
@@ -290,44 +289,72 @@ impl PartialEq for SnapshotContents {
     }
 }
 
+#[test]
+fn test_snapshot_contents() {
+    let snapshot_contents = SnapshotContents("testing".to_string());
+    assert_eq!(snapshot_contents.to_inline(0), r#""testing""#);
+}
+
 // from a snapshot to a string we want to write back
 fn denormalize_inline_snapshot(snapshot: &str, indentation: usize) -> String {
     let mut out = String::new();
     let is_escape = snapshot.lines().count() > 1 || snapshot.contains(&['\\', '"'][..]);
 
     out.push_str(if is_escape { "r###\"" } else { "\"" });
-    let mut new_lines: Vec<_> = snapshot.lines().map(Cow::Borrowed).collect();
-    if new_lines.is_empty() {
-        new_lines.push(Cow::Borrowed(""));
-    }
-
     // if we have more than one line we want to change into the block
     // representation mode
-    if new_lines.len() > 1 || snapshot.contains('┇') {
-        new_lines.insert(0, Cow::Borrowed(""));
-        if indentation > 0 {
-            for (idx, line) in new_lines.iter_mut().enumerate() {
-                if idx == 0 {
-                    continue;
-                }
-                *line = Cow::Owned(format!(
-                    "{c: >width$}{line}",
-                    c = "⋮",
-                    width = indentation,
-                    line = line
-                ));
-            }
-            new_lines.push(Cow::Owned(format!(
-                "{c: >width$}",
-                c = " ",
-                width = indentation
-            )));
-        } else {
-            new_lines.push(Cow::Borrowed(""));
-        }
+    if snapshot.lines().count() > 1 {
+        out.push_str("\n");
+        out.extend(
+            snapshot
+                .lines()
+                .map(|l| format!("{c: >width$}{l}\n", c = "⋮", width = indentation, l = l)),
+        );
+        out.push_str(&format!("{c: >width$}", c = " ", width = indentation));
+    } else {
+        out.push_str(snapshot);
     }
 
     out.push_str(if is_escape { "\"###" } else { "\"" });
 
     out
 }
+
+#[test]
+fn test_denormalize_inline_snapshot() {
+    let t = &"
+a
+b"[1..];
+    assert_eq!(
+        denormalize_inline_snapshot(t, 0),
+        "r###\"
+⋮a
+⋮b
+ \"###"
+    );
+
+    let t = &"
+a
+b"[1..];
+    assert_eq!(
+        denormalize_inline_snapshot(t, 4),
+        "r###\"
+   ⋮a
+   ⋮b
+    \"###"
+    );
+
+    let t = &"
+    a
+    b"[1..];
+    assert_eq!(
+        denormalize_inline_snapshot(t, 0),
+        "r###\"
+⋮    a
+⋮    b
+ \"###"
+    );
+
+    let t = "ab";
+    assert_eq!(denormalize_inline_snapshot(t, 0), r##""ab""##);
+}


Test patch:
diff --git a/tests/test_inline.rs b/tests/test_inline.rs
index 96d870d..1374bb0 100644
--- a/tests/test_inline.rs
+++ b/tests/test_inline.rs
@@ -1,9 +1,9 @@
-use insta::{
-    assert_debug_snapshot_matches, assert_json_snapshot_matches,
-    assert_snapshot_matches, assert_yaml_snapshot_matches,
-};
 #[cfg(feature = "ron")]
 use insta::assert_ron_snapshot_matches;
+use insta::{
+    assert_debug_snapshot_matches, assert_json_snapshot_matches, assert_snapshot_matches,
+    assert_yaml_snapshot_matches,
+};
 use serde::Serialize;
 use std::thread;