Skip to content

Commit

Permalink
Before writing a value, double-check that we can roundtrip it.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Dec 20, 2023
1 parent c21c96f commit d4ab210
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SourceFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ class SourceFile(filename: String, content: String) {
*/
fun <T : Any> setLiteralAndGetNewlineDelta(literalValue: LiteralValue<T>): Int {
val encoded = literalValue.format.encode(literalValue.actual, language)
val roundTripped = literalValue.format.parse(encoded, language) // sanity check
if (roundTripped != literalValue.actual) {
throw Error(
"There is an error in " +
literalValue.format::class.simpleName +
", the following value isn't roundtripping.\n" +
"Please this error and the data below at https://github.com/diffplug/selfie/issues/new\n" +
"```\n" +
"ORIGINAL\n" +
literalValue.actual +
"\n" +
"ROUNDTRIPPED\n" +
roundTripped +
"\n" +
"ENCODED ORIGINAL\n" +
encoded +
"\n" +
"```\n")
}
val existingNewlines = slice.count { it == '\n' }
val newNewlines = encoded.count { it == '\n' }
contentSlice = Slice(slice.replaceSelfWith(".toBe($encoded)"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
╔═ selfie ═╗
apple
╔═ selfie[count] ═╗
5
╔═ [end of file] ═╗

0 comments on commit d4ab210

Please sign in to comment.