Add test for contracts

This commit is contained in:
Pavel Punegov
2018-10-29 20:25:45 +03:00
committed by Pavel Punegov
parent b3797c98ff
commit 9798decca0
2 changed files with 52 additions and 0 deletions
+5
View File
@@ -1183,6 +1183,11 @@ task classDelegation_linkTest(type: LinkKonanTest) {
lib = "codegen/classDelegation/linkTest_lib.kt"
}
task contracts(type: RunStandaloneKonanTest) {
flags = [ '-Xuse-experimental=kotlin.Experimental', '-tr' ]
source = "codegen/contracts/contracts.kt"
}
task delegatedProperty_simpleVal(type: RunKonanTest) {
goldValue = "x\n42\n"
source = "codegen/delegatedProperty/simpleVal.kt"
@@ -0,0 +1,47 @@
import kotlin.test.*
import kotlin.contracts.*
open class S
class P(val str: String = "P") : S()
@UseExperimental(kotlin.contracts.ExperimentalContracts::class)
fun check(actual: Boolean) {
contract { returns() implies actual }
assertTrue(actual)
}
@Test fun testContractForCast() {
val s: S = P()
check(s is P)
assertEquals(s.str, "P")
}
@Test fun testRequire() {
val s: S = P()
require(s is P)
assertEquals(s.str, "P")
}
@Test fun testNonNullSmartCast() {
val i: Int? = 1234
requireNotNull(i)
assertEquals(i, 1234)
}
@Test fun testRunLambdaForVal() {
val x: Int
run {
x = 42
}
assertEquals(x, 42)
}
@Test fun testIsNullString() {
assertEquals("STR", nullableString("str"))
assertEquals("", nullableString(null))
}
private fun nullableString(string: String?): String = if (string.isNullOrBlank()) "" else "STR"