Skip to content

Commit 18804e8

Browse files
committed
Give a single-line diff for the line which has the mismatch.
1 parent c5dd020 commit 18804e8

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.selfie.guts
17+
18+
object SnapshotNotEqualErrorMsg {
19+
const val REASONABLE_LINE_LENGTH = 120
20+
fun forUnequalStrings(expected: String, actual: String): String {
21+
var lineNumber = 1
22+
var columnNumber = 1
23+
var index = 0
24+
25+
while (index < expected.length && index < actual.length) {
26+
val expectedChar = expected[index]
27+
val actualChar = actual[index]
28+
if (expectedChar != actualChar) {
29+
val endOfLineExpected =
30+
expected.indexOf('\n', index).let { if (it == -1) expected.length else it }
31+
val endOfLineActual =
32+
actual.indexOf('\n', index).let { if (it == -1) actual.length else it }
33+
val expectedLine = expected.substring(index - columnNumber + 1, endOfLineExpected)
34+
val actualLine = actual.substring(index - columnNumber + 1, endOfLineActual)
35+
return "Snapshot mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine"
36+
}
37+
if (expectedChar == '\n') {
38+
lineNumber++
39+
columnNumber = 1
40+
} else {
41+
columnNumber++
42+
}
43+
index++
44+
}
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"
49+
}
50+
throw IllegalArgumentException("The strings were supposed to be unequal")
51+
}
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.selfie.guts
17+
18+
import io.kotest.matchers.shouldBe
19+
import kotlin.test.Test
20+
21+
class SnapshotNotEqualErrorMsgTest {
22+
@Test
23+
fun errorLine1() {
24+
SnapshotNotEqualErrorMsg.forUnequalStrings("Testing 123", "Testing ABC") shouldBe
25+
"""Snapshot mismatch at L1:C9
26+
-Testing 123
27+
+Testing ABC"""
28+
29+
SnapshotNotEqualErrorMsg.forUnequalStrings("123 Testing", "ABC Testing") shouldBe
30+
"""Snapshot mismatch at L1:C1
31+
-123 Testing
32+
+ABC Testing"""
33+
}
34+
35+
@Test
36+
fun errorLine2() {
37+
SnapshotNotEqualErrorMsg.forUnequalStrings("Line\nTesting 123", "Line\nTesting ABC") shouldBe
38+
"""Snapshot mismatch at L2:C9
39+
-Testing 123
40+
+Testing ABC"""
41+
42+
SnapshotNotEqualErrorMsg.forUnequalStrings("Line\n123 Testing", "Line\nABC Testing") shouldBe
43+
"""Snapshot mismatch at L2:C1
44+
-123 Testing
45+
+ABC Testing"""
46+
}
47+
}

0 commit comments

Comments
 (0)