mirror of
https://github.com/apple/pkl.git
synced 2026-05-25 16:19:20 +02:00
Fix pkl:test fact power assertions when member source section is unavailable (#1571)
Power assertions only work when the source section is available. If it is unavailable, power assertions throw a ParserError (unexpected EOF on an empty input) when re-parsing the expression for presentation.
This commit is contained in:
@@ -30,6 +30,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import org.pkl.core.PklBugException;
|
||||||
import org.pkl.core.ast.ConstantValueNode;
|
import org.pkl.core.ast.ConstantValueNode;
|
||||||
import org.pkl.core.ast.expression.member.InferParentWithinMethodNode;
|
import org.pkl.core.ast.expression.member.InferParentWithinMethodNode;
|
||||||
import org.pkl.core.ast.expression.member.InferParentWithinObjectMethodNode;
|
import org.pkl.core.ast.expression.member.InferParentWithinObjectMethodNode;
|
||||||
@@ -83,6 +84,10 @@ public class PowerAssertions {
|
|||||||
SourceSection sourceSection,
|
SourceSection sourceSection,
|
||||||
Map<Node, List<Object>> trackedValues,
|
Map<Node, List<Object>> trackedValues,
|
||||||
@Nullable Consumer<AnsiStringBuilder> firstFrameSuffix) {
|
@Nullable Consumer<AnsiStringBuilder> firstFrameSuffix) {
|
||||||
|
if (!sourceSection.isAvailable()) {
|
||||||
|
throw new PklBugException("Power assertions require an available source section");
|
||||||
|
}
|
||||||
|
|
||||||
out.appendSandboxed(
|
out.appendSandboxed(
|
||||||
() -> {
|
() -> {
|
||||||
var lines = lines(sourceSection);
|
var lines = lines(sourceSection);
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ public final class TestRunner {
|
|||||||
try {
|
try {
|
||||||
var factValue = VmUtils.readMember(listing, idx);
|
var factValue = VmUtils.readMember(listing, idx);
|
||||||
if (factValue == Boolean.FALSE) {
|
if (factValue == Boolean.FALSE) {
|
||||||
if (PowerAssertions.isEnabled()) {
|
if (PowerAssertions.isEnabled() && member.getSourceSection().isAvailable()) {
|
||||||
try (var valueTracker = valueTrackerFactory.create()) {
|
try (var valueTracker = valueTrackerFactory.create()) {
|
||||||
listing.cachedValues.clear();
|
listing.cachedValues.clear();
|
||||||
VmUtils.readMember(listing, idx);
|
VmUtils.readMember(listing, idx);
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import org.assertj.core.api.Assertions.assertThatCode
|
|||||||
import org.junit.jupiter.api.AfterAll
|
import org.junit.jupiter.api.AfterAll
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.Timeout
|
import org.junit.jupiter.api.Timeout
|
||||||
|
import org.junit.jupiter.api.assertDoesNotThrow
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
import org.junit.jupiter.api.condition.EnabledOnOs
|
import org.junit.jupiter.api.condition.EnabledOnOs
|
||||||
import org.junit.jupiter.api.condition.OS
|
import org.junit.jupiter.api.condition.OS
|
||||||
@@ -695,6 +696,32 @@ class EvaluatorTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `power assertions work with test facts with unavailable source section`() {
|
||||||
|
val evaluator =
|
||||||
|
with(EvaluatorBuilder.preconfigured()) {
|
||||||
|
powerAssertionsEnabled = true
|
||||||
|
build()
|
||||||
|
}
|
||||||
|
|
||||||
|
assertDoesNotThrow {
|
||||||
|
evaluator.evaluateTest(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
amends "pkl:test"
|
||||||
|
facts {
|
||||||
|
["foo"] {
|
||||||
|
...List(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
.trimIndent()
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun checkModule(module: PModule) {
|
private fun checkModule(module: PModule) {
|
||||||
assertThat(module.properties.size).isEqualTo(2)
|
assertThat(module.properties.size).isEqualTo(2)
|
||||||
assertThat(module.getProperty("name")).isEqualTo("pigeon")
|
assertThat(module.getProperty("name")).isEqualTo("pigeon")
|
||||||
|
|||||||
Reference in New Issue
Block a user