|
| 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 | +} |
0 commit comments