Skip to content

Commit

Permalink
Very very beginnings of routing snapshot creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Aug 23, 2023
1 parent a8e6c13 commit a4ba0f3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,35 @@
*/
package com.diffplug.selfie

object SelfieRouting {
var isWriting: Boolean = true
var currentFile: SnapshotFile? = null
var currentDiskPrefix: String? = null
private fun assertInitializedProperly() {
if (currentFile == null || currentDiskPrefix == null) {
throw AssertionError("You called `toMatchDisk` without setting up snapshots.")
}
}
fun onDiskRightNow(scenario: String?): Snapshot? {
assertInitializedProperly()
val snapshotSuffix = scenario?.let { "/$scenario" } ?: ""
val snapshotName = "${currentDiskPrefix!!}${snapshotSuffix}"
return currentFile!!.snapshots.get(snapshotName)
}
}

open class DiskSelfie internal constructor(private val actual: Snapshot) {
fun toMatchDisk(scenario: String = ""): Snapshot = TODO()
fun toMatchDisk(scenario: String? = null): Snapshot {
val snapshot = SelfieRouting.onDiskRightNow(scenario)
if (actual != snapshot) {
if (SelfieRouting.isWriting) {
TODO("write snapshot")
} else {
throw AssertionError()
}
}
return actual
}
}
fun <T> expectSelfie(actual: T, snapshotter: Snapshotter<T>) =
DiskSelfie(snapshotter.snapshot(actual))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@
*/
package com.diffplug.selfie.junit5

import com.diffplug.selfie.SelfieRouting
import com.diffplug.selfie.SnapshotFile
import org.junit.jupiter.api.extension.AfterAllCallback
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext

class SelfieExtension : BeforeAllCallback, AfterAllCallback, BeforeEachCallback {
override fun beforeAll(context: ExtensionContext) {
println("beforeAll ${context.displayName}")
// TOOD: load the selfie file if it exists
// TODO: create the selfie metadata if necessary
SelfieRouting.currentFile = SnapshotFile()
}
override fun beforeEach(context: ExtensionContext) {
println("beforeEach ${context.displayName}")
SelfieRouting.currentDiskPrefix = context.testMethod.get().name
}
override fun afterAll(context: ExtensionContext) {
println("afterAll ${context.displayName}")
// TODO: test/prune orphan snapshots
SelfieRouting.currentDiskPrefix = null
SelfieRouting.currentFile = null
}
}

0 comments on commit a4ba0f3

Please sign in to comment.