Generating stub methods for read-only map and entry.

This commit is contained in:
Evgeny Gerashchenko
2013-09-25 00:11:31 +04:00
parent d1cfbbbad5
commit b4bb08c013
6 changed files with 143 additions and 4 deletions
@@ -0,0 +1,30 @@
class MyMap<K, V>: Map<K, V> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun containsKey(key: Any?): Boolean = false
override fun containsValue(value: Any?): Boolean = false
override fun get(key: Any?): V? = null
override fun keySet(): Set<K> = throw UnsupportedOperationException()
override fun values(): Collection<V> = throw UnsupportedOperationException()
override fun entrySet(): Set<Map.Entry<K, V>> = throw UnsupportedOperationException()
}
fun expectUoe(block: () -> Unit) {
try {
block()
throw AssertionError()
} catch (e: UnsupportedOperationException) {
}
}
fun box(): String {
val map = MyMap<String, Int>() as MutableMap<String, Int>
expectUoe { map.put("", 1) }
expectUoe { map.remove("") }
expectUoe { map.putAll(map) }
expectUoe { map.clear() }
return "OK"
}
@@ -0,0 +1,15 @@
class MyMapEntry<K, V>: Map.Entry<K, V> {
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
override fun getKey(): K = throw UnsupportedOperationException()
override fun getValue(): V = throw UnsupportedOperationException()
}
fun box(): String {
try {
(MyMapEntry<String, Int>() as MutableMap.MutableEntry<String, Int>).setValue(1)
throw AssertionError()
} catch (e: UnsupportedOperationException) {
return "OK"
}
}
@@ -0,0 +1,14 @@
class MyMapEntry<K, V>: Map.Entry<K, V> {
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = false
override fun getKey(): K = throw UnsupportedOperationException()
override fun getValue(): V = throw UnsupportedOperationException()
public fun setValue(value: V): V = value
}
fun box(): String {
(MyMapEntry<String, Int>() as MutableMap.MutableEntry<String, Int>).setValue(1)
return "OK"
}
@@ -0,0 +1,27 @@
class MyMap<K, V>: Map<K, V> {
override fun size(): Int = 0
override fun isEmpty(): Boolean = true
override fun containsKey(key: Any?): Boolean = false
override fun containsValue(value: Any?): Boolean = false
override fun get(key: Any?): V? = null
override fun keySet(): Set<K> = throw UnsupportedOperationException()
override fun values(): Collection<V> = throw UnsupportedOperationException()
override fun entrySet(): Set<Map.Entry<K, V>> = throw UnsupportedOperationException()
public fun put(key: K, value: V): V? = null
public fun remove(key: Any?): V? = null
public fun putAll(m: Map<out K, V>) {}
public fun clear() {}
}
fun box(): String {
val map = MyMap<String, Int>() as MutableMap<String, Int>
map.put("", 1)
map.remove("")
map.putAll(map)
map.clear()
return "OK"
}