java-snapshot-testing
java-snapshot-testing copied to clipboard
Perfect roundtrips on plaintext snapshots
Discussed in https://github.com/origin-energy/java-snapshot-testing/discussions/129
Originally posted by nedtwigg December 11, 2022
I think it's important for plaintext snapshots to have perfect roundtrips. That means that it's okay if the encoded text isn't exactly the same as the original text, but when decoded it should be exactly the same again, including newlines. I can see a good argument for removing all \r
from snapshots though...
Dumb example: if you change every character into open paren unicode number hex close parent so that A
becomes (41)
, that encoding is bad, but lossless. You get back exactly what you put in.
By picking rare characters for the brackets and escape characters, you get an encoding which is almost always just the text on its own, but in the rare case that you need to escape some characters to encode, you can still perfectly decode.
I dumped some example code which does encoding of that type into https://github.com/origin-energy/java-snapshot-testing/issues/126, feel free to use it if you'd like.