added tests of println override

This commit is contained in:
Sergey Chernov 2026-03-09 11:54:30 +03:00
parent 1502a365bf
commit f03006ce37
2 changed files with 88 additions and 1 deletions

View File

@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych" group = "net.sergeych"
version = "1.5.0-SNAPSHOT" version = "1.5.1-SNAPSHOT"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below // Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below

View File

@ -0,0 +1,87 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.bridge.globalBinder
import net.sergeych.lyng.obj.ObjVoid
import kotlin.test.Test
import kotlin.test.assertEquals
class PrintlnOverrideTest {
@Test
fun testPrintlnOverrideWithAddFn() = runTest {
val scope = Script.newScope()
val output = mutableListOf<String>()
// Override println with addFn
scope.addFn("println") {
val list = mutableListOf<String>()
for (a in args.list) {
list.add(toStringOf(a).value)
}
output.add(list.joinToString(" "))
ObjVoid
}
scope.createChildScope().eval("""
println("top level")
fun nested() {
println("inside function")
fun deep_nested() {
println("deeply nested")
}
deep_nested()
}
nested()
if (true) {
println("inside block")
}
""".trimIndent())
assertEquals(listOf("top level", "inside function", "deeply nested", "inside block"), output)
}
@Test
fun testPrintlnOverrideWithGlobalBinder() = runTest {
val scope = Script.newScope()
val output = mutableListOf<String>()
// Override println with globalBinder
scope.globalBinder().bindGlobalFun("println") {
val sb = StringBuilder()
for (i in 0 until args.size) {
if (i > 0) sb.append(" ")
sb.append(string(i))
}
output.add(sb.toString())
ObjVoid
}
scope.eval("""
println("gb top level")
fun gb_nested() {
println("gb inside function")
}
gb_nested()
""".trimIndent())
assertEquals(listOf("gb top level", "gb inside function"), output)
}
}