insta
insta copied to clipboard
support prettier yaml with newlines
struct A {
b: String,
c: String,
}
#[test]
fn t() {
let a = A {
b: "aaa\nbbb\n".to_string(),
c: "ccc".to_string(),
};
insta::assert_yaml_snapshot!(a);
}
Instead of
---
source: ...
expression: a
---
b: "aaa\nbbb\n"
c: ccc
I want to have
---
b: |
aaa
bbb
c: cccc
P.S. My actual use case is that my data looks like this:
let chunk2 = StreamChunk::from_pretty(
" I I
+ 7 6
- 3 2
- 1 0
+ 5 7
- 2 1
+ 11 8",
);
And I'm trying to replace the tests with insta, but without pretty newlines it's not usable :(
P.P.S, I noticed you switched from serde_yaml to rust-yaml for serialization https://github.com/mitsuhiko/insta/pull/264 (due to it's breaking changes?), but serde-yaml 0.9 also makes the formatting prettier https://github.com/dtolnay/serde-yaml/issues/226, so that's a little unfortunate :(
My workaround is to call serde_yaml::to_string on my own, and pass a string to assert_snapshot
As a user in the same place as you @xxchan:
- I've tried to get rust to serialize yaml nicely, in particular multiline strings — both within and outside of insta, both with
serde_yamlandrust-yaml - But I've struggled (though https://github.com/dtolnay/serde-yaml/issues/226 does look better than I remember)
- I'm not sure there's much insta can do, at least if there is I'd be happy to put a PR in
Well, one thing I can't workaround is the header part. expr and description both can't prettify newlines. info works, but that requires me to add Serialize :(
I would be happy to see multi line strings in the generated YAML. Would absolutely accept a patch. Unfortunately we use yaml_rust and it's hard to move off it without breaking all snapshots and that does not itself support pretty formatting.
Turns out toml serialization looks a lot better for simple multi-line comparisons. Still hoping for YAML in the longer run, but TOML will have to do for now. Thanks for the awesome tool!
Considering that yaml_rust is unmaintained since 3 years, would it be worth a consideration again to switch to serde_yaml?
I'm considering bumping all of this up with 2.0 and also move to serde_yaml. The risk here obviously is that serde_yaml gives no guarantees of snapshot stability.
Turns out serde_yaml is now officially deprecated.