better elvis + throw

+added run {}
This commit is contained in:
sergeych committed 2025-08-18 00:56:52 +03:00
1 parent 835333dfad
commit 95c1da60ed
4 files changed
+42

No files matched your search

@@ -376,6 +376,11 @@ class Compiler(
}
"throw" -> {
val s = parseThrowStatement()
operand = Accessor { s.execute(it).asReadonly }
}
else -> operand?.let { left ->
// selector: <lvalue>, '.' , <id>
// we replace operand with selector code, that
@@ -315,6 +315,9 @@ open class Obj {
args.firstAndOnly().callOn(copy(Arguments(thisObj)))
thisObj
}
addFn("run") {
args.firstAndOnly().callOn(this)
}
addFn("getAt") {
requireExactCount(1)
thisObj.getAt(this, requiredArg<Obj>(0))
@@ -2961,5 +2961,25 @@ class ScriptTest {
""".trimIndent())
}
@Test
fun testElvisAndThrow() = runTest {
eval("""
val x = assertThrows {
null ?: throw "test" + "x"
}
assertEquals( "testx", x.message)
""".trimIndent())
}
@Test
fun testElvisAndRunThrow() = runTest {
eval("""
val x = assertThrows {
null ?: run { throw "testx" }
}
assertEquals( "testx", x.message)
""".trimIndent())
}
}