Skip to content

Commit a1707bb

Browse files
committed
Rename SnapshotValueReader.peekKey() to peekKeyRaw() since it is unescaped.
1 parent 5173032 commit a1707bb

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class SnapshotFile {
242242

243243
class SnapshotReader(val valueReader: SnapshotValueReader) {
244244
fun peekKey(): String? {
245-
val next = valueReader.peekKey() ?: return null
245+
val next = valueReader.peekKeyRaw() ?: return null
246246
if (next == SnapshotFile.END_OF_FILE) {
247247
return null
248248
}
@@ -255,7 +255,7 @@ class SnapshotReader(val valueReader: SnapshotValueReader) {
255255
val rootName = peekKey()
256256
var snapshot = Snapshot.of(valueReader.nextValue())
257257
while (true) {
258-
val nextKey = valueReader.peekKey() ?: return snapshot
258+
val nextKey = valueReader.peekKeyRaw() ?: return snapshot
259259
val facetIdx = nextKey.indexOf('[')
260260
if (facetIdx == -1 || (facetIdx == 0 && nextKey == SnapshotFile.END_OF_FILE)) {
261261
return snapshot
@@ -287,15 +287,15 @@ class SnapshotValueReader(val lineReader: LineReader) {
287287
val unixNewlines = lineReader.unixNewlines()
288288

289289
/** The key of the next value, does not increment anything about the reader's state. */
290-
fun peekKey(): String? {
291-
return nextKey()
290+
fun peekKeyRaw(): String? {
291+
return nextKeyRaw()
292292
}
293293

294294
/** Reads the next value. */
295295
@OptIn(ExperimentalEncodingApi::class)
296296
fun nextValue(): SnapshotValue {
297297
// validate key
298-
nextKey()
298+
nextKeyRaw()
299299
val isBase64 = nextLine()!!.contains(FLAG_BASE64)
300300
resetLine()
301301

@@ -323,7 +323,7 @@ class SnapshotValueReader(val lineReader: LineReader) {
323323
/** Same as nextValue, but faster. */
324324
fun skipValue() {
325325
// Ignore key
326-
nextKey()
326+
nextKeyRaw()
327327
resetLine()
328328

329329
scanValue {
@@ -342,7 +342,7 @@ class SnapshotValueReader(val lineReader: LineReader) {
342342
nextLine = nextLine()
343343
}
344344
}
345-
private fun nextKey(): String? {
345+
private fun nextKeyRaw(): String? {
346346
val line = nextLine() ?: return null
347347
val startIndex = line.indexOf(KEY_START)
348348
val endIndex = line.indexOf(KEY_END)

jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/SnapshotValueReaderTest.kt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,42 @@ class SnapshotValueReaderTest {
4646
╔═ 05_notSureHowKotlinMultilineWorks ═╗
4747
"""
4848
.trimIndent())
49-
reader.peekKey() shouldBe "00_empty"
50-
reader.peekKey() shouldBe "00_empty"
49+
reader.peekKeyRaw() shouldBe "00_empty"
50+
reader.peekKeyRaw() shouldBe "00_empty"
5151
reader.nextValue().valueString() shouldBe ""
52-
reader.peekKey() shouldBe "01_singleLineString"
53-
reader.peekKey() shouldBe "01_singleLineString"
52+
reader.peekKeyRaw() shouldBe "01_singleLineString"
53+
reader.peekKeyRaw() shouldBe "01_singleLineString"
5454
reader.nextValue().valueString() shouldBe "this is one line"
55-
reader.peekKey() shouldBe "01a_singleLineLeadingSpace"
55+
reader.peekKeyRaw() shouldBe "01a_singleLineLeadingSpace"
5656
reader.nextValue().valueString() shouldBe " the leading space is significant"
57-
reader.peekKey() shouldBe "01b_singleLineTrailingSpace"
57+
reader.peekKeyRaw() shouldBe "01b_singleLineTrailingSpace"
5858
reader.nextValue().valueString() shouldBe "the trailing space is significant "
59-
reader.peekKey() shouldBe "02_multiLineStringTrimmed"
59+
reader.peekKeyRaw() shouldBe "02_multiLineStringTrimmed"
6060
reader.nextValue().valueString() shouldBe "Line 1\nLine 2"
6161
// note that leading and trailing newlines in the snapshots are significant
6262
// this is critical so that snapshots can accurately capture the exact number of newlines
63-
reader.peekKey() shouldBe "03_multiLineStringTrailingNewline"
63+
reader.peekKeyRaw() shouldBe "03_multiLineStringTrailingNewline"
6464
reader.nextValue().valueString() shouldBe "Line 1\nLine 2\n"
65-
reader.peekKey() shouldBe "04_multiLineStringLeadingNewline"
65+
reader.peekKeyRaw() shouldBe "04_multiLineStringLeadingNewline"
6666
reader.nextValue().valueString() shouldBe "\nLine 1\nLine 2"
67-
reader.peekKey() shouldBe "05_notSureHowKotlinMultilineWorks"
67+
reader.peekKeyRaw() shouldBe "05_notSureHowKotlinMultilineWorks"
6868
reader.nextValue().valueString() shouldBe ""
6969
}
7070

7171
@Test
7272
fun invalidNames() {
73-
shouldThrow<ParseException> { SnapshotValueReader.of("╔═name ═╗").peekKey() }
73+
shouldThrow<ParseException> { SnapshotValueReader.of("╔═name ═╗").peekKeyRaw() }
7474
.let { it.message shouldBe "L1:Expected to start with '╔═ '" }
75-
shouldThrow<ParseException> { SnapshotValueReader.of("╔═ name═╗").peekKey() }
75+
shouldThrow<ParseException> { SnapshotValueReader.of("╔═ name═╗").peekKeyRaw() }
7676
.let { it.message shouldBe "L1:Expected to contain ' ═╗'" }
77-
shouldThrow<ParseException> { SnapshotValueReader.of("╔═ name ═╗").peekKey() }
77+
shouldThrow<ParseException> { SnapshotValueReader.of("╔═ name ═╗").peekKeyRaw() }
7878
.let { it.message shouldBe "L1:Leading spaces are disallowed: ' name'" }
79-
shouldThrow<ParseException> { SnapshotValueReader.of("╔═ name ═╗").peekKey() }
79+
shouldThrow<ParseException> { SnapshotValueReader.of("╔═ name ═╗").peekKeyRaw() }
8080
.let { it.message shouldBe "L1:Trailing spaces are disallowed: 'name '" }
81-
SnapshotValueReader.of("╔═ name ═╗ comment okay").peekKey() shouldBe "name"
82-
SnapshotValueReader.of("╔═ name ═╗okay here too").peekKey() shouldBe "name"
81+
SnapshotValueReader.of("╔═ name ═╗ comment okay").peekKeyRaw() shouldBe "name"
82+
SnapshotValueReader.of("╔═ name ═╗okay here too").peekKeyRaw() shouldBe "name"
8383
SnapshotValueReader.of("╔═ name ═╗ okay ╔═ ═╗ (it's the first ' ═╗' that counts)")
84-
.peekKey() shouldBe "name"
84+
.peekKeyRaw() shouldBe "name"
8585
}
8686

8787
@Test
@@ -96,15 +96,15 @@ class SnapshotValueReaderTest {
9696
╔═ test with \┌\─ ascii art \─\┐ in name ═╗
9797
"""
9898
.trimIndent())
99-
reader.peekKey() shouldBe "test with \\(square brackets\\) in name"
99+
reader.peekKeyRaw() shouldBe "test with \\(square brackets\\) in name"
100100
reader.nextValue().valueString() shouldBe ""
101-
reader.peekKey() shouldBe """test with \\backslash\\ in name"""
101+
reader.peekKeyRaw() shouldBe """test with \\backslash\\ in name"""
102102
reader.nextValue().valueString() shouldBe ""
103-
reader.peekKey() shouldBe "test with\\nnewline\\nin name"
103+
reader.peekKeyRaw() shouldBe "test with\\nnewline\\nin name"
104104
reader.nextValue().valueString() shouldBe ""
105-
reader.peekKey() shouldBe "test with \\ttab\\t in name"
105+
reader.peekKeyRaw() shouldBe "test with \\ttab\\t in name"
106106
reader.nextValue().valueString() shouldBe ""
107-
reader.peekKey() shouldBe "test with \\\\─ ascii art \\\\┐ in name"
107+
reader.peekKeyRaw() shouldBe "test with \\\\─ ascii art \\\\┐ in name"
108108
reader.nextValue().valueString() shouldBe ""
109109
}
110110

@@ -121,11 +121,11 @@ class SnapshotValueReaderTest {
121121
𐝃𐝁𐝃𐝃 linear a is dead
122122
"""
123123
.trimIndent())
124-
reader.peekKey() shouldBe "ascii art okay"
124+
reader.peekKeyRaw() shouldBe "ascii art okay"
125125
reader.nextValue().valueString() shouldBe """ ╔══╗"""
126-
reader.peekKey() shouldBe "escaped iff on first line"
126+
reader.peekKeyRaw() shouldBe "escaped iff on first line"
127127
reader.nextValue().valueString() shouldBe """╔══╗"""
128-
reader.peekKey() shouldBe "body escape characters"
128+
reader.peekKeyRaw() shouldBe "body escape characters"
129129
reader.nextValue().valueString() shouldBe """𐝁𐝃 linear a is dead"""
130130
}
131131

@@ -148,12 +148,12 @@ class SnapshotValueReaderTest {
148148
}
149149
private fun assertKeyValueWithSkip(input: String, key: String, value: String) {
150150
val reader = SnapshotValueReader.of(input)
151-
while (reader.peekKey() != key) {
151+
while (reader.peekKeyRaw() != key) {
152152
reader.skipValue()
153153
}
154-
reader.peekKey() shouldBe key
154+
reader.peekKeyRaw() shouldBe key
155155
reader.nextValue().valueString() shouldBe value
156-
while (reader.peekKey() != null) {
156+
while (reader.peekKeyRaw() != null) {
157157
reader.skipValue()
158158
}
159159
}
@@ -163,7 +163,7 @@ class SnapshotValueReaderTest {
163163
val reader = SnapshotValueReader.of("""╔═ Apple ═╗ base64 length 3 bytes
164164
c2Fk
165165
""")
166-
reader.peekKey() shouldBe "Apple"
166+
reader.peekKeyRaw() shouldBe "Apple"
167167
reader.nextValue().valueBinary() shouldBe "sad".encodeToByteArray()
168168
}
169169
}

0 commit comments

Comments
 (0)