diff --git a/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/Selfie.kt b/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/Selfie.kt index 993ec0a5..a0f1430d 100644 --- a/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/Selfie.kt +++ b/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/Selfie.kt @@ -18,78 +18,88 @@ package com.diffplug.selfie import com.diffplug.selfie.junit5.Router import org.opentest4j.AssertionFailedError -/** - * Sometimes a selfie is environment-specific, but should not be deleted when run in a different - * environment. - */ -fun preserveSelfiesOnDisk(vararg subsToKeep: String): Unit { - if (subsToKeep.isEmpty()) { - Router.readOrWriteOrKeep(null, null) - } else { - for (sub in subsToKeep) { - Router.readOrWriteOrKeep(null, sub) +object Selfie { + /** + * Sometimes a selfie is environment-specific, but should not be deleted when run in a different + * environment. + */ + @JvmStatic + fun preserveSelfiesOnDisk(vararg subsToKeep: String): Unit { + if (subsToKeep.isEmpty()) { + Router.readOrWriteOrKeep(null, null) + } else { + for (sub in subsToKeep) { + Router.readOrWriteOrKeep(null, sub) + } } } -} -open class DiskSelfie internal constructor(private val actual: Snapshot) { - fun toMatchDisk(sub: String = ""): Snapshot { - val onDisk = Router.readOrWriteOrKeep(actual, sub) - if (RW.isWrite) return actual - else if (onDisk == null) throw AssertionFailedError("No such snapshot") - else if (actual.value != onDisk.value) - throw AssertionFailedError("Snapshot failure", onDisk.value, actual.value) - else if (actual.lenses.keys != onDisk.lenses.keys) - throw AssertionFailedError( - "Snapshot failure: mismatched lenses", onDisk.lenses.keys, actual.lenses.keys) - for (key in actual.lenses.keys) { - val actualValue = actual.lenses[key]!! - val onDiskValue = onDisk.lenses[key]!! - if (actualValue != onDiskValue) { - throw AssertionFailedError("Snapshot failure within lens $key", onDiskValue, actualValue) + open class DiskSelfie internal constructor(private val actual: Snapshot) { + fun toMatchDisk(sub: String = ""): Snapshot { + val onDisk = Router.readOrWriteOrKeep(actual, sub) + if (RW.isWrite) return actual + else if (onDisk == null) throw AssertionFailedError("No such snapshot") + else if (actual.value != onDisk.value) + throw AssertionFailedError("Snapshot failure", onDisk.value, actual.value) + else if (actual.lenses.keys != onDisk.lenses.keys) + throw AssertionFailedError( + "Snapshot failure: mismatched lenses", onDisk.lenses.keys, actual.lenses.keys) + for (key in actual.lenses.keys) { + val actualValue = actual.lenses[key]!! + val onDiskValue = onDisk.lenses[key]!! + if (actualValue != onDiskValue) { + throw AssertionFailedError("Snapshot failure within lens $key", onDiskValue, actualValue) + } } + // if we're in read mode and the equality checks passed, stick with the disk value + return onDisk } - // if we're in read mode and the equality checks passed, stick with the disk value - return onDisk } -} -fun expectSelfie(actual: T, snapshotter: Snapshotter) = - DiskSelfie(snapshotter.snapshot(actual)) -class StringSelfie(private val actual: String) : DiskSelfie(Snapshot.of(actual)) { - fun toBe(expected: String): String = TODO() - fun toBe_TODO(): String = TODO() -} -fun expectSelfie(actual: String) = StringSelfie(actual) + @JvmStatic + fun expectSelfie(actual: T, snapshotter: Snapshotter) = + DiskSelfie(snapshotter.snapshot(actual)) -class BinarySelfie(private val actual: ByteArray) : DiskSelfie(Snapshot.of(actual)) { - fun toBeBase64(expected: String): ByteArray = TODO() - fun toBeBase64_TODO(): ByteArray = TODO() -} -fun expectSelfie(actual: ByteArray) = BinarySelfie(actual) + class StringSelfie(private val actual: String) : DiskSelfie(Snapshot.of(actual)) { + fun toBe(expected: String): String = TODO() + fun toBe_TODO(): String = TODO() + } -class IntSelfie(private val actual: Int) { - fun toBe(expected: Int): Int = TODO() - fun toBe_TODO(): Int = TODO() -} -fun expectSelfie(actual: Int) = IntSelfie(actual) + @JvmStatic fun expectSelfie(actual: String) = StringSelfie(actual) -class LongSelfie(private val actual: Long) { - fun toBe(expected: Long): Long = TODO() - fun toBe_TODO(): Long = TODO() -} -fun expectSelfie(actual: Long) = LongSelfie(actual) + class BinarySelfie(private val actual: ByteArray) : DiskSelfie(Snapshot.of(actual)) { + fun toBeBase64(expected: String): ByteArray = TODO() + fun toBeBase64_TODO(): ByteArray = TODO() + } -class BooleanSelfie(private val actual: Boolean) { - fun toBe(expected: Boolean): Boolean = TODO() - fun toBe_TODO(): Boolean = TODO() -} -fun expectSelfie(actual: Boolean) = BooleanSelfie(actual) + @JvmStatic fun expectSelfie(actual: ByteArray) = BinarySelfie(actual) -// infix versions for the inline methods, consistent with Kotest's API -infix fun String.shouldBeSelfie(expected: String): String = expectSelfie(this).toBe(expected) -infix fun ByteArray.shouldBeSelfieBase64(expected: String): ByteArray = - expectSelfie(this).toBeBase64(expected) -infix fun Int.shouldBeSelfie(expected: Int): Int = expectSelfie(this).toBe(expected) -infix fun Long.shouldBeSelfie(expected: Long): Long = expectSelfie(this).toBe(expected) -infix fun Boolean.shouldBeSelfie(expected: Boolean): Boolean = expectSelfie(this).toBe(expected) + class IntSelfie(private val actual: Int) { + fun toBe(expected: Int): Int = TODO() + fun toBe_TODO(): Int = TODO() + } + + @JvmStatic fun expectSelfie(actual: Int) = IntSelfie(actual) + + class LongSelfie(private val actual: Long) { + fun toBe(expected: Long): Long = TODO() + fun toBe_TODO(): Long = TODO() + } + + @JvmStatic fun expectSelfie(actual: Long) = LongSelfie(actual) + + class BooleanSelfie(private val actual: Boolean) { + fun toBe(expected: Boolean): Boolean = TODO() + fun toBe_TODO(): Boolean = TODO() + } + + @JvmStatic fun expectSelfie(actual: Boolean) = BooleanSelfie(actual) + + // infix versions for the inline methods, consistent with Kotest's API + infix fun String.shouldBeSelfie(expected: String): String = expectSelfie(this).toBe(expected) + infix fun ByteArray.shouldBeSelfieBase64(expected: String): ByteArray = + expectSelfie(this).toBeBase64(expected) + infix fun Int.shouldBeSelfie(expected: Int): Int = expectSelfie(this).toBe(expected) + infix fun Long.shouldBeSelfie(expected: Long): Long = expectSelfie(this).toBe(expected) + infix fun Boolean.shouldBeSelfie(expected: Boolean): Boolean = expectSelfie(this).toBe(expected) +} diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_CarriageReturnTest.kt b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_CarriageReturnTest.kt index aa434dea..c6490392 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_CarriageReturnTest.kt +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_CarriageReturnTest.kt @@ -1,6 +1,6 @@ package undertest.junit5 -import com.diffplug.selfie.expectSelfie +import com.diffplug.selfie.Selfie.expectSelfie import kotlin.test.Test class UT_CarriageReturnTest { diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_DuplicateWriteTest.kt b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_DuplicateWriteTest.kt index 7a734b3a..8738700e 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_DuplicateWriteTest.kt +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_DuplicateWriteTest.kt @@ -1,6 +1,6 @@ package undertest.junit5 -import com.diffplug.selfie.expectSelfie +import com.diffplug.selfie.Selfie.expectSelfie import org.junit.jupiter.api.Test class UT_DuplicateWriteTest { diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_MethodLevelGCTest.kt b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_MethodLevelGCTest.kt index ed22e9ef..dbf08566 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_MethodLevelGCTest.kt +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_MethodLevelGCTest.kt @@ -1,6 +1,6 @@ package undertest.junit5 // spotless:off -import com.diffplug.selfie.expectSelfie +import com.diffplug.selfie.Selfie.expectSelfie import kotlin.test.Test // spotless:on diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_ReadWriteTest.kt b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_ReadWriteTest.kt index 34251085..69d17441 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_ReadWriteTest.kt +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_ReadWriteTest.kt @@ -1,6 +1,6 @@ package undertest.junit5 -import com.diffplug.selfie.expectSelfie +import com.diffplug.selfie.Selfie.expectSelfie import kotlin.test.Test class UT_ReadWriteTest { diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt index 9a574547..0659bd88 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/UT_WithinMethodGCTest.kt @@ -1,6 +1,6 @@ package undertest.junit5 // spotless:off -import com.diffplug.selfie.expectSelfie +import com.diffplug.selfie.Selfie.expectSelfie import kotlin.test.Test // spotless:on