Skip to content

Commit 6968913

Browse files
committed
Improve the VcrSelfie error messages.
1 parent f5ed9dd commit 6968913

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023-2024 DiffPlug
2+
* Copyright (C) 2023-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,9 +44,11 @@ enum class Mode {
4444
internal fun msgSnapshotNotFoundNoSuchFile(file: TypedPath) =
4545
msg("Snapshot not found: no such file $file")
4646
internal fun msgSnapshotMismatch(expected: String, actual: String) =
47-
msg(SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
47+
msg("Snapshot " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
4848
internal fun msgSnapshotMismatchBinary(expected: ByteArray, actual: ByteArray) =
4949
msgSnapshotMismatch(expected.toQuotedPrintable(), actual.toQuotedPrintable())
50+
internal fun msgVcrKeyMismatch(key: String, expected: String, actual: String) =
51+
msg("VCR key $key " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
5052
private fun ByteArray.toQuotedPrintable(): String {
5153
val sb = StringBuilder()
5254
for (byte in this) {
@@ -63,7 +65,9 @@ enum class Mode {
6365
when (this) {
6466
interactive ->
6567
"$headline\n" +
66-
"‣ update this snapshot by adding `_TODO` to the function name\n" +
68+
(if (headline.startsWith("Snapshot "))
69+
"‣ update this snapshot by adding `_TODO` to the function name\n"
70+
else "") +
6771
"‣ update all snapshots in this file by adding `//selfieonce` or `//SELFIEWRITE`"
6872
readonly -> headline
6973
overwrite -> "$headline\n(didn't expect this to ever happen in overwrite mode)"

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ class VcrSelfie(
7070
disk.writeDisk(snapshot, sub, call)
7171
}
7272
}
73+
private fun keyMismatch(expected: String, actual: String): Throwable =
74+
Selfie.system.fs.assertFailed(
75+
Selfie.system.mode.msgVcrKeyMismatch("$sub[$OPEN${state.count}$CLOSE]", expected, actual),
76+
expected,
77+
actual)
7378
fun <V> next(key: String, roundtripValue: Roundtrip<V, String>, value: Cacheable<V>): V {
7479
if (state.readMode) {
7580
val expected = state.sequence[state.count++]
7681
if (expected.first != key) {
77-
throw Selfie.system.fs.assertFailed(
78-
"vcr key mismatch at index ${state.count - 1}", expected.first, key)
82+
throw keyMismatch(expected.first, key)
7983
}
8084
return roundtripValue.parse(expected.second.valueString())
8185
} else {
@@ -87,13 +91,11 @@ class VcrSelfie(
8791
fun next(key: String, value: Cacheable<String>): String = next(key, Roundtrip.identity(), value)
8892
inline fun <reified V> nextJson(key: String, value: Cacheable<V>): V =
8993
next(key, RoundtripJson.of<V>(), value)
90-
9194
fun <V> nextBinary(key: String, roundtripValue: Roundtrip<V, ByteArray>, value: Cacheable<V>): V {
9295
if (state.readMode) {
9396
val expected = state.sequence[state.count++]
9497
if (expected.first != key) {
95-
throw Selfie.system.fs.assertFailed(
96-
"vcr key mismatch at index ${state.count - 1}", expected.first, key)
98+
throw keyMismatch(expected.first, key)
9799
}
98100
return roundtripValue.parse(expected.second.valueBinary())
99101
} else {
@@ -102,7 +104,6 @@ class VcrSelfie(
102104
return value
103105
}
104106
}
105-
106-
fun <V> nextBinary(key: String, value: Cacheable<ByteArray>): ByteArray
107-
= nextBinary(key, Roundtrip.identity(), value)
107+
fun <V> nextBinary(key: String, value: Cacheable<ByteArray>): ByteArray =
108+
nextBinary(key, Roundtrip.identity(), value)
108109
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 DiffPlug
2+
* Copyright (C) 2024-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ object SnapshotNotEqualErrorMsg {
3232
actual.indexOf('\n', index).let { if (it == -1) actual.length else it }
3333
val expectedLine = expected.substring(index - columnNumber + 1, endOfLineExpected)
3434
val actualLine = actual.substring(index - columnNumber + 1, endOfLineActual)
35-
return "Snapshot mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine"
35+
return "mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine"
3636
}
3737
if (expectedChar == '\n') {
3838
lineNumber++
@@ -53,11 +53,11 @@ object SnapshotNotEqualErrorMsg {
5353
val endIdx =
5454
longer.indexOf('\n', endOfLineActual + 1).let { if (it == -1) longer.length else it }
5555
val line = longer.substring(endOfLineActual + 1, endIdx)
56-
return "Snapshot mismatch at L${lineNumber+1}:C1 - line(s) ${if (added == "+") "added" else "removed"}\n${added}$line"
56+
return "mismatch at L${lineNumber+1}:C1 - line(s) ${if (added == "+") "added" else "removed"}\n${added}$line"
5757
} else {
5858
val expectedLine = expected.substring(index - columnNumber + 1, endOfLineExpected)
5959
val actualLine = actual.substring(index - columnNumber + 1, endOfLineActual)
60-
return "Snapshot mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine"
60+
return "mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine"
6161
}
6262
}
6363
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 DiffPlug
2+
* Copyright (C) 2024-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,76 +22,76 @@ class SnapshotNotEqualErrorMsgTest {
2222
@Test
2323
fun errorLine1() {
2424
SnapshotNotEqualErrorMsg.forUnequalStrings("Testing 123", "Testing ABC") shouldBe
25-
"""Snapshot mismatch at L1:C9
25+
"""mismatch at L1:C9
2626
-Testing 123
2727
+Testing ABC"""
2828

2929
SnapshotNotEqualErrorMsg.forUnequalStrings("123 Testing", "ABC Testing") shouldBe
30-
"""Snapshot mismatch at L1:C1
30+
"""mismatch at L1:C1
3131
-123 Testing
3232
+ABC Testing"""
3333
}
3434

3535
@Test
3636
fun errorLine2() {
3737
SnapshotNotEqualErrorMsg.forUnequalStrings("Line\nTesting 123", "Line\nTesting ABC") shouldBe
38-
"""Snapshot mismatch at L2:C9
38+
"""mismatch at L2:C9
3939
-Testing 123
4040
+Testing ABC"""
4141

4242
SnapshotNotEqualErrorMsg.forUnequalStrings("Line\n123 Testing", "Line\nABC Testing") shouldBe
43-
"""Snapshot mismatch at L2:C1
43+
"""mismatch at L2:C1
4444
-123 Testing
4545
+ABC Testing"""
4646
}
4747

4848
@Test
4949
fun extraLine1() {
5050
SnapshotNotEqualErrorMsg.forUnequalStrings("123", "123ABC") shouldBe
51-
"""Snapshot mismatch at L1:C4
51+
"""mismatch at L1:C4
5252
-123
5353
+123ABC"""
5454
SnapshotNotEqualErrorMsg.forUnequalStrings("123ABC", "123") shouldBe
55-
"""Snapshot mismatch at L1:C4
55+
"""mismatch at L1:C4
5656
-123ABC
5757
+123"""
5858
}
5959

6060
@Test
6161
fun extraLine2() {
6262
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n123", "line\n123ABC") shouldBe
63-
"""Snapshot mismatch at L2:C4
63+
"""mismatch at L2:C4
6464
-123
6565
+123ABC"""
6666
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n123ABC", "line\n123") shouldBe
67-
"""Snapshot mismatch at L2:C4
67+
"""mismatch at L2:C4
6868
-123ABC
6969
+123"""
7070
}
7171

7272
@Test
7373
fun extraLine() {
7474
SnapshotNotEqualErrorMsg.forUnequalStrings("line", "line\nnext") shouldBe
75-
"""Snapshot mismatch at L2:C1 - line(s) added
75+
"""mismatch at L2:C1 - line(s) added
7676
+next"""
7777
SnapshotNotEqualErrorMsg.forUnequalStrings("line\nnext", "line") shouldBe
78-
"""Snapshot mismatch at L2:C1 - line(s) removed
78+
"""mismatch at L2:C1 - line(s) removed
7979
-next"""
8080
}
8181

8282
@Test
8383
fun extraNewline() {
8484
SnapshotNotEqualErrorMsg.forUnequalStrings("line", "line\n") shouldBe
85-
"""Snapshot mismatch at L2:C1 - line(s) added
85+
"""mismatch at L2:C1 - line(s) added
8686
+"""
8787
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n", "line") shouldBe
88-
"""Snapshot mismatch at L2:C1 - line(s) removed
88+
"""mismatch at L2:C1 - line(s) removed
8989
-"""
9090
SnapshotNotEqualErrorMsg.forUnequalStrings("", "\n") shouldBe
91-
"""Snapshot mismatch at L2:C1 - line(s) added
91+
"""mismatch at L2:C1 - line(s) added
9292
+"""
9393
SnapshotNotEqualErrorMsg.forUnequalStrings("\n", "") shouldBe
94-
"""Snapshot mismatch at L2:C1 - line(s) removed
94+
"""mismatch at L2:C1 - line(s) removed
9595
-"""
9696
}
9797
}

0 commit comments

Comments
 (0)