Skip to content

Commit f5ed9dd

Browse files
committed
Add nextBinary
1 parent fbedf0b commit f5ed9dd

File tree

1 file changed

+18
-0
lines changed
  • jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,22 @@ class VcrSelfie(
8787
fun next(key: String, value: Cacheable<String>): String = next(key, Roundtrip.identity(), value)
8888
inline fun <reified V> nextJson(key: String, value: Cacheable<V>): V =
8989
next(key, RoundtripJson.of<V>(), value)
90+
91+
fun <V> nextBinary(key: String, roundtripValue: Roundtrip<V, ByteArray>, value: Cacheable<V>): V {
92+
if (state.readMode) {
93+
val expected = state.sequence[state.count++]
94+
if (expected.first != key) {
95+
throw Selfie.system.fs.assertFailed(
96+
"vcr key mismatch at index ${state.count - 1}", expected.first, key)
97+
}
98+
return roundtripValue.parse(expected.second.valueBinary())
99+
} else {
100+
val value = value.get()
101+
state.sequence.add(key to SnapshotValue.of(roundtripValue.serialize(value)))
102+
return value
103+
}
104+
}
105+
106+
fun <V> nextBinary(key: String, value: Cacheable<ByteArray>): ByteArray
107+
= nextBinary(key, Roundtrip.identity(), value)
90108
}

0 commit comments

Comments
 (0)