Skip to content

Commit 6a73c21

Browse files
committed
Handle corner-cases.
1 parent 18804e8 commit 6a73c21

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,33 @@ object SnapshotNotEqualErrorMsg {
4242
}
4343
index++
4444
}
45-
if (expected.length != actual.length) {
46-
// val diffString = if (expected.length < actual.length) "Actual is longer" else
47-
// "Expected is longer"
48-
return "Snapshot mismatch at L$lineNumber:C$columnNumber"
45+
val endOfLineExpected =
46+
expected.indexOf('\n', index).let { if (it == -1) expected.length else it }
47+
val endOfLineActual = actual.indexOf('\n', index).let { if (it == -1) actual.length else it }
48+
49+
if (endOfLineActual == endOfLineExpected) {
50+
// it ended at a line break
51+
if (actual.length > expected.length) {
52+
val line =
53+
actual.let { str ->
54+
val endIdx =
55+
str.indexOf('\n', endOfLineActual + 1).let { if (it == -1) str.length else it }
56+
str.substring(endOfLineActual + 1, endIdx)
57+
}
58+
return "Snapshot mismatch at L${lineNumber+1}:C1 - line(s) added\n+$line"
59+
} else {
60+
val line =
61+
expected.let { str ->
62+
val endIdx =
63+
str.indexOf('\n', endOfLineActual + 1).let { if (it == -1) str.length else it }
64+
str.substring(endOfLineActual + 1, endIdx)
65+
}
66+
return "Snapshot mismatch at L${lineNumber+1}:C1 - line(s) removed\n-$line"
67+
}
68+
} else {
69+
val expectedLine = expected.substring(index - columnNumber + 1, endOfLineExpected)
70+
val actualLine = actual.substring(index - columnNumber + 1, endOfLineActual)
71+
return "Snapshot mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine"
4972
}
50-
throw IllegalArgumentException("The strings were supposed to be unequal")
5173
}
5274
}

jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/guts/SnapshotNotEqualErrorMsgTest.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,54 @@ class SnapshotNotEqualErrorMsgTest {
4444
-123 Testing
4545
+ABC Testing"""
4646
}
47+
48+
@Test
49+
fun extraLine1() {
50+
SnapshotNotEqualErrorMsg.forUnequalStrings("123", "123ABC") shouldBe
51+
"""Snapshot mismatch at L1:C4
52+
-123
53+
+123ABC"""
54+
SnapshotNotEqualErrorMsg.forUnequalStrings("123ABC", "123") shouldBe
55+
"""Snapshot mismatch at L1:C4
56+
-123ABC
57+
+123"""
58+
}
59+
60+
@Test
61+
fun extraLine2() {
62+
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n123", "line\n123ABC") shouldBe
63+
"""Snapshot mismatch at L2:C4
64+
-123
65+
+123ABC"""
66+
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n123ABC", "line\n123") shouldBe
67+
"""Snapshot mismatch at L2:C4
68+
-123ABC
69+
+123"""
70+
}
71+
72+
@Test
73+
fun extraLine() {
74+
SnapshotNotEqualErrorMsg.forUnequalStrings("line", "line\nnext") shouldBe
75+
"""Snapshot mismatch at L2:C1 - line(s) added
76+
+next"""
77+
SnapshotNotEqualErrorMsg.forUnequalStrings("line\nnext", "line") shouldBe
78+
"""Snapshot mismatch at L2:C1 - line(s) removed
79+
-next"""
80+
}
81+
82+
@Test
83+
fun extraNewline() {
84+
SnapshotNotEqualErrorMsg.forUnequalStrings("line", "line\n") shouldBe
85+
"""Snapshot mismatch at L2:C1 - line(s) added
86+
+"""
87+
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n", "line") shouldBe
88+
"""Snapshot mismatch at L2:C1 - line(s) removed
89+
-"""
90+
SnapshotNotEqualErrorMsg.forUnequalStrings("", "\n") shouldBe
91+
"""Snapshot mismatch at L2:C1 - line(s) added
92+
+"""
93+
SnapshotNotEqualErrorMsg.forUnequalStrings("\n", "") shouldBe
94+
"""Snapshot mismatch at L2:C1 - line(s) removed
95+
-"""
96+
}
4797
}

0 commit comments

Comments
 (0)