Skip to content

Commit

Permalink
Fix StringStreamDataWriter.toString to not consume buffer (#384)
Browse files Browse the repository at this point in the history
During debugging, it was confusing to see some contents of the buffer
when pausing in the debugger before an assertion, but then seeing the
assertion fail. It didn't make sense why the assertion fails - the value
of the buffer was as expected.

It turned out that the debugger called `toString()`, consuming the
buffer, and leaving an empty buffer when performing the assertion. The
solution here is to just `peek()` the buffer in `toString()`, not
consume it.
  • Loading branch information
krzema12 authored Jan 26, 2025
1 parent e112751 commit beeb568
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ internal class StringStreamDataWriter(
buffer.writeUtf8(string = str, beginIndex = off, endIndex = off + len)
}

override fun toString(): String = buffer.readUtf8()
/**
* Returns the contents of the internal buffer, without altering the buffer's state.
*/
override fun toString(): String = buffer.peek().readUtf8()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.krzeminski.snakeyaml.engine.kmp.api

import io.kotest.assertions.withClue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.string.shouldContain

class StringStreamDataWriterTest: FunSpec({
test("toString() doesn't consume buffer") {
val writer = StringStreamDataWriter()
writer.write("foobar")

// First read.
writer.toString()

withClue("subsequent calls to toString() shouldn't change the buffer's state") {
writer.toString() shouldContain "foobar"
}
}
})

0 comments on commit beeb568

Please sign in to comment.