From d4ab2106edddef33cfdc4b784661f0a4c91ee69a Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 20 Dec 2023 08:57:09 -0800 Subject: [PATCH] Before writing a value, double-check that we can roundtrip it. --- .../kotlin/com/diffplug/selfie/SourceFile.kt | 19 +++++++++++++++++++ .../junit5/__snapshots__/UT_PrismTrainTest.ss | 2 ++ 2 files changed, 21 insertions(+) diff --git a/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SourceFile.kt b/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SourceFile.kt index 94af2c17..3b84a3e5 100644 --- a/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SourceFile.kt +++ b/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SourceFile.kt @@ -43,6 +43,25 @@ class SourceFile(filename: String, content: String) { */ fun setLiteralAndGetNewlineDelta(literalValue: LiteralValue): 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)")) diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_PrismTrainTest.ss b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_PrismTrainTest.ss index 67565856..8df13763 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_PrismTrainTest.ss +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_PrismTrainTest.ss @@ -1,3 +1,5 @@ ╔═ selfie ═╗ apple +╔═ selfie[count] ═╗ +5 ╔═ [end of file] ═╗