Skip to content

Commit

Permalink
Small refactor of inline normalization (#602)
Browse files Browse the repository at this point in the history
Now that some PRs have merged, this cleans up a couple functions
  • Loading branch information
max-sixty authored Sep 21, 2024
1 parent 71e7340 commit 406e46c
Showing 1 changed file with 42 additions and 53 deletions.
95 changes: 42 additions & 53 deletions insta/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 406e46c

Please sign in to comment.