lazy messages for check/require
This commit is contained in:
parent
3dfe98a93d
commit
92cb088f36
@ -87,8 +87,23 @@ While not strictly for testing, these functions help in defensive programming:
|
||||
|
||||
Throws an `IllegalArgumentException` if the condition is false. Use this for validating function arguments.
|
||||
|
||||
If we want to evaluate the message lazily:
|
||||
|
||||
require(condition) { "requirement not met: %s"(someData) }
|
||||
|
||||
In this case, formatting will only occur if the condition is not met.
|
||||
|
||||
|
||||
|
||||
### `check`
|
||||
|
||||
check(condition, message="check failed")
|
||||
|
||||
Throws an `IllegalStateException` if the condition is false. Use this for validating internal state.
|
||||
|
||||
With lazy message evaluation:
|
||||
|
||||
check(condition) { "check failed: %s"(someData) }
|
||||
|
||||
In this case, formatting will only occur if the condition is not met.
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
group = "net.sergeych"
|
||||
version = "1.0.9-SNAPSHOT"
|
||||
version = "1.0.10-SNAPSHOT"
|
||||
|
||||
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
||||
|
||||
|
||||
@ -253,16 +253,18 @@ class Script(
|
||||
addFn("require") {
|
||||
val condition = requiredArg<ObjBool>(0)
|
||||
if (!condition.value) {
|
||||
val message = args.list.getOrNull(1)?.toString() ?: "requirement not met"
|
||||
raiseIllegalArgument(message)
|
||||
var message = args.list.getOrNull(1)
|
||||
if( message is Statement ) message = message.execute(this)
|
||||
raiseIllegalArgument(message?.toString() ?: "requirement not met")
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
addFn("check") {
|
||||
val condition = requiredArg<ObjBool>(0)
|
||||
if (!condition.value) {
|
||||
val message = args.list.getOrNull(1)?.toString() ?: "check failed"
|
||||
raiseIllegalState(message)
|
||||
var message = args.list.getOrNull(1)
|
||||
if( message is Statement ) message = message.execute(this)
|
||||
raiseIllegalState(message?.toString() ?: "check failed")
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
|
||||
@ -4297,4 +4297,14 @@ class ScriptTest {
|
||||
assertEquals(tf(...{ x: 3, y: 4, z: 50 }), "x=3, y=4, z=50")
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
|
||||
// @Test
|
||||
// fun testSplatAssignemnt() = runTest {
|
||||
// eval("""
|
||||
// val abc = [1, 2, 3]
|
||||
// val [a, b, c] = ...abc
|
||||
// println(a, b, c)
|
||||
// """.trimIndent())
|
||||
// }
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user