Skip to content

Commit

Permalink
A provisional API to implement memoizing of regular and suspending fu…
Browse files Browse the repository at this point in the history
…nctions.
  • Loading branch information
nedtwigg committed Feb 9, 2024
1 parent 7783dd9 commit 15de0ee
Showing 1 changed file with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.diffplug.selfie

import com.diffplug.selfie.guts.DiskSnapshotTodo
import com.diffplug.selfie.guts.DiskStorage
import com.diffplug.selfie.guts.LiteralFormat
import com.diffplug.selfie.guts.LiteralString
import com.diffplug.selfie.guts.LiteralValue
import com.diffplug.selfie.guts.SnapshotSystem
import com.diffplug.selfie.guts.recordCall
import kotlin.jvm.JvmOverloads

class StringMemo(val disk: DiskStorage, val generator: () -> String) {
@JvmOverloads
fun toMatchDisk(sub: String = "") : String {
val call = recordCall(false)
if (Selfie.system.mode.canWrite(false, call, Selfie.system)) {
val actual = generator()
disk.writeDisk(Snapshot.of(actual), sub, call)
return actual
} else {
val snapshot = disk.readDisk(sub, call)
?: throw Selfie.system.fs.assertFailed(Selfie.system.mode.msgSnapshotNotFound())
if (snapshot.subject.isBinary || snapshot.facets.isNotEmpty()) {
throw Selfie.system.fs.assertFailed("Expected a string subject with no facets, got ${snapshot}")
}
return snapshot.subject.valueString()
}
}
@JvmOverloads
fun toMatchDisk_TODO(sub: String = "") : String {
val call = recordCall(false)
if (Selfie.system.mode.canWrite(true, call, Selfie.system)) {
val actual = generator()
disk.writeDisk(Snapshot.of(actual), sub, call)
Selfie.system.writeInline(DiskSnapshotTodo.createLiteral(), call)
return actual
} else {
throw Selfie.system.fs.assertFailed("Can't call `toMatchDisk_TODO` in ${Mode.readonly} mode!")
}
}
@JvmOverloads
fun toBe_TODO(unusedArg: Any? = null) : String {
val call = recordCall(false)
val writable = Selfie.system.mode.canWrite(true, call, Selfie.system)
if (writable) {
val actual = generator()
Selfie.system.writeInline(LiteralValue(null, actual, LiteralString), call)
return actual
} else {
throw Selfie.system.fs.assertFailed("Can't call `toBe_TODO` in ${Mode.readonly} mode!")
}
}
fun toBe(expected: String) : String = expected
}
class StringMemoSuspend(val disk: DiskStorage, val generator: suspend () -> String) {
suspend fun toMatchDisk(sub: String = "") : String {
val call = recordCall(false)
if (Selfie.system.mode.canWrite(false, call, Selfie.system)) {
val actual = generator()
disk.writeDisk(Snapshot.of(actual), sub, call)
return actual
} else {
val snapshot = disk.readDisk(sub, call)
?: throw Selfie.system.fs.assertFailed(Selfie.system.mode.msgSnapshotNotFound())
if (snapshot.subject.isBinary || snapshot.facets.isNotEmpty()) {
throw Selfie.system.fs.assertFailed("Expected a string subject with no facets, got ${snapshot}")
}
return snapshot.subject.valueString()
}
}
suspend fun toMatchDisk_TODO(sub: String = "") : String {
val call = recordCall(false)
if (Selfie.system.mode.canWrite(true, call, Selfie.system)) {
val actual = generator()
disk.writeDisk(Snapshot.of(actual), sub, call)
Selfie.system.writeInline(DiskSnapshotTodo.createLiteral(), call)
return actual
} else {
throw Selfie.system.fs.assertFailed("Can't call `toMatchDisk_TODO` in ${Mode.readonly} mode!")
}
}
suspend fun toBe_TODO(unusedArg: Any? = null) : String {
val call = recordCall(false)
val writable = Selfie.system.mode.canWrite(true, call, Selfie.system)
if (writable) {
val actual = generator()
Selfie.system.writeInline(LiteralValue(null, actual, LiteralString), call)
return actual
} else {
throw Selfie.system.fs.assertFailed("Can't call `toBe_TODO` in ${Mode.readonly} mode!")
}
}
fun toBe(expected: String) : String = expected
}

0 comments on commit 15de0ee

Please sign in to comment.