Skip to content

Commit 292f6e8

Browse files
committed
Standardize on a "frame" metaphor.
1 parent aad41a4 commit 292f6e8

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/Mode.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ enum class Mode {
4747
msg("Snapshot " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
4848
internal fun msgSnapshotMismatchBinary(expected: ByteArray, actual: ByteArray) =
4949
msgSnapshotMismatch(expected.toQuotedPrintable(), actual.toQuotedPrintable())
50-
internal fun msgVcrKeyMismatch(key: String, expected: String, actual: String) =
51-
msg("VCR key $key " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
52-
internal fun msgVcrKeyUnread(expected: Int, actual: Int) =
53-
msg("VCR entries unread - only $actual were read out of $expected")
54-
internal fun msgVcrKeyUnderflow(expected: Int) =
50+
internal fun msgVcrMismatch(key: String, expected: String, actual: String) =
51+
msg("VCR frame $key " + SnapshotNotEqualErrorMsg.forUnequalStrings(expected, actual))
52+
internal fun msgVcrUnread(expected: Int, actual: Int) =
53+
msg("VCR frames unread - only $actual were read out of $expected")
54+
internal fun msgVcrUnderflow(expected: Int) =
5555
msg(
56-
"VCR entries exhausted - only $expected are available but you tried to read ${expected + 1}")
56+
"VCR frames exhausted - only $expected are available but you tried to read ${expected + 1}")
5757
private fun ByteArray.toQuotedPrintable(): String {
5858
val sb = StringBuilder()
5959
for (byte in this) {

jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/VcrSelfie.kt

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ internal constructor(
3434
}
3535

3636
private class State(val readMode: Boolean) {
37-
var count = 0
38-
val sequence = mutableListOf<Pair<String, SnapshotValue>>()
37+
var currentFrame = 0
38+
val frames = mutableListOf<Pair<String, SnapshotValue>>()
3939
}
4040
private val state: State
4141

@@ -55,61 +55,66 @@ internal constructor(
5555
check(num == idx)
5656
++idx
5757
val keyAfterNum = key.substring(nextClose + 1)
58-
state.sequence.add(keyAfterNum to value)
58+
state.frames.add(keyAfterNum to value)
5959
}
6060
}
6161
}
6262
override fun close() {
6363
if (state.readMode) {
64-
if (state.sequence.size != state.count) {
64+
if (state.frames.size != state.currentFrame) {
6565
throw Selfie.system.fs.assertFailed(
66-
Selfie.system.mode.msgVcrKeyUnread(state.sequence.size, state.count))
66+
Selfie.system.mode.msgVcrUnread(state.frames.size, state.currentFrame))
6767
}
6868
} else {
6969
var snapshot = Snapshot.of("")
7070
var idx = 1
71-
for ((key, value) in state.sequence) {
71+
for ((key, value) in state.frames) {
7272
snapshot = snapshot.plusFacet("$OPEN$idx$CLOSE$key", value)
7373
}
7474
disk.writeDisk(snapshot, sub, call)
7575
}
7676
}
77-
private fun nextValue(key: String): SnapshotValue {
77+
private fun nextFrameValue(key: String): SnapshotValue {
7878
val mode = Selfie.system.mode
7979
val fs = Selfie.system.fs
80-
if (state.sequence.size <= state.count) {
81-
throw fs.assertFailed(mode.msgVcrKeyUnderflow(state.sequence.size))
80+
if (state.frames.size <= state.currentFrame) {
81+
throw fs.assertFailed(mode.msgVcrUnderflow(state.frames.size))
8282
}
83-
val expected = state.sequence[state.count++]
83+
val expected = state.frames[state.currentFrame++]
8484
if (expected.first != key) {
8585
throw fs.assertFailed(
86-
mode.msgVcrKeyMismatch("$sub[$OPEN${state.count}$CLOSE]", expected.first, key),
86+
mode.msgVcrMismatch("$sub[$OPEN${state.currentFrame}$CLOSE]", expected.first, key),
8787
expected.first,
8888
key)
8989
}
9090
return expected.second
9191
}
92-
fun <V> next(key: String, roundtripValue: Roundtrip<V, String>, value: Cacheable<V>): V {
92+
fun <V> nextFrame(key: String, roundtripValue: Roundtrip<V, String>, value: Cacheable<V>): V {
9393
if (state.readMode) {
94-
return roundtripValue.parse(nextValue(key).valueString())
94+
return roundtripValue.parse(nextFrameValue(key).valueString())
9595
} else {
9696
val value = value.get()
97-
state.sequence.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
97+
state.frames.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
9898
return value
9999
}
100100
}
101-
fun next(key: String, value: Cacheable<String>): String = next(key, Roundtrip.identity(), value)
102-
inline fun <reified V> nextJson(key: String, value: Cacheable<V>): V =
103-
next(key, RoundtripJson.of<V>(), value)
104-
fun <V> nextBinary(key: String, roundtripValue: Roundtrip<V, ByteArray>, value: Cacheable<V>): V {
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 {
105110
if (state.readMode) {
106-
return roundtripValue.parse(nextValue(key).valueBinary())
111+
return roundtripValue.parse(nextFrameValue(key).valueBinary())
107112
} else {
108113
val value = value.get()
109-
state.sequence.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
114+
state.frames.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
110115
return value
111116
}
112117
}
113-
fun <V> nextBinary(key: String, value: Cacheable<ByteArray>): ByteArray =
114-
nextBinary(key, Roundtrip.identity(), value)
118+
fun <V> nextFrameBinary(key: String, value: Cacheable<ByteArray>): ByteArray =
119+
nextFrameBinary(key, Roundtrip.identity(), value)
115120
}

0 commit comments

Comments
 (0)