diff --git a/lynglib/build.gradle.kts b/lynglib/build.gradle.kts index daf1234..b5eaecb 100644 --- a/lynglib/build.gradle.kts +++ b/lynglib/build.gradle.kts @@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.dsl.JvmTarget 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 diff --git a/lynglib/src/commonTest/kotlin/net/sergeych/lyng/PrintlnOverrideTest.kt b/lynglib/src/commonTest/kotlin/net/sergeych/lyng/PrintlnOverrideTest.kt new file mode 100644 index 0000000..c68ab8c --- /dev/null +++ b/lynglib/src/commonTest/kotlin/net/sergeych/lyng/PrintlnOverrideTest.kt @@ -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() + + // Override println with addFn + scope.addFn("println") { + val list = mutableListOf() + 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() + + // 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) + } +}