tests: Copy stdlib tests from Kotlin/JVM
This patch copies Kotlin/JVM stdlib tests (libraries/stdlib/test) excepting ones with explicit java dependencies. It also transforms the tests to use them as box-tests.
This commit is contained in:
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
import kotlin.test.*
|
||||
|
||||
import kotlin.properties.*
|
||||
|
||||
private class NotNullVarTestGeneric<T : Any>(val a1: String, val b1: T) {
|
||||
var a: String by Delegates.notNull()
|
||||
var b by Delegates.notNull<T>()
|
||||
|
||||
public fun doTest() {
|
||||
a = a1
|
||||
b = b1
|
||||
assertTrue(a == "a", "fail: a should be a, but was $a")
|
||||
assertTrue(b == "b", "fail: b should be b, but was $b")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
NotNullVarTestGeneric("a", "b").doTest()
|
||||
}
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
import kotlin.test.*
|
||||
|
||||
import kotlin.properties.*
|
||||
|
||||
class ObservablePropertyTest {
|
||||
var result = false
|
||||
|
||||
var b: Int by Delegates.observable(1, { property, old, new ->
|
||||
assertEquals("b", property.name)
|
||||
result = true
|
||||
assertEquals(new, b, "New value has already been set")
|
||||
})
|
||||
|
||||
fun doTest() {
|
||||
b = 4
|
||||
assertTrue(b == 4, "fail: b != 4")
|
||||
assertTrue(result, "fail: result should be true")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
ObservablePropertyTest().doTest()
|
||||
}
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
import kotlin.test.*
|
||||
|
||||
import kotlin.properties.*
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
import kotlin.properties.*
|
||||
|
||||
class A(val p: Boolean)
|
||||
|
||||
class VetoablePropertyTest {
|
||||
var result = false
|
||||
var b: A by Delegates.vetoable(A(true), { property, old, new ->
|
||||
assertEquals("b", property.name)
|
||||
assertEquals(old, b, "New value hasn't been set yet")
|
||||
result = new.p == true;
|
||||
result
|
||||
})
|
||||
|
||||
fun doTest() {
|
||||
val firstValue = A(true)
|
||||
b = firstValue
|
||||
assertTrue(b == firstValue, "fail1: b should be firstValue = A(true)")
|
||||
assertTrue(result, "fail2: result should be true")
|
||||
b = A(false)
|
||||
assertTrue(b == firstValue, "fail3: b should be firstValue = A(true)")
|
||||
assertFalse(result, "fail4: result should be false")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
VetoablePropertyTest().doTest()
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import kotlin.test.*
|
||||
|
||||
class ValByMapExtensionsTest {
|
||||
val map: Map<String, String> = hashMapOf("a" to "all", "b" to "bar", "c" to "code")
|
||||
val genericMap = mapOf<String, Any?>("i" to 1, "x" to 1.0)
|
||||
|
||||
val a by map
|
||||
val b: String by map
|
||||
val c: Any by map
|
||||
val d: String? by map
|
||||
val e: String by map.withDefault { "default" }
|
||||
val f: String? by map.withDefault { null }
|
||||
// val n: Int by map // prohibited by type system
|
||||
val i: Int by genericMap
|
||||
val x: Double by genericMap
|
||||
|
||||
|
||||
fun doTest() {
|
||||
assertEquals("all", a)
|
||||
assertEquals("bar", b)
|
||||
assertEquals("code", c)
|
||||
assertEquals("default", e)
|
||||
assertEquals(null, f)
|
||||
assertEquals(1, i)
|
||||
assertEquals(1.0, x)
|
||||
assertFailsWith<NoSuchElementException> { d }
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
ValByMapExtensionsTest().doTest()
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import kotlin.test.*
|
||||
|
||||
class VarByMapExtensionsTest {
|
||||
val map = hashMapOf<String, Any?>("a" to "all", "b" to null, "c" to 1, "xProperty" to 1.0)
|
||||
val map2: MutableMap<String, CharSequence> = hashMapOf("a2" to "all")
|
||||
|
||||
var a: String by map
|
||||
var b: Any? by map
|
||||
var c: Int by map
|
||||
var d: String? by map
|
||||
var a2: String by map2.withDefault { "empty" }
|
||||
//var x: Int by map2 // prohibited by type system
|
||||
|
||||
fun doTest() {
|
||||
assertEquals("all", a)
|
||||
assertEquals(null, b)
|
||||
assertEquals(1, c)
|
||||
c = 2
|
||||
assertEquals(2, c)
|
||||
assertEquals(2, map["c"])
|
||||
|
||||
assertEquals("all", a2)
|
||||
map2.remove("a2")
|
||||
assertEquals("empty", a2)
|
||||
|
||||
map["c"] = "string"
|
||||
// fails { c } // does not fail in JS due to KT-8135
|
||||
|
||||
map["a"] = null
|
||||
a // fails { a } // does not fail due to KT-8135
|
||||
|
||||
assertFailsWith<NoSuchElementException> { d }
|
||||
map["d"] = null
|
||||
assertEquals(null, d)
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
VarByMapExtensionsTest().doTest()
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.properties.*
|
||||
|
||||
class IdentityEqualsIsUsedToUnescapeLazyValTest {
|
||||
var equalsCalled = 0
|
||||
private val a by lazy { ClassWithCustomEquality { equalsCalled++ } }
|
||||
|
||||
fun doTest() {
|
||||
a
|
||||
a
|
||||
assertTrue(equalsCalled == 0, "fail: equals called $equalsCalled times.")
|
||||
}
|
||||
}
|
||||
|
||||
private class ClassWithCustomEquality(private val onEqualsCalled: () -> Unit) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
onEqualsCalled()
|
||||
return super.equals(other)
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
IdentityEqualsIsUsedToUnescapeLazyValTest().doTest()
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.properties.*
|
||||
|
||||
class LazyValTest {
|
||||
var result = 0
|
||||
val a by lazy {
|
||||
++result
|
||||
}
|
||||
|
||||
fun doTest() {
|
||||
a
|
||||
assertTrue(a == 1, "fail: initializer should be invoked only once")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
LazyValTest().doTest()
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.properties.*
|
||||
|
||||
class NullableLazyValTest {
|
||||
var resultA = 0
|
||||
var resultB = 0
|
||||
|
||||
val a: Int? by lazy { resultA++; null }
|
||||
val b by lazy { foo() }
|
||||
|
||||
fun doTest() {
|
||||
a
|
||||
b
|
||||
|
||||
assertTrue(a == null, "fail: a should be null")
|
||||
assertTrue(b == null, "fail: b should be null")
|
||||
assertTrue(resultA == 1, "fail: initializer for a should be invoked only once")
|
||||
assertTrue(resultB == 1, "fail: initializer for b should be invoked only once")
|
||||
}
|
||||
|
||||
fun foo(): String? {
|
||||
resultB++
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
NullableLazyValTest().doTest()
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.properties.*
|
||||
|
||||
class UnsafeLazyValTest {
|
||||
var result = 0
|
||||
val a by lazy(LazyThreadSafetyMode.NONE) {
|
||||
++result
|
||||
}
|
||||
|
||||
fun doTest() {
|
||||
a
|
||||
assertTrue(a == 1, "fail: initializer should be invoked only once")
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
UnsafeLazyValTest().doTest()
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.properties.*
|
||||
|
||||
class UnsafeNullableLazyValTest {
|
||||
var resultA = 0
|
||||
var resultB = 0
|
||||
|
||||
val a: Int? by lazy(LazyThreadSafetyMode.NONE) { resultA++; null }
|
||||
val b by lazy(LazyThreadSafetyMode.NONE) { foo() }
|
||||
|
||||
fun doTest() {
|
||||
a
|
||||
b
|
||||
|
||||
assertTrue(a == null, "fail: a should be null")
|
||||
assertTrue(b == null, "fail: a should be null")
|
||||
assertTrue(resultA == 1, "fail: initializer for a should be invoked only once")
|
||||
assertTrue(resultB == 1, "fail: initializer for b should be invoked only once")
|
||||
}
|
||||
|
||||
fun foo(): String? {
|
||||
resultB++
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
UnsafeNullableLazyValTest().doTest()
|
||||
}
|
||||
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user