Skip to content

Commit

Permalink
tests: fix almost all offending tests
Browse files Browse the repository at this point in the history
- fix failing tests
- refactor scanValue lookup
- implement skipValues
- make sure spaces/lines are keep
- TODO: unclear the body escape function
  • Loading branch information
jknack committed Aug 22, 2023
1 parent f50d129 commit cc0b2ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,41 @@ class SnapshotValueReader(val lineReader: LineReader) {
// validate key
nextKey()
resetLine()

// read value
var nextLine = nextLine()
val buffer = StringBuilder()
while (nextLine != null) {
if (nextLine.isNotBlank() && nextLine[0] == headerFirstChar) {
break
}
resetLine()
buffer.append(nextLine).append('\n')
// read next
nextLine = nextLine()
scanValue { line ->
buffer.append(line).append("\n")
}
if (buffer.isEmpty()) {
return SnapshotValue.EMPTY
}
return SnapshotValue.of(bodyEsc.unescape(buffer.toString().trim()))
buffer.setLength(buffer.length - 1)
return SnapshotValue.of(bodyEsc.unescape(buffer.toString()))
}

/** Same as nextValue, but faster. */
fun skipValue(): Unit = TODO()
fun skipValue() {
// Ignore key
nextKey()
resetLine()

scanValue {
// ignore it
}
}
private fun scanValue(consumer: (String) -> Unit) {
// read next
var nextLine = nextLine()
while (nextLine != null && nextLine.indexOf(headerFirstChar) != 0) {
resetLine()

consumer(nextLine)

// read next
nextLine = nextLine()
}
}
private fun nextKey(): String? {
val line = nextLine() ?: return null
val startIndex = line.indexOf(headerStart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class SnapshotValueReaderTest {
reader.peekKey() shouldBe "ascii art okay"
reader.nextValue().valueString() shouldBe """ ╔══╗"""
reader.peekKey() shouldBe "escaped iff on first line"
// TODO: bit confused about escape/unescape what do we need here?
reader.nextValue().valueString() shouldBe """╔══╗"""
reader.peekKey() shouldBe "body escape characters"
reader.nextValue().valueString() shouldBe """𐝁𐝃 linear a is dead"""
Expand Down

0 comments on commit cc0b2ec

Please sign in to comment.