Fix forcing of VmValue during error rendering (#1629)

This commit is contained in:
Islon Scherer
2026-06-01 18:25:33 +02:00
committed by GitHub
parent c2652e0722
commit bfda4cc8c8
4 changed files with 92 additions and 7 deletions
@@ -1,12 +1,12 @@
res1 = "Cannot instantiate, or amend an instance of, external class `Int`."
res2 = "Cannot instantiate, or amend an instance of, external class `List`."
res3 = "Cannot instantiate, or amend an instance of, external class `List`."
res4 = "Object of type `new Person {}` cannot have an element."
res4 = "Object of type `wrongParent#Person` cannot have an element."
res5 = "Cannot instantiate abstract class `ValueRenderer`."
res6 = "Object of type `new Mapping {}` cannot have an element."
res6 = "Object of type `Mapping` cannot have an element."
res7 = "Cannot instantiate, or amend an instance of, external class `Int`."
res8 = "Cannot instantiate, or amend an instance of, external class `List`."
res9 = "Cannot instantiate, or amend an instance of, external class `List`."
res10 = "Object of type `new Person {}` cannot have an element."
res10 = "Object of type `wrongParent#Person` cannot have an element."
res11 = "Cannot instantiate abstract class `ValueRenderer`."
res12 = "Object of type `new Mapping {}` cannot have an element."
res12 = "Object of type `Mapping` cannot have an element."
@@ -0,0 +1,68 @@
/*
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* 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
*
* https://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 org.pkl.core.util
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatCode
import org.junit.jupiter.api.Test
import org.pkl.core.runtime.VmClass
import org.pkl.core.runtime.VmValue
import org.pkl.core.runtime.VmValueConverter
import org.pkl.core.runtime.VmValueVisitor
class ErrorMessagesTest {
private class UnforceableValue : VmValue() {
var forced = false
private set
override fun force(allowUndefinedValues: Boolean) {
forced = true
throw RuntimeException("value must not be forced while rendering an error message")
}
override fun accept(visitor: VmValueVisitor) {
visitor.visitString("lazy")
}
override fun <T : Any> accept(converter: VmValueConverter<T>, path: Iterable<Any>): T =
throw UnsupportedOperationException()
override fun equals(obj: Any?): Boolean = this === obj
override fun toString(): String {
force(true)
return "lazy"
}
override fun getVmClass(): VmClass = throw UnsupportedOperationException()
override fun export(): Any = throw UnsupportedOperationException()
override fun hashCode(): Int = System.identityHashCode(this)
}
@Test
fun `renders VmValue arguments without forcing them`() {
val value = UnforceableValue()
lateinit var message: String
assertThatCode { message = ErrorMessages.create("cannotIterateOverThisValue", value) }
.doesNotThrowAnyException()
assertThat(value.forced).isFalse()
assertThat(message).contains("lazy")
}
}