Add readtext example
This commit is contained in:
parent
990610ee29
commit
139e2819d7
|
@ -10,8 +10,6 @@ class InputStreamToStringTest {
|
||||||
private val fileName = "src/test/resources/inputstream2string.txt"
|
private val fileName = "src/test/resources/inputstream2string.txt"
|
||||||
private val fileFullContent = "Computer programming can be a hassle\r\n" +
|
private val fileFullContent = "Computer programming can be a hassle\r\n" +
|
||||||
"It's like trying to take a defended castle"
|
"It's like trying to take a defended castle"
|
||||||
private val fileContentUpToStopChar = "Computer programming can be a hassle\r\n" +
|
|
||||||
"It"
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() {
|
fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() {
|
||||||
|
@ -20,7 +18,19 @@ class InputStreamToStringTest {
|
||||||
val content = inputStream.bufferedReader().use(BufferedReader::readText)
|
val content = inputStream.bufferedReader().use(BufferedReader::readText)
|
||||||
assertEquals(fileFullContent, content)
|
assertEquals(fileFullContent, content)
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
fun whenReadFileWithBufferedReaderReadText_thenFullFileContentIsReadAsString() {
|
||||||
|
val file = File(fileName)
|
||||||
|
val inputStream = file.inputStream()
|
||||||
|
val reader = BufferedReader(inputStream.reader())
|
||||||
|
var content: String
|
||||||
|
try {
|
||||||
|
content = reader.readText()
|
||||||
|
} finally {
|
||||||
|
reader.close()
|
||||||
|
}
|
||||||
|
assertEquals(fileFullContent, content)
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() {
|
fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() {
|
||||||
val file = File(fileName)
|
val file = File(fileName)
|
||||||
|
@ -44,8 +54,8 @@ class InputStreamToStringTest {
|
||||||
fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() {
|
fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() {
|
||||||
val file = File(fileName)
|
val file = File(fileName)
|
||||||
val inputStream = file.inputStream()
|
val inputStream = file.inputStream()
|
||||||
val content = inputStream.use { it.readUpToChar('\'') }
|
val content = inputStream.use { it.readUpToChar(' ') }
|
||||||
assertEquals(fileContentUpToStopChar, content)
|
assertEquals("Computer", content)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue