diff --git a/pkl-core/src/test/kotlin/org/pkl/core/ErrorColoringTest.kt b/pkl-core/src/test/kotlin/org/pkl/core/ErrorColoringTest.kt new file mode 100644 index 00000000..298a4d33 --- /dev/null +++ b/pkl-core/src/test/kotlin/org/pkl/core/ErrorColoringTest.kt @@ -0,0 +1,54 @@ +package org.pkl.core + +import org.assertj.core.api.Assertions.assertThat +import org.fusesource.jansi.Ansi +import org.junit.jupiter.api.* + +class ErrorColoringTest { + companion object { + val evaluator by lazy { Evaluator.preconfigured() } + + @AfterAll + @JvmStatic + fun afterAll() { + evaluator.close() + } + } + + private fun evaluate(program: String, expression: String): Any { + return evaluator.evaluateExpression(ModuleSource.text(program), expression) + } + + @BeforeEach + fun setup() { + // Enable colouring before each test + Ansi.setEnabled(true) + } + + @AfterEach + fun teardown() { + // Disable colouring after each test + Ansi.setEnabled(false) + } + + @Test + fun `simple error`() { + val error = assertThrows { evaluate("bar = 2", "bar = 15") } + + assertThat(error).message() + .contains("\u001B[31m–– Pkl Error ––\u001B[m") + .contains("\u001B[94m1 | \u001B[m") + .contains("\u001B[0;31m^") + } + + @Test + fun `repeated error`() { + val error = assertThrows { evaluate("""self: String = "Strings; if they were lazy, you could tie the knot on \(self.take(7))"""", "self") } + assertThat(error).message() + .contains("A stack overflow occurred.") + .contains("┌─ ") + .contains(" repetitions of:") + .contains("│ 1 | ") + .contains("^^^^") + } +}