KT-9377 Support is-checks for read-only collections

Additional tests.
This commit is contained in:
Dmitry Petrov
2015-10-02 12:44:24 +03:00
parent 06d9ff6a71
commit 1f69ae254d
4 changed files with 157 additions and 6 deletions
@@ -28,23 +28,23 @@ class MME : MutableMap.MutableEntry<String, String> {
override fun setValue(value: String): String = throw UnsupportedOperationException()
}
inline fun safeAsReturnsNull(operation: String, block: () -> Any?) {
inline fun safeAsReturnsNull(operation: String, cast: () -> Any?) {
try {
val x = block()
val x = cast()
assert(x == null) { "$operation: should return null, got $x" }
}
catch (e: Throwable) {
assert(false) { "$operation: should not throw exceptions, got $e" }
throw AssertionError("$operation: should not throw exceptions, got $e")
}
}
inline fun safeAsReturnsNonNull(operation: String, block: () -> Any?) {
inline fun safeAsReturnsNonNull(operation: String, cast: () -> Any?) {
try {
val x = block()
val x = cast()
assert(x != null) { "$operation: should return non-null" }
}
catch (e: Throwable) {
assert(false) { "$operation: should not throw exceptions, got $e" }
throw AssertionError("$operation: should not throw exceptions, got $e")
}
}
@@ -108,6 +108,7 @@ fun box(): String {
safeAsReturnsNull("map as? MutableMap") { map as? MutableMap<*, *> }
safeAsReturnsNonNull("mmap as? MutableMap") { mmap as? MutableMap<*, *> }
safeAsReturnsNonNull("hashMap as? MutableMap") { hashMap as? MutableMap<*, *> }
val entry = ME() as Any
val mentry = MME()
@@ -129,5 +130,8 @@ fun box(): String {
safeAsReturnsNull("null as? MutableMap") { null as? MutableMap<*, *> }
safeAsReturnsNull("null as? MutableMap.MutableEntry") { null as? MutableMap.MutableEntry<*, *> }
safeAsReturnsNull("mlist as? MutableSet") { mlist as? MutableSet<*> }
safeAsReturnsNull("mlist as? MutableIterator") { mlist as? MutableIterator<*> }
return "OK"
}