Enable useCustomStringDelimiters for pkl test example output (#416)

This makes the `<file>-expected.pcf` file a bit easier to read when examples contain quotes or
backslashes.
This commit is contained in:
Lily Ballard
2024-04-18 12:58:05 -07:00
committed by GitHub
parent 81bfdb7cbd
commit 76f1b92039
2 changed files with 57 additions and 4 deletions

View File

@@ -263,7 +263,7 @@ public class TestRunner {
VmUtils.createSyntheticObjectProperty(Identifier.EXAMPLES, "examples", examples)),
0);
var builder = new StringBuilder();
new PcfRenderer(builder, " ", converter, false, false).renderDocument(outputFileContent);
new PcfRenderer(builder, " ", converter, false, true).renderDocument(outputFileContent);
try {
Files.writeString(outputFile, builder);
} catch (IOException e) {

View File

@@ -1,14 +1,17 @@
package org.pkl.core
import org.pkl.commons.createTempFile
import org.pkl.core.ModuleSource.path
import org.pkl.core.ModuleSource.text
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import org.pkl.commons.writeString
import org.pkl.core.ModuleSource.*
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.createFile
import kotlin.io.path.name
class EvaluateTestsTest {
@@ -200,7 +203,57 @@ class EvaluateTestsTest {
}
""".trimIndent())
}
@Test
fun `written examples use custom string delimiters`(@TempDir tempDir: Path) {
val file = tempDir.createTempFile(prefix = "example", suffix = ".pkl")
Files.writeString(file, """
amends "pkl:test"
examples {
["myStr"] {
"my \"string\""
}
}
""".trimIndent())
evaluator.evaluateTest(path(file), false)
val expectedFile = file.parent.resolve(file.fileName.toString() + "-expected.pcf")
assertThat(expectedFile).exists()
assertThat(expectedFile).hasContent("""
examples {
["myStr"] {
#"my "string""#
}
}
""".trimIndent())
}
// test for backwards compatibility
@Test
fun `examples that don't use custom string delimiters still pass`(@TempDir tempDir: Path) {
val file = tempDir.createTempFile(prefix = "example", suffix = ".pkl")
Files.writeString(file, """
amends "pkl:test"
examples {
["myStr"] {
"my \"string\""
}
}
""".trimIndent())
createExpected(file).writeString("""
examples {
["myStr"] {
"my \"string\""
}
}
""".trimIndent())
val result = evaluator.evaluateTest(path(file), false)
assertFalse(result.failed())
}
companion object {
private fun createExpected(path: Path): Path {
return path