Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow snapshotting values which contain ### #540

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to insta and cargo-insta are documented here.

## 1.40.0

- Inline snapshots now use the required number of `#`s to escape the snapshot
value, rather than always using `###`. This allows snapshotting values which
themselves contain `###`. If there are no existing `#` characters in the
snapshot value, a single `#` will be used.

- Print a warning when encountering old snapshot formats. #503

- No longer suggest running `cargo insta` message when running `cargo insta test --check`. #515
Expand Down
10 changes: 5 additions & 5 deletions cargo-insta/tests/snapshots/main__test_json_inline.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ fn test_json_snapshot() {
};
insta::assert_json_snapshot!(&user, {
".id" => "[user_id]",
}, @r###"
}, @r#"
{
"id": "[user_id]",
"email": "[email protected]"
}
"###);
"#);
}

#[test]
Expand All @@ -34,7 +34,7 @@ fn test_json_snapshot_trailing_comma() {
};
insta::assert_compact_json_snapshot!(
&user,
@r###"{"id": 42, "email": "[email protected]"}"###,
@r#"{"id": 42, "email": "[email protected]"}"#,
);
}

Expand All @@ -49,11 +49,11 @@ fn test_json_snapshot_trailing_comma_redaction() {
{
".id" => "[user_id]",
},
@r###"
@r#"
{
"id": "[user_id]",
"email": "[email protected]"
}
"###,
"#,
);
}
8 changes: 4 additions & 4 deletions cargo-insta/tests/snapshots/main__test_yaml_inline.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ fn test_yaml_snapshot() {
};
insta::assert_yaml_snapshot!(&user, {
".id" => "[user_id]",
}, @r###"
}, @r#"
---
id: "[user_id]"
email: [email protected]
"###);
"#);
}

#[test]
Expand All @@ -36,10 +36,10 @@ fn test_yaml_snapshot_trailing_comma() {
{
".id" => "[user_id]",
},
@r###"
@r#"
---
id: "[user_id]"
email: [email protected]
"###,
"#,
);
}
56 changes: 44 additions & 12 deletions insta/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,25 @@ impl SnapshotContents {
pub fn to_inline(&self, indentation: usize) -> String {
let contents = &self.0;
let mut out = String::new();

// 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 != '#')
.map(|group| group.len())
.max()
.unwrap_or(0);
out.push('r');
"#".repeat(max_contiguous_hash + 1)
} else {
"".to_string()
};

out.push_str(&delimiter);
out.push('"');

out.push_str(if is_escape { "r###\"" } else { "\"" });
// if we have more than one line we want to change into the block
// representation mode
if contents.contains('\n') {
Expand All @@ -554,7 +570,8 @@ impl SnapshotContents {
out.push_str(contents);
}

out.push_str(if is_escape { "\"###" } else { "\"" });
out.push('"');
out.push_str(&delimiter);

out
}
Expand Down Expand Up @@ -739,32 +756,32 @@ a
b"[1..];
assert_eq!(
SnapshotContents(t.to_string()).to_inline(0),
"r###\"
"r#\"
a
b
\"###"
\"#"
);

let t = &"
a
b"[1..];
assert_eq!(
SnapshotContents(t.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#\"
a
b
\"###"
\"#"
);

let t = &"
Expand All @@ -773,27 +790,42 @@ b"[1..];
b"[1..];
assert_eq!(
SnapshotContents(t.to_string()).to_inline(0),
"r###\"
"r#\"
a

b
\"###"
\"#"
);

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""#);
}

#[test]
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";
assert_eq!(
SnapshotContents(t.to_string()).to_inline(0),
r#####"r####"
a
###b
"####"#####
);
}

#[test]
fn test_normalize_inline_snapshot() {
use similar_asserts::assert_eq;
Expand Down
Loading