|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 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 |
| 17 | + |
| 18 | +import com.diffplug.selfie.guts.CallStack |
| 19 | +import com.diffplug.selfie.guts.DiskStorage |
| 20 | +import com.diffplug.selfie.guts.recordCall |
| 21 | + |
| 22 | +private const val OPEN = "«" |
| 23 | +private const val CLOSE = "»" |
| 24 | + |
| 25 | +class VcrSelfie |
| 26 | +internal constructor( |
| 27 | + private val sub: String, |
| 28 | + private val call: CallStack, |
| 29 | + private val disk: DiskStorage, |
| 30 | +) : AutoCloseable { |
| 31 | + class TestLocator internal constructor(private val sub: String, private val disk: DiskStorage) { |
| 32 | + private val call = recordCall(false) |
| 33 | + fun createVcr() = VcrSelfie(sub, call, disk) |
| 34 | + } |
| 35 | + |
| 36 | + private class State(val readMode: Boolean) { |
| 37 | + var currentFrame = 0 |
| 38 | + val frames = mutableListOf<Pair<String, SnapshotValue>>() |
| 39 | + } |
| 40 | + private val state: State |
| 41 | + |
| 42 | + init { |
| 43 | + val canWrite = Selfie.system.mode.canWrite(isTodo = false, call, Selfie.system) |
| 44 | + state = State(readMode = !canWrite) |
| 45 | + if (state.readMode) { |
| 46 | + val snapshot = |
| 47 | + disk.readDisk(sub, call) |
| 48 | + ?: throw Selfie.system.fs.assertFailed(Selfie.system.mode.msgSnapshotNotFound()) |
| 49 | + var idx = 1 |
| 50 | + for ((key, value) in snapshot.facets) { |
| 51 | + check(key.startsWith(OPEN)) |
| 52 | + val nextClose = key.indexOf(CLOSE) |
| 53 | + check(nextClose != -1) |
| 54 | + val num = key.substring(OPEN.length, nextClose).toInt() |
| 55 | + check(num == idx) |
| 56 | + ++idx |
| 57 | + val keyAfterNum = key.substring(nextClose + 1) |
| 58 | + state.frames.add(keyAfterNum to value) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + override fun close() { |
| 63 | + if (state.readMode) { |
| 64 | + if (state.frames.size != state.currentFrame) { |
| 65 | + throw Selfie.system.fs.assertFailed( |
| 66 | + Selfie.system.mode.msgVcrUnread(state.frames.size, state.currentFrame)) |
| 67 | + } |
| 68 | + } else { |
| 69 | + var snapshot = Snapshot.of("") |
| 70 | + var idx = 1 |
| 71 | + for ((key, value) in state.frames) { |
| 72 | + snapshot = snapshot.plusFacet("$OPEN$idx$CLOSE$key", value) |
| 73 | + } |
| 74 | + disk.writeDisk(snapshot, sub, call) |
| 75 | + } |
| 76 | + } |
| 77 | + private fun nextFrameValue(key: String): SnapshotValue { |
| 78 | + val mode = Selfie.system.mode |
| 79 | + val fs = Selfie.system.fs |
| 80 | + if (state.frames.size <= state.currentFrame) { |
| 81 | + throw fs.assertFailed(mode.msgVcrUnderflow(state.frames.size)) |
| 82 | + } |
| 83 | + val expected = state.frames[state.currentFrame++] |
| 84 | + if (expected.first != key) { |
| 85 | + throw fs.assertFailed( |
| 86 | + mode.msgVcrMismatch("$sub[$OPEN${state.currentFrame}$CLOSE]", expected.first, key), |
| 87 | + expected.first, |
| 88 | + key) |
| 89 | + } |
| 90 | + return expected.second |
| 91 | + } |
| 92 | + fun <V> nextFrame(key: String, roundtripValue: Roundtrip<V, String>, value: Cacheable<V>): V { |
| 93 | + if (state.readMode) { |
| 94 | + return roundtripValue.parse(nextFrameValue(key).valueString()) |
| 95 | + } else { |
| 96 | + val value = value.get() |
| 97 | + state.frames.add(key to SnapshotValue.of(roundtripValue.serialize(value))) |
| 98 | + return value |
| 99 | + } |
| 100 | + } |
| 101 | + fun nextFrame(key: String, value: Cacheable<String>): String = |
| 102 | + nextFrame(key, Roundtrip.identity(), value) |
| 103 | + inline fun <reified V> nextFrameJson(key: String, value: Cacheable<V>): V = |
| 104 | + nextFrame(key, RoundtripJson.of<V>(), value) |
| 105 | + fun <V> nextFrameBinary( |
| 106 | + key: String, |
| 107 | + roundtripValue: Roundtrip<V, ByteArray>, |
| 108 | + value: Cacheable<V> |
| 109 | + ): V { |
| 110 | + if (state.readMode) { |
| 111 | + return roundtripValue.parse(nextFrameValue(key).valueBinary()) |
| 112 | + } else { |
| 113 | + val value = value.get() |
| 114 | + state.frames.add(key to SnapshotValue.of(roundtripValue.serialize(value))) |
| 115 | + return value |
| 116 | + } |
| 117 | + } |
| 118 | + fun <V> nextFrameBinary(key: String, value: Cacheable<ByteArray>): ByteArray = |
| 119 | + nextFrameBinary(key, Roundtrip.identity(), value) |
| 120 | +} |
0 commit comments