Skip to content

Commit 0330ede

Browse files
committed
The inline diff makes it a little tricky to differentiate the msg, so I added a long horizontal pipe character.
1 parent 1ee4009 commit 0330ede

File tree

2 files changed

+23
-6
lines changed
  • jvm
    • selfie-lib/src/commonMain/kotlin/com/diffplug/selfie
    • selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5

2 files changed

+23
-6
lines changed

jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/Mode.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package com.diffplug.selfie
1717

1818
import com.diffplug.selfie.guts.CallStack
1919
import com.diffplug.selfie.guts.CommentTracker
20+
import com.diffplug.selfie.guts.SnapshotNotEqualErrorMsg
2021
import com.diffplug.selfie.guts.SnapshotSystem
2122
import com.diffplug.selfie.guts.TypedPath
2223

@@ -42,13 +43,26 @@ enum class Mode {
4243
internal fun msgSnapshotNotFound() = msg("Snapshot not found")
4344
internal fun msgSnapshotNotFoundNoSuchFile(file: TypedPath) =
4445
msg("Snapshot not found: no such file $file")
45-
internal fun msgSnapshotMismatch(expected: String, actual: String) = msg("Snapshot mismatch")
46+
internal fun msgSnapshotMismatch(expected: String, actual: String) =
47+
msg(SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
4648
internal fun msgSnapshotMismatchBinary(expected: ByteArray, actual: ByteArray) =
47-
msg("Snapshot mismatch")
49+
msgSnapshotMismatch(expected.toQuotedPrintable(), actual.toQuotedPrintable())
50+
private fun ByteArray.toQuotedPrintable(): String {
51+
val sb = StringBuilder()
52+
for (byte in this) {
53+
val b = byte.toInt() and 0xFF // Make sure byte is treated as unsigned
54+
if (b in 33..126 && b != 61) { // Printable ASCII, except '='
55+
sb.append(b.toChar())
56+
} else {
57+
sb.append("=").append(b.toString(16).uppercase().padStart(2, '0')) // Convert to hex and pad
58+
}
59+
}
60+
return sb.toString()
61+
}
4862
private fun msg(headline: String) =
4963
when (this) {
5064
interactive ->
51-
"$headline\n" +
65+
"$headline\n" + (if (headline.any { it == '\n' }) "────────────────────\n" else "") +
5266
"- update this snapshot by adding `_TODO` to the function name\n" +
5367
"- update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`"
5468
readonly -> headline

jvm/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/InteractiveTest.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ class InteractiveTest : HarnessJUnit() {
3737
fun inlineMismatch() {
3838
ut_mirrorKt().lineWith("expectSelfie(").setContent(" expectSelfie(5).toBe(10)")
3939
gradleInteractiveFail().message shouldBe
40-
"Snapshot mismatch\n" +
41-
"- update this snapshot by adding `_TODO` to the function name\n" +
42-
"- update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`"
40+
"""Snapshot mismatch at L1:C1
41+
-10
42+
+5
43+
────────────────────
44+
- update this snapshot by adding `_TODO` to the function name
45+
- update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`"""
4346
}
4447

4548
@Test @Order(3)

0 commit comments

Comments
 (0)