Skip to content

Commit 8aec163

Browse files
committed
Big improvement to VCR error messages.
1 parent e0c7265 commit 8aec163

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ enum class Mode {
4747
msg("Snapshot " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
4848
internal fun msgSnapshotMismatchBinary(expected: ByteArray, actual: ByteArray) =
4949
msgSnapshotMismatch(expected.toQuotedPrintable(), actual.toQuotedPrintable())
50-
internal fun msgVcrSnapshotNotFound() = msg("VCR snapshot not found")
51-
internal fun msgVcrMismatch(key: String, expected: String, actual: String) =
52-
msg("VCR frame $key " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
53-
internal fun msgVcrUnread(expected: Int, actual: Int) =
54-
msg("VCR frames unread - only $actual were read out of $expected")
55-
internal fun msgVcrUnderflow(expected: Int) =
56-
msg(
57-
"VCR frames exhausted - only $expected are available but you tried to read ${expected + 1}")
50+
internal fun msgVcrSnapshotNotFound(call: CallStack) = msgVcr("VCR snapshot not found", call)
51+
internal fun msgVcrMismatch(key: String, expected: String, actual: String, call: CallStack) =
52+
msgVcr("VCR frame $key " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual), call)
53+
internal fun msgVcrUnread(expected: Int, actual: Int, call: CallStack) =
54+
msgVcr("VCR frames unread - only $actual were read out of $expected", call)
55+
internal fun msgVcrUnderflow(expected: Int, call: CallStack) =
56+
msgVcr(
57+
"VCR frames exhausted - only $expected are available but you tried to read ${expected + 1}",
58+
call)
5859
private fun ByteArray.toQuotedPrintable(): String {
5960
val sb = StringBuilder()
6061
for (byte in this) {
@@ -71,12 +72,18 @@ enum class Mode {
7172
when (this) {
7273
interactive ->
7374
"$headline\n" +
74-
(if (headline.startsWith("Snapshot "))
75-
"‣ update this snapshot by adding `_TODO` to the function name\n" +
76-
"‣ update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`"
77-
else
78-
"‣ update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`\n" +
79-
"‣ if that doesn't work remember to put your test rule into the `selfie` package")
75+
"‣ update this snapshot by adding `_TODO` to the function name\n" +
76+
"‣ update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`"
77+
readonly -> headline
78+
overwrite -> "$headline\n(didn't expect this to ever happen in overwrite mode)"
79+
}
80+
private fun msgVcr(headline: String, call: CallStack) =
81+
when (this) {
82+
interactive ->
83+
"$headline\n" +
84+
"‣ update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`\n" +
85+
"‣ could not find control comment in ${call.location.ideLink(Selfie.system.layout)}\n" +
86+
"‣ remember to call `Selfie.vcrTestLocator()` in the test itself, or put the file above into the `selfie` package to mark that it is not the test file"
8087
readonly -> headline
8188
overwrite -> "$headline\n(didn't expect this to ever happen in overwrite mode)"
8289
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal constructor(
3131
private val call: CallStack,
3232
private val disk: DiskStorage,
3333
) : AutoCloseable {
34+
/** Creates VCRs who store their data based on where this TestLocator was created. */
3435
class TestLocator internal constructor(private val sub: String, private val disk: DiskStorage) {
3536
private val call = recordCall(false)
3637
fun createVcr() = VcrSelfie(sub, call, disk)
@@ -73,7 +74,8 @@ internal constructor(
7374
} else {
7475
val snapshot =
7576
disk.readDisk(sub, call)
76-
?: throw Selfie.system.fs.assertFailed(Selfie.system.mode.msgVcrSnapshotNotFound())
77+
?: throw Selfie.system.fs.assertFailed(
78+
Selfie.system.mode.msgVcrSnapshotNotFound(call))
7779
var idx = 1
7880
val frames = mutableListOf<Pair<String, SnapshotValue>>()
7981
for ((key, value) in snapshot.facets) {
@@ -93,7 +95,7 @@ internal constructor(
9395
if (state is State.Read) {
9496
if (state.frames.size != state.framesReadSoFar()) {
9597
throw Selfie.system.fs.assertFailed(
96-
Selfie.system.mode.msgVcrUnread(state.frames.size, state.framesReadSoFar()))
98+
Selfie.system.mode.msgVcrUnread(state.frames.size, state.framesReadSoFar(), call))
9799
}
98100
} else {
99101
disk.writeDisk((state as State.Write).closeAndGetSnapshot(), sub, call)
@@ -104,12 +106,12 @@ internal constructor(
104106
val fs = Selfie.system.fs
105107
val currentFrame = state.currentFrameThenAdvance()
106108
if (state.frames.size <= currentFrame) {
107-
throw fs.assertFailed(mode.msgVcrUnderflow(state.frames.size))
109+
throw fs.assertFailed(mode.msgVcrUnderflow(state.frames.size, call), call)
108110
}
109111
val expected = state.frames[currentFrame]
110112
if (expected.first != key) {
111113
throw fs.assertFailed(
112-
mode.msgVcrMismatch("$sub[$OPEN${currentFrame}$CLOSE]", expected.first, key),
114+
mode.msgVcrMismatch("$sub[$OPEN${currentFrame}$CLOSE]", expected.first, key, call),
113115
expected.first,
114116
key)
115117
}

0 commit comments

Comments
 (0)