Files
kotlin-fork/compiler/testData/codegen/box/assert/jvm/lambdaNotEvaluated.kt
T
Ivan Kylchik c7435ba760 Replace all occurrences of WITH_RUNTIME with WITH_STDLIB
We are going to deprecate `WITH_RUNTIME` directive. The main reason
behind this change is that `WITH_STDLIB` directive better describes
its meaning, specifically it will add kotlin stdlib to test's classpath.
2021-11-17 15:26:38 +03:00

53 lines
1.3 KiB
Kotlin
Vendored

// TARGET_BACKEND: JVM
// ASSERTIONS_MODE: jvm
// WITH_STDLIB
// If assertions are disabled, neither argument to assert should be evaluated.
// If assertions are enabled, both arguments should be evaluate to values before
// checking the assertion.
package assertions
interface Checker {
fun check(): Boolean
}
class Checker1 : Checker {
override fun check(): Boolean {
var result = true
val lam = {
result = false
{ "Assertion failure" }
}
assert(true, lam())
return result
}
}
class Checker2 : Checker {
override fun check(): Boolean {
var result = true
val lam = {
result = false
{ "Assertion failure" }
}
assert(true, lam())
return result
}
}
fun checkerWithAssertions(enabled: Boolean): Checker {
val loader = Checker::class.java.classLoader
loader.setPackageAssertionStatus("assertions", enabled)
val c = loader.loadClass(if (enabled) "assertions.Checker1" else "assertions.Checker2")
return c.newInstance() as Checker
}
fun box(): String {
var c = checkerWithAssertions(true)
if (c.check()) return "Fail 1"
c = checkerWithAssertions(false)
if (!c.check()) return "Fail 2"
return "OK"
}