Reformatted.
This commit is contained in:
@@ -9,97 +9,97 @@ import java.util.*
|
|||||||
import org.junit.Test as test
|
import org.junit.Test as test
|
||||||
|
|
||||||
class IoTest(){
|
class IoTest(){
|
||||||
test fun testLineIteratorWithManualClose() {
|
test fun testLineIteratorWithManualClose() {
|
||||||
val reader = sample().buffered()
|
val reader = sample().buffered()
|
||||||
try {
|
try {
|
||||||
val list = reader.lineIterator().toArrayList()
|
val list = reader.lineIterator().toArrayList()
|
||||||
assertEquals(arrayList("Hello", "World"), list)
|
assertEquals(arrayList("Hello", "World"), list)
|
||||||
} finally {
|
} finally {
|
||||||
reader.close()
|
reader.close()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun sample() : Reader {
|
|
||||||
return StringReader("Hello\nWorld");
|
|
||||||
}
|
|
||||||
|
|
||||||
test fun testLineIterator() {
|
|
||||||
// TODO we should maybe zap the useLines approach as it encourages
|
|
||||||
// use of iterators which don't close the underlying stream
|
|
||||||
val list1 = sample().useLines{it.toArrayList()}
|
|
||||||
val list2 = sample().useLines<ArrayList<String>>{it.toArrayList()}
|
|
||||||
|
|
||||||
assertEquals(arrayList("Hello", "World"), list1)
|
|
||||||
assertEquals(arrayList("Hello", "World"), list2)
|
|
||||||
}
|
|
||||||
|
|
||||||
test fun testForEach() {
|
|
||||||
val list = ArrayList<String>()
|
|
||||||
val reader = sample().buffered()
|
|
||||||
|
|
||||||
reader.use{
|
|
||||||
while (true) {
|
|
||||||
val line = it.readLine()
|
|
||||||
if (line != null)
|
|
||||||
list.add(line)
|
|
||||||
else
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(arrayList("Hello", "World"), list)
|
fun sample(): Reader {
|
||||||
}
|
return StringReader("Hello\nWorld");
|
||||||
|
|
||||||
test fun testForEachLine() {
|
|
||||||
val list = ArrayList<String>()
|
|
||||||
val reader = sample()
|
|
||||||
|
|
||||||
/* TODO would be nicer maybe to write this as
|
|
||||||
reader.lines.forEach { ... }
|
|
||||||
|
|
||||||
as we could one day maybe one day write that as
|
|
||||||
for (line in reader.lines)
|
|
||||||
|
|
||||||
if the for(elem in thing) {...} statement could act as syntax sugar for
|
|
||||||
thing.forEach{ elem -> ... }
|
|
||||||
|
|
||||||
if thing is not an Iterable/array/Iterator but has a suitable forEach method
|
|
||||||
*/
|
|
||||||
reader.forEachLine{
|
|
||||||
list.add(it)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(arrayList("Hello", "World"), list)
|
test fun testLineIterator() {
|
||||||
}
|
// TODO we should maybe zap the useLines approach as it encourages
|
||||||
|
// use of iterators which don't close the underlying stream
|
||||||
test fun testForEachLineFile() {
|
val list1 = sample().useLines{ it.toArrayList() }
|
||||||
val file = File.createTempFile("temp", System.nanoTime().toString())
|
val list2 = sample().useLines<ArrayList<String>>{ it.toArrayList() }
|
||||||
file.writeText("Hello\nWorld")
|
|
||||||
|
|
||||||
|
assertEquals(arrayList("Hello", "World"), list1)
|
||||||
val list = ArrayList<String>()
|
assertEquals(arrayList("Hello", "World"), list2)
|
||||||
file.forEachLine{
|
|
||||||
list.add(it)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(arrayList("Hello", "World"), list)
|
test fun testForEach() {
|
||||||
file.deleteOnExit()
|
val list = ArrayList<String>()
|
||||||
}
|
val reader = sample().buffered()
|
||||||
|
|
||||||
test fun testListFiles() {
|
reader.use{
|
||||||
val dir = File.createTempFile("temp", System.nanoTime().toString())
|
while (true) {
|
||||||
dir.delete()
|
val line = it.readLine()
|
||||||
dir.mkdir()
|
if (line != null)
|
||||||
|
list.add(line)
|
||||||
File.createTempFile("temp", "1.kt", dir)
|
else
|
||||||
File.createTempFile("temp", "2.java", dir)
|
break
|
||||||
File.createTempFile("temp", "3.kt", dir)
|
}
|
||||||
|
}
|
||||||
val result = dir.listFiles { it.getName().endsWith(".kt") }
|
|
||||||
|
assertEquals(arrayList("Hello", "World"), list)
|
||||||
assertNotNull(result)
|
}
|
||||||
assertEquals(result!!.size, 2)
|
|
||||||
}
|
test fun testForEachLine() {
|
||||||
|
val list = ArrayList<String>()
|
||||||
|
val reader = sample()
|
||||||
|
|
||||||
|
/* TODO would be nicer maybe to write this as
|
||||||
|
reader.lines.forEach { ... }
|
||||||
|
|
||||||
|
as we could one day maybe one day write that as
|
||||||
|
for (line in reader.lines)
|
||||||
|
|
||||||
|
if the for(elem in thing) {...} statement could act as syntax sugar for
|
||||||
|
thing.forEach{ elem -> ... }
|
||||||
|
|
||||||
|
if thing is not an Iterable/array/Iterator but has a suitable forEach method
|
||||||
|
*/
|
||||||
|
reader.forEachLine{
|
||||||
|
list.add(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(arrayList("Hello", "World"), list)
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun testForEachLineFile() {
|
||||||
|
val file = File.createTempFile("temp", System.nanoTime().toString())
|
||||||
|
file.writeText("Hello\nWorld")
|
||||||
|
|
||||||
|
|
||||||
|
val list = ArrayList<String>()
|
||||||
|
file.forEachLine{
|
||||||
|
list.add(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(arrayList("Hello", "World"), list)
|
||||||
|
file.deleteOnExit()
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun testListFiles() {
|
||||||
|
val dir = File.createTempFile("temp", System.nanoTime().toString())
|
||||||
|
dir.delete()
|
||||||
|
dir.mkdir()
|
||||||
|
|
||||||
|
File.createTempFile("temp", "1.kt", dir)
|
||||||
|
File.createTempFile("temp", "2.java", dir)
|
||||||
|
File.createTempFile("temp", "3.kt", dir)
|
||||||
|
|
||||||
|
val result = dir.listFiles { it.getName().endsWith(".kt") }
|
||||||
|
|
||||||
|
assertNotNull(result)
|
||||||
|
assertEquals(result!!.size, 2)
|
||||||
|
}
|
||||||
|
|
||||||
test fun relativePath() {
|
test fun relativePath() {
|
||||||
val file1 = File("src")
|
val file1 = File("src")
|
||||||
|
|||||||
Reference in New Issue
Block a user