migrated most of the test cases over to the new -> syntax for functions; still can't compile or run the tests yet though :(, needs more work...

This commit is contained in:
James Strachan
2011-12-21 16:26:37 +00:00
parent aaf18bdd86
commit 894c84190a
8 changed files with 135 additions and 132 deletions
+114 -112
View File
@@ -1,136 +1,138 @@
package std
package std.io
package io {
import java.io.*
import java.nio.charset.*
import java.io.*
import java.nio.charset.*
inline fun print(message : Any?) { System.out?.print(message) }
inline fun print(message : Int) { System.out?.print(message) }
inline fun print(message : Long) { System.out?.print(message) }
inline fun print(message : Byte) { System.out?.print(message) }
inline fun print(message : Short) { System.out?.print(message) }
inline fun print(message : Char) { System.out?.print(message) }
inline fun print(message : Boolean) { System.out?.print(message) }
inline fun print(message : Float) { System.out?.print(message) }
inline fun print(message : Double) { System.out?.print(message) }
inline fun print(message : CharArray) { System.out?.print(message) }
inline fun print(message : Any?) { System.out?.print(message) }
inline fun print(message : Int) { System.out?.print(message) }
inline fun print(message : Long) { System.out?.print(message) }
inline fun print(message : Byte) { System.out?.print(message) }
inline fun print(message : Short) { System.out?.print(message) }
inline fun print(message : Char) { System.out?.print(message) }
inline fun print(message : Boolean) { System.out?.print(message) }
inline fun print(message : Float) { System.out?.print(message) }
inline fun print(message : Double) { System.out?.print(message) }
inline fun print(message : CharArray) { System.out?.print(message) }
inline fun println(message : Any?) { System.out?.println(message) }
inline fun println(message : Int) { System.out?.println(message) }
inline fun println(message : Long) { System.out?.println(message) }
inline fun println(message : Byte) { System.out?.println(message) }
inline fun println(message : Short) { System.out?.println(message) }
inline fun println(message : Char) { System.out?.println(message) }
inline fun println(message : Boolean) { System.out?.println(message) }
inline fun println(message : Float) { System.out?.println(message) }
inline fun println(message : Double) { System.out?.println(message) }
inline fun println(message : CharArray) { System.out?.println(message) }
inline fun println() { System.out?.println() }
inline fun println(message : Any?) { System.out?.println(message) }
inline fun println(message : Int) { System.out?.println(message) }
inline fun println(message : Long) { System.out?.println(message) }
inline fun println(message : Byte) { System.out?.println(message) }
inline fun println(message : Short) { System.out?.println(message) }
inline fun println(message : Char) { System.out?.println(message) }
inline fun println(message : Boolean) { System.out?.println(message) }
inline fun println(message : Float) { System.out?.println(message) }
inline fun println(message : Double) { System.out?.println(message) }
inline fun println(message : CharArray) { System.out?.println(message) }
inline fun println() { System.out?.println() }
private val stdin : BufferedReader = BufferedReader(InputStreamReader(object : InputStream() {
override fun read() : Int {
return System.`in`?.read() ?: -1
}
override fun reset() {
System.`in`?.reset()
}
override fun read(b: ByteArray?): Int {
return System.`in`?.read(b) ?: -1
}
override fun close() {
System.`in`?.close()
}
override fun mark(readlimit: Int) {
System.`in`?.mark(readlimit)
}
override fun skip(n: Long): Long {
return System.`in`?.skip(n) ?: -1.lng
}
override fun available(): Int {
return System.`in`?.available() ?: 0
}
override fun markSupported(): Boolean {
return System.`in`?.markSupported() ?: false
}
override fun read(b: ByteArray?, off: Int, len: Int): Int {
return System.`in`?.read(b, off, len) ?: -1
}
}))
inline fun readLine() : String? = stdin.readLine()
/** Uses the given resource then closes it to ensure its closed again */
inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
var closed = false
try {
return block(this)
} catch (e: Exception) {
try {
this.close()
} catch (closeException: Exception) {
// eat the closeException as we are already throwing the original cause
// and we don't want to mask the real exception
}
throw e
} finally {
if (!closed) {
this.close()
}
}
private val stdin : BufferedReader = BufferedReader(InputStreamReader(object : InputStream() {
override fun read() : Int {
return System.`in`?.read() ?: -1
}
fun InputStream.iterator() : ByteIterator =
object: ByteIterator() {
override val hasNext : Boolean
get() = available() > 0
override fun reset() {
System.`in`?.reset()
}
override fun nextByte() = read().byt
}
override fun read(b: ByteArray?): Int {
return System.`in`?.read(b) ?: -1
}
inline fun InputStream.buffered(bufferSize: Int) = BufferedInputStream(this, bufferSize)
override fun close() {
System.`in`?.close()
}
inline val InputStream.reader : InputStreamReader
get() = InputStreamReader(this)
override fun mark(readlimit: Int) {
System.`in`?.mark(readlimit)
}
inline val InputStream.bufferedReader : BufferedReader
get() = BufferedReader(reader)
override fun skip(n: Long): Long {
return System.`in`?.skip(n) ?: -1.lng
}
inline fun InputStream.reader(charset: Charset) : InputStreamReader = InputStreamReader(this, charset)
override fun available(): Int {
return System.`in`?.available() ?: 0
}
inline fun InputStream.reader(charsetName: String) = InputStreamReader(this, charsetName)
override fun markSupported(): Boolean {
return System.`in`?.markSupported() ?: false
}
inline fun InputStream.reader(charsetDecoder: CharsetDecoder) = InputStreamReader(this, charsetDecoder)
override fun read(b: ByteArray?, off: Int, len: Int): Int {
return System.`in`?.read(b, off, len) ?: -1
}
}))
inline val InputStream.buffered : BufferedInputStream
get() = if(this is BufferedInputStream) this else BufferedInputStream(this)
inline fun readLine() : String? = stdin.readLine()
/** Uses the given resource then closes it to ensure its closed again */
/*
inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
var closed = false
try {
return block(this)
} catch (e: Exception) {
try {
this.close()
} catch (closeException: Exception) {
// eat the closeException as we are already throwing the original cause
// and we don't want to mask the real exception
}
throw e
} finally {
if (!closed) {
this.close()
}
}
}
*/
fun InputStream.iterator() : ByteIterator =
object: ByteIterator() {
override val hasNext : Boolean
get() = available() > 0
override fun nextByte() = read().byt
}
inline fun InputStream.buffered(bufferSize: Int) = BufferedInputStream(this, bufferSize)
inline val InputStream.reader : InputStreamReader
get() = InputStreamReader(this)
inline val InputStream.bufferedReader : BufferedReader
get() = BufferedReader(reader)
inline fun InputStream.reader(charset: Charset) : InputStreamReader = InputStreamReader(this, charset)
inline fun InputStream.reader(charsetName: String) = InputStreamReader(this, charsetName)
inline fun InputStream.reader(charsetDecoder: CharsetDecoder) = InputStreamReader(this, charsetDecoder)
inline val InputStream.buffered : BufferedInputStream
get() = if(this is BufferedInputStream) this else BufferedInputStream(this)
// inline val Reader.buffered : BufferedReader
// get() = if(this is BufferedReader) this else BufferedReader(this)
inline fun Reader.buffered() = BufferedReader(this)
inline fun Reader.buffered() = BufferedReader(this)
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
/*
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
var nextLine: String? = null
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
var nextLine: String? = null
override val hasNext: Boolean
get() {
nextLine = reader.readLine()
return nextLine != null
}
override fun next(): String = nextLine.sure()
override val hasNext: Boolean
get() {
nextLine = reader.readLine()
return nextLine != null
}
}
override fun next(): String = nextLine.sure()
}
*/
+2 -2
View File
@@ -190,10 +190,10 @@ val <T> List<T>.tail : T?
get() {
val s = this.size
return if (s > 0) this.get(s - 1) else null
}
val <T> List<T>.last : T?
get() = this.tail
get() = this.tail
+6 -6
View File
@@ -9,18 +9,18 @@ import org.junit.runner.*
import org.junit.runner.notification.*
import junit.framework.*
fun assert(message: String, block: fun() : Boolean) {
fun assert(message: String, block: ()-> Boolean) {
val actual = block()
Assert.assertTrue(message, actual)
}
fun assert(block: fun() : Boolean) = assert(block.toString(), block)
fun assert(block: ()-> Boolean) = assert(block.toString(), block)
fun assertNot(message: String, block: fun() : Boolean) {
fun assertNot(message: String, block: ()-> Boolean) {
assert(message){ !block() }
}
fun assertNot(block: fun() : Boolean) = assertNot(block.toString(), block)
fun assertNot(block: ()-> Boolean) = assertNot(block.toString(), block)
fun assert(actual: Boolean, message: String) {
println("Answer: ${actual} for ${message}")
@@ -34,7 +34,7 @@ fun assertNull(actual: Any?, message: String = "") {
Assert.assertNull(message, actual)
}
fun fails(block: fun() : Any) {
fun fails(block: ()-> Any) {
try {
block()
Assert.fail("Expected an exception to be thrown")
@@ -43,7 +43,7 @@ fun fails(block: fun() : Any) {
}
}
fun todo(block: fun(): Any) {
fun todo(block: ()-> Any) {
println("TODO at " + Exception().getStackTrace()?.get(1) + " for " + block)
}
+6 -6
View File
@@ -6,24 +6,24 @@ import java.util.*
trait Traversable<T> {
/** Returns true if any elements in the collection match the given predicate */
fun any(predicate: fun(T): Boolean) : Boolean
fun any(predicate: (T)-> Boolean) : Boolean
/** Returns true if all elements in the collection match the given predicate */
fun all(predicate: fun(T): Boolean) : Boolean
fun all(predicate: (T)-> Boolean) : Boolean
/** Returns the first item in the collection which matches the given predicate or null if none matched */
fun find(predicate: fun(T): Boolean) : T?
fun find(predicate: (T)-> Boolean) : T?
/** Returns a new collection containing all elements in this collection which match the given predicate */
// TODO using: Collection<T> for the return type - I wonder if this exact type could be
// deduced somewhat from the This.Type; e.g. returning Set on a Set, Array on Array etc
fun filter(predicate: fun(T): Boolean) : Collection<T>
fun filter(predicate: (T)-> Boolean) : Collection<T>
/** Performs the given operation on each element inside the collection */
fun foreach(operation: fun(element: T) : Unit)
fun foreach(operation: (element: T)-> Unit)
/** Returns a new collection containing the results of applying the given function to each element in this collection */
fun <T, R> java.lang.Iterable<T>.map(transform : fun(T) : R) : Collection<R>
fun <T, R> java.lang.Iterable<T>.map(transform : (T)-> R) : Collection<R>
}
/**
+2 -2
View File
@@ -25,7 +25,7 @@ class CollectionTest() : TestSupport() {
data.all{it.length == 3}
}
assertNot {
data.all{s => s.startsWith("b")}
data.all{s -> s.startsWith("b")}
}
}
@@ -119,7 +119,7 @@ class CollectionTest() : TestSupport() {
we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-849
*/
val lengths = data.map<String,Int>{s => s.length}
val lengths = data.map<String,Int>{s -> s.length}
assert {
lengths.all{it == 3}
}
+2 -1
View File
@@ -5,12 +5,13 @@ import std.test.*
import std.io.*
import std.util.*
import java.io.*
import java.util.*
class IoTest() : TestSupport() {
val file = File("test/HelloWorld.txt")
fun testUse() {
val list = arrayList<String>()
val list = ArrayList<String>()
val reader = FileReader(file).buffered()
reader.close()
+1 -1
View File
@@ -10,7 +10,7 @@ import std.util.*
import java.util.*
class MapTest() : TestSupport() {
val data = HashMap<String, Int>()
val data: Map<String,Int> = HashMap<String, Int>()
fun testGetOrElse() {
val a = data.getOrElse("foo"){2}
+2 -2
View File
@@ -23,7 +23,7 @@ class SetTest() : TestSupport() {
data.all{it.length == 3}
}
assertNot {
data.all{s => s.startsWith("b")}
data.all{(s: String) -> s.startsWith("b")}
}
}
@@ -59,7 +59,7 @@ class SetTest() : TestSupport() {
we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-849
*/
val lengths = data.map<String,Int>{s => s.length}
val lengths = data.map<String,Int>{(s: String) -> s.length}
assert {
lengths.all{it == 3}
}