diff --git a/insta/src/snapshot.rs b/insta/src/snapshot.rs index af6c654e..93569f87 100644 --- a/insta/src/snapshot.rs +++ b/insta/src/snapshot.rs @@ -460,8 +460,7 @@ impl Snapshot { // (could rename to `matches_exact` for consistency, after some current // pending merge requests are merged) pub fn matches_fully(&self, other: &Snapshot) -> bool { - let contents_match_exact = - self.contents().as_str_exact() == other.contents().as_str_exact(); + let contents_match_exact = self.contents().matches_fully(other.contents()); match self.kind() { SnapshotKind::File => { self.metadata.trim_for_persistence() == other.metadata.trim_for_persistence() @@ -551,9 +550,13 @@ impl SnapshotContents { let out = sc.to_string(); // Legacy inline snapshots have `---` at the start, so this strips that if // it exists. - match out.strip_prefix("---\n") { + let out = match out.strip_prefix("---\n") { Some(old_snapshot) => old_snapshot.to_string(), None => out, + }; + match sc.kind { + SnapshotKind::Inline => legacy_inline_normalize(&out), + SnapshotKind::File => out, } } as_str_legacy(self) == as_str_legacy(other) @@ -617,7 +620,7 @@ impl SnapshotContents { fn normalize(&self) -> String { let kind_specific_normalization = match self.kind { - SnapshotKind::Inline => get_inline_snapshot_value(&self.contents), + SnapshotKind::Inline => normalize_inline_snapshot(&self.contents), SnapshotKind::File => self.contents.clone(), }; // Then this we do for both kinds @@ -727,60 +730,46 @@ fn test_names_of_path() { ); } -/// Helper function that returns the real inline snapshot value from a given -/// frozen value string. If the string starts with the '⋮' character -/// (optionally prefixed by whitespace) the alternative serialization format -/// is picked which has slightly improved indentation semantics. -/// -/// This also changes all newlines to \n -fn get_inline_snapshot_value(frozen_value: &str) -> String { - // TODO: could move this into the SnapshotContents struct - // (the only call site) - - if frozen_value.trim_start().starts_with('⋮') { - elog!("A snapshot uses an old snapshot format; please update it to the new format with `cargo insta test --force-update-snapshots --accept`.\n\nSnapshot is at: {}", frozen_value); - - // legacy format - retain so old snapshots still work - // TODO: move this into `matches_legacy` after the current merge - // requests have settled. - let mut buf = String::new(); - let mut line_iter = frozen_value.lines(); - let mut indentation = 0; - - for line in &mut line_iter { - let line_trimmed = line.trim_start(); - if line_trimmed.is_empty() { - continue; - } - indentation = line.len() - line_trimmed.len(); - // 3 because '⋮' is three utf-8 bytes long - buf.push_str(&line_trimmed[3..]); - buf.push('\n'); - break; +/// legacy format - retain so old snapshots still work +fn legacy_inline_normalize(frozen_value: &str) -> String { + if !frozen_value.trim_start().starts_with('⋮') { + return frozen_value.to_string(); + } + let mut buf = String::new(); + let mut line_iter = frozen_value.lines(); + let mut indentation = 0; + + for line in &mut line_iter { + let line_trimmed = line.trim_start(); + if line_trimmed.is_empty() { + continue; } + indentation = line.len() - line_trimmed.len(); + // 3 because '⋮' is three utf-8 bytes long + buf.push_str(&line_trimmed[3..]); + buf.push('\n'); + break; + } - for line in &mut line_iter { - if let Some(prefix) = line.get(..indentation) { - if !prefix.trim().is_empty() { - return "".to_string(); - } + for line in &mut line_iter { + if let Some(prefix) = line.get(..indentation) { + if !prefix.trim().is_empty() { + return "".to_string(); } - if let Some(remainder) = line.get(indentation..) { - if let Some(rest) = remainder.strip_prefix('⋮') { - buf.push_str(rest); - buf.push('\n'); - } else if remainder.trim().is_empty() { - continue; - } else { - return "".to_string(); - } + } + if let Some(remainder) = line.get(indentation..) { + if let Some(rest) = remainder.strip_prefix('⋮') { + buf.push_str(rest); + buf.push('\n'); + } else if remainder.trim().is_empty() { + continue; + } else { + return "".to_string(); } } - - buf.trim_end().to_string() - } else { - normalize_inline_snapshot(frozen_value) } + + buf.trim_end().to_string() } #[test] @@ -1021,7 +1010,7 @@ a #[test] fn test_inline_snapshot_value_newline() { // https://github.com/mitsuhiko/insta/issues/39 - assert_eq!(get_inline_snapshot_value("\n"), ""); + assert_eq!(normalize_inline_snapshot("\n"), ""); } #[test]