Add readtext example

This commit is contained in:
Alexander Molochko 2019-04-10 13:08:00 +03:00
parent 990610ee29
commit 139e2819d7
1 changed files with 15 additions and 5 deletions

View File

@ -10,8 +10,6 @@ class InputStreamToStringTest {
private val fileName = "src/test/resources/inputstream2string.txt"
private val fileFullContent = "Computer programming can be a hassle\r\n" +
"It's like trying to take a defended castle"
private val fileContentUpToStopChar = "Computer programming can be a hassle\r\n" +
"It"
@Test
fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() {
@ -20,7 +18,19 @@ class InputStreamToStringTest {
val content = inputStream.bufferedReader().use(BufferedReader::readText)
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
fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() {
val file = File(fileName)
@ -44,8 +54,8 @@ class InputStreamToStringTest {
fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() {
val file = File(fileName)
val inputStream = file.inputStream()
val content = inputStream.use { it.readUpToChar('\'') }
assertEquals(fileContentUpToStopChar, content)
val content = inputStream.use { it.readUpToChar(' ') }
assertEquals("Computer", content)
}
@Test