Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StringStreamDataWriter.toString to not consume buffer #384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
})
Loading