Add java.nio.Path extensions to stdlib-jdk7: part 2
- Add notExists - Rename isFile to isRegularFile - Remove forEachBlock - Rename listFiles - Add relativeTo extensions - Remove extra overloads - Update doc comments - Address review comments #KT-19192
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@@ -8,9 +8,7 @@ package kotlin.jdk7.test
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.StandardOpenOption
|
||||
import java.util.*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.*
|
||||
|
||||
class PathReadWriteTest {
|
||||
@Test
|
||||
@@ -18,7 +16,7 @@ class PathReadWriteTest {
|
||||
val file = Files.createTempFile(null, null)
|
||||
file.writeText("Hello\n")
|
||||
file.appendText("World\n")
|
||||
file.appendText("Again")
|
||||
file.writeText("Again", Charsets.US_ASCII, StandardOpenOption.APPEND)
|
||||
|
||||
assertEquals("Hello\nWorld\nAgain", file.readText())
|
||||
assertEquals(listOf("Hello", "World", "Again"), file.readLines(Charsets.UTF_8))
|
||||
@@ -35,25 +33,21 @@ class PathReadWriteTest {
|
||||
writer.write("World")
|
||||
writer.close()
|
||||
|
||||
file.forEachBlock { arr: ByteArray, size: Int ->
|
||||
assertTrue(size in 11..12, size.toString())
|
||||
assertTrue(arr.contains('W'.toByte()))
|
||||
}
|
||||
val list = ArrayList<String>()
|
||||
file.forEachLine(StandardOpenOption.READ, charset = Charsets.UTF_8) {
|
||||
file.forEachLine(charset = Charsets.UTF_8, options = arrayOf(StandardOpenOption.READ)) {
|
||||
list.add(it)
|
||||
}
|
||||
assertEquals(arrayListOf("Hello", "World"), list)
|
||||
assertEquals(listOf("Hello", "World"), list)
|
||||
|
||||
assertEquals(arrayListOf("Hello", "World"), file.readLines())
|
||||
assertEquals(listOf("Hello", "World"), file.readLines())
|
||||
|
||||
file.useLines {
|
||||
assertEquals(arrayListOf("Hello", "World"), it.toList())
|
||||
assertEquals(listOf("Hello", "World"), it.toList())
|
||||
}
|
||||
|
||||
val text = file.inputStream().reader().readText()
|
||||
assertTrue(text.contains("Hello"))
|
||||
assertTrue(text.contains("World"))
|
||||
assertTrue("Hello" in text)
|
||||
assertTrue("World" in text)
|
||||
|
||||
file.writeText("")
|
||||
var c = 0
|
||||
@@ -76,5 +70,62 @@ class PathReadWriteTest {
|
||||
|
||||
file.toFile().deleteOnExit()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBufferedReader() {
|
||||
val file = Files.createTempFile(null, null)
|
||||
val lines = listOf("line1", "line2")
|
||||
Files.write(file, lines, Charsets.UTF_8)
|
||||
|
||||
assertEquals(file.bufferedReader().use { it.readLines() }, lines)
|
||||
assertEquals(file.bufferedReader(Charsets.UTF_8, 1024, StandardOpenOption.READ).use { it.readLines() }, lines)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBufferedWriter() {
|
||||
val file = Files.createTempFile(null, null)
|
||||
|
||||
file.bufferedWriter().use { it.write("line1\n") }
|
||||
file.bufferedWriter(Charsets.UTF_8, 1024, StandardOpenOption.APPEND).use { it.write("line2\n") }
|
||||
|
||||
assertEquals(Files.readAllLines(file, Charsets.UTF_8), listOf("line1", "line2"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPrintWriter() {
|
||||
val file = Files.createTempFile(null, null)
|
||||
|
||||
val writer = file.printWriter()
|
||||
val str1 = "Hello, world!"
|
||||
val str2 = "Everything is wonderful!"
|
||||
writer.println(str1)
|
||||
writer.println(str2)
|
||||
writer.close()
|
||||
|
||||
val writer2 = file.printWriter(options = arrayOf(StandardOpenOption.APPEND))
|
||||
val str3 = "Hello again!"
|
||||
writer2.println(str3)
|
||||
writer2.close()
|
||||
|
||||
val writer3 = file.printWriter(Charsets.UTF_8, StandardOpenOption.APPEND)
|
||||
val str4 = "Hello one last time!"
|
||||
writer3.println(str4)
|
||||
writer3.close()
|
||||
|
||||
file.bufferedReader().use { reader ->
|
||||
assertEquals(str1, reader.readLine())
|
||||
assertEquals(str2, reader.readLine())
|
||||
assertEquals(str3, reader.readLine())
|
||||
assertEquals(str4, reader.readLine())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWriteBytes() {
|
||||
val file = Files.createTempFile(null, null)
|
||||
file.writeBytes("Hello".encodeToByteArray())
|
||||
file.appendBytes(" world!".encodeToByteArray())
|
||||
assertEquals(file.readText(), "Hello world!")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user