backend/tests: Add blackbox tests from Kotlin JVM
Added tests from testData/codegen/box directory. There are blackbox tests in other directories and they are to be added.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
open class SuperFoo {
|
||||
public fun bar(): String {
|
||||
if (this is Foo) {
|
||||
superFoo() // Smart cast
|
||||
return baz() // Cannot be cast
|
||||
}
|
||||
return baz()
|
||||
}
|
||||
|
||||
public fun baz() = "OK"
|
||||
}
|
||||
|
||||
class Foo : SuperFoo() {
|
||||
public fun superFoo() {}
|
||||
}
|
||||
|
||||
fun box(): String = Foo().bar()
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// See also KT-7801
|
||||
class A
|
||||
|
||||
fun <T> test(v: T): T {
|
||||
val a: T = if (v !is A) v else v
|
||||
return a
|
||||
}
|
||||
|
||||
fun box() = test("OK")
|
||||
@@ -0,0 +1,13 @@
|
||||
class Wrapper<T>(var x: T)
|
||||
|
||||
inline fun <reified T> change(w: Wrapper<T>, x: Any?) {
|
||||
if (x is T) {
|
||||
w.x = x
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val w = Wrapper<String>("FAIL")
|
||||
change(w, "OK")
|
||||
return w.x
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
fun foo() = "OK"
|
||||
}
|
||||
|
||||
fun A?.bar() = if (this != null) foo() else "FAIL"
|
||||
|
||||
fun box() = A().bar()
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
open class A {
|
||||
open val a = "OK"
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override val a = "FAIL"
|
||||
fun foo() = "CRUSH"
|
||||
}
|
||||
|
||||
class C {
|
||||
fun A?.complex(): String {
|
||||
if (this is B) return foo()
|
||||
else if (this != null) return a
|
||||
else return "???"
|
||||
}
|
||||
|
||||
fun bar() = A().complex()
|
||||
}
|
||||
|
||||
fun box() = C().bar()
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
open class A {
|
||||
class B : A() {
|
||||
val a = "FAIL"
|
||||
}
|
||||
|
||||
fun foo(): String {
|
||||
if (this is B) return a
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun box(): String = A().foo()
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
open class A {
|
||||
fun f(): String =
|
||||
when (this) {
|
||||
is B -> x
|
||||
else -> "FAIL"
|
||||
}
|
||||
}
|
||||
|
||||
class B(val x: String) : A()
|
||||
|
||||
fun box() = B("OK").f()
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
open class A {
|
||||
open fun foo() = "FAIL"
|
||||
|
||||
fun bar() = if (this is C) foo() else foo()
|
||||
}
|
||||
|
||||
open class B : A()
|
||||
|
||||
open class C : B() {
|
||||
override fun foo() = "OK"
|
||||
}
|
||||
|
||||
fun box() = C().bar()
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||
// IGNORE_BACKEND: JS
|
||||
|
||||
class Foo(val s: String)
|
||||
fun foo(): Foo? = Foo("OK")
|
||||
|
||||
fun <T> run(f: () -> T): T = f()
|
||||
|
||||
val foo: Foo = run {
|
||||
val x = foo()
|
||||
if (x == null) throw Exception()
|
||||
x
|
||||
}
|
||||
|
||||
fun box() = foo.s
|
||||
@@ -0,0 +1,8 @@
|
||||
fun String?.foo() = this ?: "OK"
|
||||
|
||||
fun foo(i: Int?): String {
|
||||
if (i == null) return i.foo()
|
||||
return "$i"
|
||||
}
|
||||
|
||||
fun box() = foo(null)
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class A(val s: String = "FAIL")
|
||||
|
||||
private fun foo(a: A?, aOther: A?): A {
|
||||
return if (a == null) {
|
||||
A()
|
||||
}
|
||||
else {
|
||||
if (aOther == null) {
|
||||
return A()
|
||||
}
|
||||
aOther
|
||||
}
|
||||
}
|
||||
|
||||
fun box() = foo(A("???"), A("OK")).s
|
||||
@@ -0,0 +1,9 @@
|
||||
fun baz(s: String?): Int {
|
||||
if (s == null) return 0
|
||||
return when(s) {
|
||||
"abc" -> s
|
||||
else -> "xyz"
|
||||
}.length
|
||||
}
|
||||
|
||||
fun box() = if (baz("abc") == 3 && baz("") == 3 && baz(null) == 0) "OK" else "FAIL"
|
||||
Reference in New Issue
Block a user