enabled test case for auto-closing helper method on Closable.foreach

This commit is contained in:
James Strachan
2012-01-04 11:49:34 +00:00
parent 49bad83c42
commit 302254f531
2 changed files with 45 additions and 12 deletions
+33 -10
View File
@@ -17,13 +17,14 @@ class IoTest() : TestSupport() {
reader.close()
}
}
fun sample() : Reader {
return StringReader("""Hello
World""");
return StringReader("Hello\nWorld");
}
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()}
@@ -31,20 +32,42 @@ World""");
assertEquals(arrayList("Hello", "World"), list2)
}
fun testUse() {
/**
fun testForEach() {
val list = ArrayList<String>()
val reader = sample().buffered()
TODO compiler error?
reader.use{
val line = it.readLine()
if (line != null) {
list.add(line)
reader.foreach{
while (true) {
val line = it.readLine()
if (line != null)
list.add(line)
else
break
}
}
assertEquals(arrayList("Hello", "World"), list)
}
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)
}
}