From 24154f060970a38b45c34dbb6ba9e580a957523d Mon Sep 17 00:00:00 2001 From: Jens Pots Date: Fri, 24 May 2024 16:59:53 +0200 Subject: [PATCH] feat(test): dummy writers and readers --- src/test/kotlin/bridge/DummyReader.kt | 25 +++++++++++++++++++++++++ src/test/kotlin/bridge/DummyWriter.kt | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/test/kotlin/bridge/DummyReader.kt create mode 100644 src/test/kotlin/bridge/DummyWriter.kt diff --git a/src/test/kotlin/bridge/DummyReader.kt b/src/test/kotlin/bridge/DummyReader.kt new file mode 100644 index 0000000..8c73b43 --- /dev/null +++ b/src/test/kotlin/bridge/DummyReader.kt @@ -0,0 +1,25 @@ +package bridge + +class DummyReader(private val contents: Array) : Reader { + private var index = 0 + + override suspend fun read(): Reader.Result { + return readSync() + } + + override fun readSync(): Reader.Result { + // If we have read all the contents, return closed. + if (isClosed()) { + return Reader.Result.closed() + } + + // Increment the index and return the next value. + val value = contents[index] + index += 1 + return Reader.Result.success(value) + } + + override fun isClosed(): Boolean { + return index >= contents.size + } +} diff --git a/src/test/kotlin/bridge/DummyWriter.kt b/src/test/kotlin/bridge/DummyWriter.kt new file mode 100644 index 0000000..c40ec79 --- /dev/null +++ b/src/test/kotlin/bridge/DummyWriter.kt @@ -0,0 +1,25 @@ +package bridge + +class DummyWriter : Writer { + private val values = mutableListOf() + private var closed = false + + override suspend fun push(value: ByteArray) { + pushSync(value) + } + + override fun pushSync(value: ByteArray) { + if (closed) { + throw IllegalStateException("Cannot push to closed writer.") + } + values.add(value) + } + + override fun close() { + closed = true + } + + fun getValues(): List { + return values + } +}