Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 6602436

Browse files
committed
Replace explicit input usage in tests by read
1 parent 9ab810d commit 6602436

File tree

5 files changed

+195
-106
lines changed

5 files changed

+195
-106
lines changed

core/commonMain/src/kotlinx/io/Bytes.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal typealias BytesPointer = Int
88
/**
99
* Read-only bytes container.
1010
*
11-
* Use [input] to create readable [Input].
11+
* Use [read] to safely create and dispose an input
1212
* All inputs from a [Bytes] instance are share the same buffers.
1313
*
1414
* ```
@@ -32,13 +32,15 @@ class Bytes internal constructor(internal val bufferPool: ObjectPool<Buffer>) :
3232

3333
/**
3434
* Create [Input] view on content.
35+
*
3536
*/
37+
@Deprecated(message = "Unsafe input usage", level = DeprecationLevel.WARNING)
3638
fun input(): Input = object : Input(this@Bytes) {
3739
override fun closeSource() {}
3840
override fun fill(buffer: Buffer): Int = 0
3941
}
4042

41-
@Suppress("OVERRIDE_BY_INLINE")
43+
@Suppress("OVERRIDE_BY_INLINE", "DEPRECATION")
4244
override inline fun <R> read(reader: Input.() -> R): R = input().use(reader)
4345

4446
override fun toString() = "Bytes($head..$tail)"

core/commonTest/src/kotlinx/io/tests/BytesTest.kt

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,26 @@ class BytesTest {
2828

2929
assertEquals(2 + 2 + 2 + 4 + 8 + 4 + 8 + 8 + 3, bytes.size)
3030

31-
val input = bytes.input()
32-
val ba = ByteArray(2)
33-
input.readArray(ba)
31+
bytes.read {
32+
val ba = ByteArray(2)
33+
readArray(ba)
3434

35-
assertEquals(0x11, ba[0])
36-
assertEquals(0x22, ba[1])
35+
assertEquals(0x11, ba[0])
36+
assertEquals(0x22, ba[1])
3737

38-
assertEquals(0x12, input.readByte())
39-
assertEquals(0x82u, input.readUByte())
40-
assertEquals(0x3456, input.readShort())
41-
assertEquals(0x789abcde, input.readInt())
42-
assertEquals(1.25, input.readDouble())
43-
assertEquals(1.25f, input.readFloat())
38+
assertEquals(0x12, readByte())
39+
assertEquals(0x82u, readUByte())
40+
assertEquals(0x3456, readShort())
41+
assertEquals(0x789abcde, readInt())
42+
assertEquals(1.25, readDouble())
43+
assertEquals(1.25f, readFloat())
4444

45-
val ll = (1..8).map { input.readByte().toInt() and 0xff }.joinToString()
46-
assertEquals("18, 52, 86, 120, 154, 188, 222, 240", ll)
47-
assertEquals(0x123456789abcdef0, input.readLong())
45+
val ll = (1..8).map { readByte().toInt() and 0xff }.joinToString()
46+
assertEquals("18, 52, 86, 120, 154, 188, 222, 240", ll)
47+
assertEquals(0x123456789abcdef0, readLong())
4848

49-
assertEquals("OK", input.readUTF8Line())
49+
assertEquals("OK", readUTF8Line())
50+
}
5051
}
5152

5253
@Test
@@ -83,9 +84,10 @@ class BytesTest {
8384
buildBytes {
8485
writeArray(ByteArray(9999))
8586
}.use { buffer ->
86-
val input = buffer.input()
87-
input.readArray(ByteArray(9999))
88-
assertTrue { input.eof() }
87+
buffer.read {
88+
readArray(ByteArray(9999))
89+
assertTrue { eof() }
90+
}
8991
}
9092
}
9193

@@ -122,8 +124,6 @@ class BytesTest {
122124
}
123125
assertTrue { eof() }
124126
}
125-
126-
127127
}
128128

129129
@Test
@@ -134,7 +134,6 @@ class BytesTest {
134134
}.read {
135135
assertTrue { eof() }
136136
}
137-
138137
}
139138

140139
@Test

core/commonTest/src/kotlinx/io/tests/OutputTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package kotlinx.io.tests
22

3-
import kotlinx.io.*
4-
import kotlin.test.*
3+
import kotlinx.io.buildBytes
4+
import kotlin.test.Test
5+
import kotlin.test.assertFalse
6+
import kotlin.test.assertTrue
57

68
class OutputTest {
79
@Test
810
fun testBuildBytes() {
9-
val bytes = buildBytes {
11+
buildBytes {
1012
writeLong(0x0001020304050607)
1113
writeLong(0x08090A0B0C0D0E0F)
1214
writeInt(0x08090A0B)
1315
writeInt(0x00010203)
14-
}
15-
bytes.input().apply {
16+
}.read {
1617
assertFalse(eof())
1718
assertReadLong(0x0001020304050607)
1819
assertReadLong(0x08090A0B0C0D0E0F)
@@ -24,14 +25,13 @@ class OutputTest {
2425

2526
@Test
2627
fun testBuildBytesChunked() {
27-
val bytes = buildBytes(2) {
28+
buildBytes(2) {
2829
writeByte(0xFF.toByte())
2930
writeInt(0x08090A0B)
3031
writeInt(0x00010203)
3132
writeInt(0xAB023F3)
3233
writeInt(0xDEAD) // by writing unit tests
33-
}
34-
bytes.input().apply {
34+
}.read {
3535
assertFalse(eof())
3636
assertReadByte(0xFF.toByte())
3737
assertReadInt(0x08090A0B)

core/commonTest/src/kotlinx/io/tests/text/InputStringTest.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ class InputStringTest {
2020

2121
@Test
2222
fun decodeUtf8FromInputUntil() = bufferSizes.forEach { size ->
23-
val input = buildBytes(size) { writeArray(content) }.input()
24-
val result = input.readUTF8StringUntilDelimiter('.')
25-
assertEquals(expected.dropLast(1), result)
23+
buildBytes(size) { writeArray(content) }.read {
24+
val result = readUTF8StringUntilDelimiter('.')
25+
assertEquals(expected.dropLast(1), result)
26+
}
2627
}
2728

2829
@Test
2930
fun decodeUtf8FromInput() = bufferSizes.forEach { size ->
30-
val input = buildBytes(size) { writeArray(content) }.input()
31-
val result = input.readUTF8String(expected.length)
32-
assertEquals(expected, result)
31+
buildBytes(size) { writeArray(content) }.read {
32+
val result = readUTF8String(expected.length)
33+
assertEquals(expected, result)
34+
}
3335
}
3436
}

0 commit comments

Comments
 (0)