-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
213 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/guts/SnapshotNotEqualErrorMsg.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (C) 2024 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.selfie.guts | ||
|
||
object SnapshotNotEqualErrorMsg { | ||
const val REASONABLE_LINE_LENGTH = 120 | ||
fun forUnequalStrings(expected: String, actual: String): String { | ||
var lineNumber = 1 | ||
var columnNumber = 1 | ||
var index = 0 | ||
|
||
while (index < expected.length && index < actual.length) { | ||
val expectedChar = expected[index] | ||
val actualChar = actual[index] | ||
if (expectedChar != actualChar) { | ||
val endOfLineExpected = | ||
expected.indexOf('\n', index).let { if (it == -1) expected.length else it } | ||
val endOfLineActual = | ||
actual.indexOf('\n', index).let { if (it == -1) actual.length else it } | ||
val expectedLine = expected.substring(index - columnNumber + 1, endOfLineExpected) | ||
val actualLine = actual.substring(index - columnNumber + 1, endOfLineActual) | ||
return "Snapshot mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine" | ||
} | ||
if (expectedChar == '\n') { | ||
lineNumber++ | ||
columnNumber = 1 | ||
} else { | ||
columnNumber++ | ||
} | ||
index++ | ||
} | ||
val endOfLineExpected = | ||
expected.indexOf('\n', index).let { if (it == -1) expected.length else it } | ||
val endOfLineActual = actual.indexOf('\n', index).let { if (it == -1) actual.length else it } | ||
|
||
if (endOfLineActual == endOfLineExpected) { | ||
// it ended at a line break | ||
val longer = if (actual.length > expected.length) actual else expected | ||
val added = if (actual.length > expected.length) "+" else "-" | ||
val endIdx = | ||
longer.indexOf('\n', endOfLineActual + 1).let { if (it == -1) longer.length else it } | ||
val line = longer.substring(endOfLineActual + 1, endIdx) | ||
return "Snapshot mismatch at L${lineNumber+1}:C1 - line(s) ${if (added == "+") "added" else "removed"}\n${added}$line" | ||
} else { | ||
val expectedLine = expected.substring(index - columnNumber + 1, endOfLineExpected) | ||
val actualLine = actual.substring(index - columnNumber + 1, endOfLineActual) | ||
return "Snapshot mismatch at L$lineNumber:C$columnNumber\n-$expectedLine\n+$actualLine" | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/guts/SnapshotNotEqualErrorMsgTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2024 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.selfie.guts | ||
|
||
import io.kotest.matchers.shouldBe | ||
import kotlin.test.Test | ||
|
||
class SnapshotNotEqualErrorMsgTest { | ||
@Test | ||
fun errorLine1() { | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("Testing 123", "Testing ABC") shouldBe | ||
"""Snapshot mismatch at L1:C9 | ||
-Testing 123 | ||
+Testing ABC""" | ||
|
||
SnapshotNotEqualErrorMsg.forUnequalStrings("123 Testing", "ABC Testing") shouldBe | ||
"""Snapshot mismatch at L1:C1 | ||
-123 Testing | ||
+ABC Testing""" | ||
} | ||
|
||
@Test | ||
fun errorLine2() { | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("Line\nTesting 123", "Line\nTesting ABC") shouldBe | ||
"""Snapshot mismatch at L2:C9 | ||
-Testing 123 | ||
+Testing ABC""" | ||
|
||
SnapshotNotEqualErrorMsg.forUnequalStrings("Line\n123 Testing", "Line\nABC Testing") shouldBe | ||
"""Snapshot mismatch at L2:C1 | ||
-123 Testing | ||
+ABC Testing""" | ||
} | ||
|
||
@Test | ||
fun extraLine1() { | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("123", "123ABC") shouldBe | ||
"""Snapshot mismatch at L1:C4 | ||
-123 | ||
+123ABC""" | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("123ABC", "123") shouldBe | ||
"""Snapshot mismatch at L1:C4 | ||
-123ABC | ||
+123""" | ||
} | ||
|
||
@Test | ||
fun extraLine2() { | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n123", "line\n123ABC") shouldBe | ||
"""Snapshot mismatch at L2:C4 | ||
-123 | ||
+123ABC""" | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n123ABC", "line\n123") shouldBe | ||
"""Snapshot mismatch at L2:C4 | ||
-123ABC | ||
+123""" | ||
} | ||
|
||
@Test | ||
fun extraLine() { | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("line", "line\nnext") shouldBe | ||
"""Snapshot mismatch at L2:C1 - line(s) added | ||
+next""" | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("line\nnext", "line") shouldBe | ||
"""Snapshot mismatch at L2:C1 - line(s) removed | ||
-next""" | ||
} | ||
|
||
@Test | ||
fun extraNewline() { | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("line", "line\n") shouldBe | ||
"""Snapshot mismatch at L2:C1 - line(s) added | ||
+""" | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("line\n", "line") shouldBe | ||
"""Snapshot mismatch at L2:C1 - line(s) removed | ||
-""" | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("", "\n") shouldBe | ||
"""Snapshot mismatch at L2:C1 - line(s) added | ||
+""" | ||
SnapshotNotEqualErrorMsg.forUnequalStrings("\n", "") shouldBe | ||
"""Snapshot mismatch at L2:C1 - line(s) removed | ||
-""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters