backend/tests: add cast_null and unchecked_cast3
This commit is contained in:
committed by
SvyatoslavScherbina
parent
ef6c5364a6
commit
16ca882298
@@ -212,6 +212,11 @@ task cast_simple(type: RunKonanTest) {
|
||||
source = "codegen/basics/cast_simple.kt"
|
||||
}
|
||||
|
||||
task cast_null(type: RunKonanTest) {
|
||||
goldValue = "Ok\n"
|
||||
source = "codegen/basics/cast_null.kt"
|
||||
}
|
||||
|
||||
task unchecked_cast1(type: RunKonanTest) {
|
||||
goldValue = "17\n17\n42\n42\n"
|
||||
source = "codegen/basics/unchecked_cast1.kt"
|
||||
@@ -223,6 +228,11 @@ task unchecked_cast2(type: RunKonanTest) {
|
||||
source = "codegen/basics/unchecked_cast2.kt"
|
||||
}
|
||||
|
||||
task unchecked_cast3(type: RunKonanTest) {
|
||||
goldValue = "Ok\n"
|
||||
source = "codegen/basics/unchecked_cast3.kt"
|
||||
}
|
||||
|
||||
task null_check(type: RunKonanTest) {
|
||||
source = "codegen/basics/null_check.kt"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
fun main(args: Array<String>) {
|
||||
testCast(null, false)
|
||||
testCastToNullable(null, true)
|
||||
testCastToNullable(Test(), true)
|
||||
testCastToNullable("", false)
|
||||
testCastNotNullableToNullable(Test(), true)
|
||||
testCastNotNullableToNullable("", false)
|
||||
|
||||
println("Ok")
|
||||
}
|
||||
|
||||
class Test
|
||||
|
||||
fun ensure(b: Boolean) {
|
||||
if (!b) {
|
||||
println("Error")
|
||||
}
|
||||
}
|
||||
|
||||
fun testCast(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as Test
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
|
||||
fun testCastToNullable(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as Test?
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
|
||||
fun testCastNotNullableToNullable(x: Any, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as Test?
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
fun main(args: Array<String>) {
|
||||
testCast<Test>(Test(), true)
|
||||
testCast<Test>(null, false)
|
||||
testCastToNullable<Test>(null, true)
|
||||
|
||||
println("Ok")
|
||||
}
|
||||
|
||||
class Test
|
||||
|
||||
fun ensure(b: Boolean) {
|
||||
if (!b) {
|
||||
println("Error")
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : Any> testCast(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as T
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
|
||||
fun <T : Any> testCastToNullable(x: Any?, expectSuccess: Boolean) {
|
||||
try {
|
||||
x as T?
|
||||
} catch (e: Throwable) {
|
||||
ensure(!expectSuccess)
|
||||
return
|
||||
}
|
||||
ensure(expectSuccess)
|
||||
}
|
||||
Reference in New Issue
Block a user