Generating stub methods for read-only collection.

This commit is contained in:
Evgeny Gerashchenko
2013-09-24 17:24:02 +04:00
parent 5de9c2499d
commit 511c908ba9
3 changed files with 54 additions and 7 deletions
@@ -0,0 +1,32 @@
class MyCollection<T>: Collection<T> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun contains(o: Any?): Boolean = false
override fun iterator(): Iterator<T> = throw UnsupportedOperationException()
override fun toArray(): Array<Any?> = throw UnsupportedOperationException()
override fun <E> toArray(a: Array<out E>): Array<E> = throw UnsupportedOperationException()
override fun containsAll(c: Collection<Any?>): Boolean = false
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
}
fun expectUoe(block: () -> Any) {
try {
block()
throw AssertionError()
} catch (e: UnsupportedOperationException) {
}
}
fun box(): String {
val collection = MyCollection<String>() as MutableCollection<String>
expectUoe { collection.add("") }
expectUoe { collection.remove("") }
expectUoe { collection.addAll(collection) }
expectUoe { collection.removeAll(collection) }
expectUoe { collection.retainAll(collection) }
expectUoe { collection.clear() }
return "OK"
}