From 2ce6d8e4829f204d72d9263cde8118190908c594 Mon Sep 17 00:00:00 2001 From: sergeych Date: Wed, 7 Jan 2026 18:12:14 +0100 Subject: [PATCH] tools for error reporting in kotlin --- .../net/sergeych/lyng/obj/ObjException.kt | 11 ++++++++ .../src/commonTest/kotlin/MapLiteralTest.kt | 26 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjException.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjException.kt index 065e79a..10e591a 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjException.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjException.kt @@ -89,6 +89,17 @@ open class ObjException( override val objClass: ObjClass = exceptionClass + /** + * Tool to get kotlin string with error report including the Lyng stack trace. + */ + suspend fun toStringWithStackTrace(): String { + val l = getStackTrace().list + return buildString { + append(message.value) + for( t in l) + append("\n\tat ${t.toString(scope)}") + } + } override suspend fun defaultToString(scope: Scope): ObjString { val at = getStackTrace().list.firstOrNull()?.toString(scope) ?: ObjString("(unknown)") diff --git a/lynglib/src/commonTest/kotlin/MapLiteralTest.kt b/lynglib/src/commonTest/kotlin/MapLiteralTest.kt index 0f72ac1..b0dbfba 100644 --- a/lynglib/src/commonTest/kotlin/MapLiteralTest.kt +++ b/lynglib/src/commonTest/kotlin/MapLiteralTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com + * 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. @@ -24,6 +24,7 @@ import net.sergeych.lyng.ExecutionError import net.sergeych.lyng.ScriptError import net.sergeych.lyng.eval import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertFailsWith class MapLiteralTest { @@ -42,6 +43,29 @@ class MapLiteralTest { ) } + @Test + fun testSimpleLiteral() = runTest { + assertEquals("""{"a":1,"b":2}""", eval("{ a: 1, b: 2 }").toJson().toString()) + assertEquals("""{"a":1,"b":2}""", eval(""" + { + a: 1, + b: 2 + } + """.trimIndent()).toJson().toString()) + assertEquals("""{"a":1,"b":2}""", eval(""" + object Contracts { + val POR = object { + fun f1() = 1 + fun f2() = 2 + } + } + { + a: Contracts.POR.f1(), + b: Contracts.POR.f2() + } + """.trimIndent()).toJson().toString()) + } + @Test fun spreadsAndOverwriteOrder() = runTest { eval(