Skip to content

Commit

Permalink
Abstract the error thrown into SnapshotStorage to prepare for KMP.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Dec 23, 2023
1 parent 2e940f1 commit 7de1410
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package com.diffplug.selfie

import com.diffplug.selfie.junit5.SnapshotStorageJUnit5
import java.util.Map.entry
import org.opentest4j.AssertionFailedError

/** NOT FOR ENDUSERS. Implemented by Selfie to integrate with various test frameworks. */
interface SnapshotStorage {
Expand All @@ -32,6 +31,8 @@ interface SnapshotStorage {
* currently executing class.
*/
fun keep(subOrKeepAll: String?)
/** Creates an assertion failed exception to throw. */
fun assertFailed(message: String, expected: Any? = null, actual: Any? = null): Error
}

object Selfie {
Expand All @@ -55,7 +56,7 @@ object Selfie {
fun toMatchDisk(sub: String = ""): Snapshot {
val comparison = storage.readWriteDisk(actual, sub)
if (!SnapshotStorageJUnit5.isWrite) {
comparison.assertEqual()
comparison.assertEqual(storage)
}
return comparison.actual
}
Expand Down Expand Up @@ -124,10 +125,10 @@ object Selfie {
return actual
} else {
if (expected == null) {
throw AssertionFailedError(
throw storage.assertFailed(
"`.toBe_TODO()` was called in `read` mode, try again with selfie in write mode")
} else {
throw AssertionFailedError(
throw storage.assertFailed(
"Inline literal did not match the actual value", expected, actual)
}
}
Expand Down Expand Up @@ -165,9 +166,9 @@ object Selfie {
}

class ExpectedActual(val expected: Snapshot?, val actual: Snapshot) {
fun assertEqual() {
internal fun assertEqual(storage: SnapshotStorage) {
if (expected == null) {
throw AssertionFailedError("No such snapshot")
throw storage.assertFailed("No such snapshot")
} else if (expected.subject == actual.subject && expected.facets == actual.facets) {
return
} else {
Expand All @@ -191,7 +192,7 @@ class ExpectedActual(val expected: Snapshot?, val actual: Snapshot) {
}
}
val includeRoot = mismatchInExpected.containsKey("")
throw AssertionFailedError(
throw storage.assertFailed(
"Snapshot failure",
serializeMultiple(Snapshot.ofEntries(mismatchInExpected.entries), !includeRoot),
serializeMultiple(Snapshot.ofEntries(mismatchInActual.entries), !includeRoot))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.junit.platform.engine.support.descriptor.MethodSource
import org.junit.platform.launcher.TestExecutionListener
import org.junit.platform.launcher.TestIdentifier
import org.junit.platform.launcher.TestPlan
import org.opentest4j.AssertionFailedError

/** Routes between `toMatchDisk()` calls and the snapshot file / pruning machinery. */
internal object SnapshotStorageJUnit5 : SnapshotStorage {
Expand Down Expand Up @@ -60,6 +61,9 @@ internal object SnapshotStorageJUnit5 : SnapshotStorage {
cm.clazz.keep(cm.method, suffix(subOrKeepAll))
}
}
override fun assertFailed(message: String, expected: Any?, actual: Any?): Error =
if (expected == null && actual == null) AssertionFailedError(message)
else AssertionFailedError(message, expected, actual)
override fun writeInline(literalValue: LiteralValue<*>) {
val call = recordCall()
val cm =
Expand Down

0 comments on commit 7de1410

Please sign in to comment.