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:
Ilya Matveev
2017-01-12 19:43:20 +07:00
parent d5988297b1
commit 1b553ebfaf
2643 changed files with 66666 additions and 0 deletions
@@ -0,0 +1,35 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
var foo = ""
var String.bar: String
get() = this
set(value) {}
class A(var baz: Int) {
var String.quux: String
get() = this
set(value) {}
}
fun box(): String {
assertEquals("<get-foo>", ::foo.getter.name)
assertEquals("<set-foo>", ::foo.setter.name)
assertEquals("<get-bar>", String::bar.getter.name)
assertEquals("<set-bar>", String::bar.setter.name)
assertEquals("<get-baz>", A::baz.getter.name)
assertEquals("<set-baz>", A::baz.setter.name)
val me = A::class.memberExtensionProperties.single() as KMutableProperty2<A, String, String>
assertEquals("<get-quux>", me.getter.name)
assertEquals("<set-quux>", me.setter.name)
return "OK"
}
@@ -0,0 +1,23 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.test.assertEquals
var state: String = ""
var String.prop: String
get() = length.toString()
set(value) { state = this + value }
fun box(): String {
val prop = String::prop
assertEquals("3", prop.getter.invoke("abc"))
assertEquals("5", prop.getter("defgh"))
prop.setter("O", "K")
return state
}
@@ -0,0 +1,25 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
class C(var state: String) {
var String.prop: String
get() = length.toString()
set(value) { state = this + value }
}
fun box(): String {
val prop = C::class.memberExtensionProperties.single() as KMutableProperty2<C, String, String>
val c = C("")
assertEquals("3", prop.getter.invoke(c, "abc"))
assertEquals("1", prop.getter(c, "d"))
prop.setter(c, "O", "K")
return c.state
}
@@ -0,0 +1,20 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.test.assertEquals
class C(var state: String)
fun box(): String {
val prop = C::state
val c = C("1")
assertEquals("1", prop.getter.invoke(c))
assertEquals("1", prop.getter(c))
prop.setter(c, "OK")
return prop.get(c)
}
@@ -0,0 +1,19 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.test.assertEquals
var state: String = ""
fun box(): String {
val prop = ::state
assertEquals("", prop.getter.invoke())
assertEquals("", prop.getter())
prop.setter("OK")
return prop.get()
}
@@ -0,0 +1,28 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.*
open class Super {
val a: Int = 1
val String.b: String get() = this
}
class Sub : Super() {
val c: Double = 1.0
val Char.d: Char get() = this
}
fun box(): String {
val sub = Sub::class
assertEquals(listOf("a", "c"), sub.memberProperties.map { it.name }.sorted())
assertEquals(listOf("b", "d"), sub.memberExtensionProperties.map { it.name }.sorted())
assertEquals(listOf("c"), sub.declaredMemberProperties.map { it.name })
assertEquals(listOf("d"), sub.declaredMemberExtensionProperties.map { it.name })
return "OK"
}
@@ -0,0 +1,24 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.*
class K(private val value: String)
fun box(): String {
val p = K::class.java.kotlin.memberProperties.single() as KProperty1<K, String>
try {
return p.get(K("Fail: private property should not be accessible by default"))
}
catch (e: IllegalCallableAccessException) {
// OK
}
p.isAccessible = true
return p.get(K("OK"))
}
@@ -0,0 +1,70 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J {
public String publicMemberJ;
private String privateMemberJ;
public static String publicStaticJ;
private static String privateStaticJ;
}
// FILE: K.kt
import kotlin.reflect.*
import kotlin.test.assertEquals
open class K : J() {
public val publicMemberK: String = ""
private val privateMemberK: String = ""
public val Any.publicMemberExtensionK: String get() = ""
private val Any.privateMemberExtensionK: String get() = ""
}
class L : K()
fun Collection<KProperty<*>>.names(): Set<String> =
this.map { it.name }.toSet()
fun check(c: Collection<KProperty<*>>, names: Set<String>) {
assertEquals(names, c.names())
}
fun box(): String {
val j = J::class
check(j.staticProperties,
setOf("publicStaticJ", "privateStaticJ"))
check(j.declaredMemberProperties,
setOf("publicMemberJ", "privateMemberJ"))
check(j.declaredMemberExtensionProperties,
emptySet())
check(j.memberProperties, j.declaredMemberProperties.names())
check(j.memberExtensionProperties, emptySet())
val k = K::class
check(k.staticProperties,
emptySet())
check(k.declaredMemberProperties,
setOf("publicMemberK", "privateMemberK"))
check(k.declaredMemberExtensionProperties,
setOf("publicMemberExtensionK", "privateMemberExtensionK"))
check(k.memberProperties, setOf("publicMemberJ") + k.declaredMemberProperties.names())
check(k.memberExtensionProperties, k.declaredMemberExtensionProperties.names())
val l = L::class
check(l.staticProperties, emptySet())
check(l.declaredMemberProperties, emptySet())
check(l.declaredMemberExtensionProperties, emptySet())
check(l.memberProperties, setOf("publicMemberJ", "publicMemberK"))
check(l.memberExtensionProperties, setOf("publicMemberExtensionK"))
return "OK"
}
@@ -0,0 +1,20 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.*
open class Super(val r: String)
class Sub(r: String) : Super(r)
fun box(): String {
val props = Sub::class.java.kotlin.declaredMemberProperties
if (!props.isEmpty()) return "Fail $props"
val allProps = Sub::class.java.kotlin.memberProperties
assertEquals(listOf("r"), allProps.map { it.name })
return allProps.single().get(Sub("OK")) as String
}
@@ -0,0 +1,15 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
class A<T> {
val result = "OK"
}
fun box(): String {
val k: KProperty1<A<*>, *> = A::class.memberProperties.single()
return k.get(A<String>()) as String
}
@@ -0,0 +1,20 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// KT-13700 Exception obtaining descriptor for property reference
import kotlin.test.assertEquals
interface H<T> {
val parent : T?
}
interface A : H<A>
fun box(): String {
assertEquals("A?", A::parent.returnType.toString())
assertEquals("T?", H<A>::parent.returnType.toString())
return "OK"
}
@@ -0,0 +1,14 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.test.assertEquals
data class Box<T>(val element: T)
fun box(): String {
val p = Box<String>::element
assertEquals("val Box<T>.element: T", p.toString())
return p.call(Box("OK"))
}
@@ -0,0 +1,31 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
var storage = "before"
class A {
val String.readonly: String
get() = this
var String.mutable: String
get() = storage
set(value) { storage = value }
}
fun box(): String {
val props = A::class.java.kotlin.memberExtensionProperties
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableProperty2<A, *, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableProperty2<A, *, *>) { "Fail 2: $mutable" }
val a = A()
mutable as KMutableProperty2<A, String, String>
assert(mutable.get(a, "") == "before") { "Fail 3: ${mutable.get(a, "")}" }
mutable.set(a, "", "OK")
return mutable.get(a, "")
}
@@ -0,0 +1,24 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
class A(val readonly: String) {
var mutable: String = "before"
}
fun box(): String {
val props = A::class.java.kotlin.memberProperties
val readonly = props.single { it.name == "readonly" }
assert(readonly !is KMutableProperty1<A, *>) { "Fail 1: $readonly" }
val mutable = props.single { it.name == "mutable" }
assert(mutable is KMutableProperty1<A, *>) { "Fail 2: $mutable" }
val a = A("")
mutable as KMutableProperty1<A, String>
assert(mutable.get(a) == "before") { "Fail 3: ${mutable.get(a)}" }
mutable.set(a, "OK")
return mutable.get(a)
}
@@ -0,0 +1,12 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.declaredMemberProperties
class A(val foo: String)
fun box(): String {
return (A::class.declaredMemberProperties.single()).invoke(A("OK")) as String
}
@@ -0,0 +1,20 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// FILE: J.java
public class J {
public String result = null;
}
// FILE: K.kt
class K : J()
fun box(): String {
val k = K()
val p = K::result
if (p.get(k) != null) return "Fail"
p.set(k, "OK")
return p.get(k)
}
@@ -0,0 +1,36 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J {
public static String x;
static String packageLocalField;
}
// FILE: K.kt
import kotlin.test.assertEquals
fun box(): String {
val f = J::x
assertEquals("x", f.name)
assertEquals(f, J::class.members.single { it.name == "x" })
f.set("OK")
assertEquals("OK", J.x)
assertEquals("OK", f.getter())
val pl = J::packageLocalField.getter
try {
pl()
return "Fail: package local field must be inaccessible"
} catch (e: Exception) {
// OK
}
return f.get()
}
@@ -0,0 +1,52 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J extends K {
}
// FILE: K.kt
import kotlin.reflect.*
import kotlin.reflect.jvm.*
public open class K {
var prop: String = ":("
val Int.ext: Int get() = this
}
fun box(): String {
val j = J()
val prop = J::prop
if (prop !is KMutableProperty1<*, *>) return "Fail instanceof"
if (prop.name != "prop") return "Fail name: ${prop.name}"
if (prop.get(j) != ":(") return "Fail get before: ${prop.get(j)}"
prop.set(j, ":)")
if (prop.get(j) != ":)") return "Fail get after: ${prop.get(j)}"
if (prop == K::prop) return "Fail J::prop == K::prop (these are different properties)"
val klass = J::class.java.kotlin
if (klass.declaredMemberProperties.isNotEmpty()) return "Fail: declaredMemberProperties should be empty"
if (klass.declaredMemberExtensionProperties.isNotEmpty()) return "Fail: declaredMemberExtensionProperties should be empty"
val prop2 = klass.memberProperties.firstOrNull { it.name == "prop" } ?: "Fail: no 'prop' property in memberProperties"
if (prop != prop2) return "Fail: property references from :: and from properties differ: $prop != $prop2"
if (prop2 !is KMutableProperty1<*, *>) return "Fail instanceof 2"
(prop2 as KMutableProperty1<J, String>).set(j, "::)")
if (prop.get(j) != "::)") return "Fail get after 2: ${prop.get(j)}"
val ext = klass.memberExtensionProperties.firstOrNull { it.name == "ext" } ?: "Fail: no 'ext' property in memberExtensionProperties"
ext as KProperty2<J, Int, Int>
val fortyTwo = ext.get(j, 42)
if (fortyTwo != 42) return "Fail ext get: $fortyTwo"
return "OK"
}
@@ -0,0 +1,28 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
class A {
val foo: String = "member"
val Unit.foo: String get() = "extension"
}
fun box(): String {
run {
val foo: KProperty1<A, *> = A::class.java.kotlin.memberProperties.single()
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
assert(foo.get(A()) == "member") { "Fail value: ${foo.get(A())}" }
}
run {
val foo: KProperty2<A, *, *> = A::class.java.kotlin.memberExtensionProperties.single()
assert(foo.name == "foo") { "Fail name: $foo (${foo.name})" }
foo as KProperty2<A, Unit, *>
assert(foo.get(A(), Unit) == "extension") { "Fail value: ${foo.get(A(), Unit)}" }
}
return "OK"
}
@@ -0,0 +1,22 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J {
private String result = "Fail";
}
// FILE: K.kt
import kotlin.reflect.*
import kotlin.reflect.jvm.*
fun box(): String {
val a = J()
val p = J::class.members.single { it.name == "result" } as KMutableProperty1<J, String>
p.isAccessible = true
p.set(a, "OK")
return p.get(a)
}
@@ -0,0 +1,22 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J {
private static String result = "Fail";
}
// FILE: K.kt
import kotlin.reflect.*
import kotlin.reflect.jvm.*
fun box(): String {
val a = J()
val p = J::class.members.single { it.name == "result" } as KMutableProperty0<String>
p.isAccessible = true
p.set("OK")
return p.get()
}
@@ -0,0 +1,35 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J {
public String foo = "";
}
// FILE: K.kt
import kotlin.test.*
class K : J() {
fun getFoo(): String = "K"
}
fun box(): String {
val j = J()
val x = J::foo
x.set(j, "J")
assertEquals("J", x.get(j))
val k = K()
val y = K::foo
y.set(k, "K")
assertEquals("K", y.get(k))
assertEquals("K", x.get(k))
val z = K::getFoo
assertEquals("K", z.invoke(k))
return "OK"
}
@@ -0,0 +1,42 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
// FILE: J.java
public class J implements K {
private String foo;
@Override
public String getFoo() {
return foo;
}
@Override
public void setFoo(String s) {
foo = s;
}
}
// FILE: K.kt
import kotlin.test.assertEquals
import kotlin.reflect.KParameter
interface K {
var foo: String
}
fun box(): String {
val p = J::foo
assertEquals("foo", p.name)
if (p.parameters.size != 1) return "Should have only 1 parameter"
if (p.parameters.single().kind != KParameter.Kind.INSTANCE) return "Should have an instance parameter"
if (J::class.members.none { it == p }) return "No foo in members"
val j = J()
p.setter.call(j, "OK")
return p.getter.call(j)
}
@@ -0,0 +1,33 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.isAccessible
class Result {
private val value = "OK"
fun ref() = Result::class.memberProperties.single() as KProperty1<Result, String>
}
fun box(): String {
val p = Result().ref()
try {
p.get(Result())
return "Fail: private property is accessible by default"
} catch(e: IllegalCallableAccessException) { }
p.isAccessible = true
val r = p.get(Result())
p.isAccessible = false
try {
p.get(Result())
return "Fail: setAccessible(false) had no effect"
} catch(e: IllegalCallableAccessException) { }
return r
}
@@ -0,0 +1,35 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.isAccessible
class A {
private var value = 0
fun ref() = A::class.memberProperties.single() as KMutableProperty1<A, Int>
}
fun box(): String {
val a = A()
val p = a.ref()
try {
p.set(a, 1)
return "Fail: private property is accessible by default"
} catch(e: IllegalCallableAccessException) { }
p.isAccessible = true
p.set(a, 2)
p.get(a)
p.isAccessible = false
try {
p.set(a, 3)
return "Fail: setAccessible(false) had no effect"
} catch(e: IllegalCallableAccessException) { }
return "OK"
}
@@ -0,0 +1,13 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
open class A(private val p: Int)
class B : A(42)
fun box() =
if (B::class.memberProperties.isEmpty()) "OK"
else "Fail: invisible fake overrides should not appear in KClass.memberProperties"
@@ -0,0 +1,30 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.*
object Obj {
@JvmStatic
private var result: String = "Fail"
}
fun box(): String {
val p = Obj::class.members.single { it.name == "result" } as KMutableProperty1<Any?, String>
p.isAccessible = true
try {
p.set(null, "OK")
return "Fail: set should check that first argument is Obj"
} catch (e: IllegalArgumentException) {}
try {
p.get(null)
return "Fail: get should check that first argument is Obj"
} catch (e: IllegalArgumentException) {}
p.set(Obj, "OK")
return p.get(Obj)
}
@@ -0,0 +1,46 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.*
import kotlin.test.*
class A(private var foo: String)
fun box(): String {
val a = A("")
val foo = A::class.memberProperties.single() as KMutableProperty1<A, String>
assertTrue(!foo.isAccessible)
assertTrue(!foo.getter.isAccessible)
assertTrue(!foo.setter.isAccessible)
val setter = foo.setter
setter.isAccessible = true
assertTrue(setter.isAccessible)
assertTrue(foo.setter.isAccessible)
// After we invoked isAccessible on a setter, the underlying field and thus the getter are also accessible
assertTrue(foo.isAccessible)
assertTrue(foo.getter.isAccessible)
setter.call(a, "A")
assertEquals("A", foo.getter.call(a))
setter.isAccessible = false
assertFalse(setter.isAccessible)
assertFalse(foo.setter.isAccessible)
assertFalse(foo.getter.isAccessible)
assertFalse(foo.isAccessible)
val getter = foo.getter
getter.isAccessible = true
assertTrue(setter.isAccessible)
assertTrue(foo.setter.isAccessible)
assertTrue(foo.isAccessible)
assertTrue(foo.getter.isAccessible)
assertTrue(getter.isAccessible)
return "OK"
}
@@ -0,0 +1,22 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.*
class K<in T : String> {
private var t: T
get() = "OK" as T
set(value) {}
fun run(): String {
val p = K::class.memberProperties.single() as KMutableProperty1<K<String>, String>
p.isAccessible = true
p.set(this as K<String>, "")
return p.get(this) as String
}
}
fun box() = K<String>().run()
@@ -0,0 +1,25 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KMutableProperty1
class A {
class B(val result: String)
var p: A.B? = null
var q: Array<Array<A.B>>? = null
}
fun box(): String {
val a = A()
val aq = A::class.members.single { it.name == "q" } as KMutableProperty1<A, Array<Array<A.B>>>
aq.set(a, arrayOf(arrayOf(A.B("array"))))
if (a.q!![0][0].result != "array") return "Fail array"
val ap = A::class.members.single { it.name == "p" } as KMutableProperty1<A, A.B>
ap.set(a, A.B("OK"))
return a.p!!.result
}
@@ -0,0 +1,34 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.reflect.jvm.isAccessible
class A(param: String) {
protected var v: String = param
fun ref() = A::class.memberProperties.single() as KMutableProperty1<A, String>
}
fun box(): String {
val a = A(":(")
val f = a.ref()
try {
f.get(a)
return "Fail: protected property getter is accessible by default"
} catch (e: IllegalCallableAccessException) { }
try {
f.set(a, ":D")
return "Fail: protected property setter is accessible by default"
} catch (e: IllegalCallableAccessException) { }
f.isAccessible = true
f.set(a, ":)")
return if (f.get(a) != ":)") "Fail: ${f.get(a)}" else "OK"
}
@@ -0,0 +1,17 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.jvm.isAccessible
class Result {
public val value: String = "OK"
}
fun box(): String {
val p = Result::value
p.isAccessible = false
// setAccessible(false) should have no effect on the accessibility of a public reflection object
return p.get(Result())
}
@@ -0,0 +1,18 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// FILE: J.java
public class J extends K {
public final int value = 42;
}
// FILE: K.kt
open class K
fun box(): String {
val f = J::value
val a = J()
return if (f.get(a) == 42) "OK" else "Fail: ${f.get(a)}"
}
@@ -0,0 +1,28 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.*
class A(param: String) {
val int: Int get() = 42
val string: String = param
var anyVar: Any? = null
val List<IntRange>.extensionToList: Unit get() {}
fun notAProperty() {}
}
fun box(): String {
val klass = A::class.java.kotlin
val props = klass.memberProperties
val names = props.map { it.name }.sorted()
assert(names == listOf("anyVar", "int", "string")) { "Fail names: $props" }
val stringProp = props.firstOrNull { it.name == "string" } ?: return "Fail, string not found: $props"
return stringProp.get(A("OK")) as String
}