Merge boxWithStdlib testData into box, delete BoxWithStdlib test

This commit is contained in:
Alexander Udalov
2016-03-07 13:36:14 +03:00
committed by Alexander Udalov
parent 22bfc9786a
commit 06a67e6602
535 changed files with 3520 additions and 3871 deletions
@@ -0,0 +1,32 @@
// 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,20 @@
// 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,22 @@
// 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,17 @@
// 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,16 @@
// 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,25 @@
// 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,21 @@
// 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,17 @@
// 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,12 @@
// 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,28 @@
// 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,21 @@
// 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,9 @@
// 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,25 @@
// 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,30 @@
// 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,32 @@
// 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,10 @@
// 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,27 @@
// 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,43 @@
// 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,19 @@
// 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,22 @@
// 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,31 @@
// 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,14 @@
// WITH_REFLECT
import kotlin.reflect.jvm.accessible
class Result {
public val value: String = "OK"
}
fun box(): String {
val p = Result::value
p.accessible = false
// setAccessible(false) should have no effect on the accessibility of a public reflection object
return p.get(Result())
}
@@ -0,0 +1,25 @@
// 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
}