From 22e7ae9ede9213f5ec5c476a3f3be884876a7f4e Mon Sep 17 00:00:00 2001 From: Giorgio Garofalo Date: Sat, 14 Sep 2024 12:24:11 +0200 Subject: [PATCH] Add full pipeline error tests --- .../iamgio/quarkdown/test/FullPipelineTest.kt | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt b/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt index 5a762f72..8f1869b8 100644 --- a/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt +++ b/test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt @@ -11,13 +11,19 @@ import eu.iamgio.quarkdown.document.size.Size import eu.iamgio.quarkdown.document.size.Sizes import eu.iamgio.quarkdown.flavor.quarkdown.QuarkdownFlavor import eu.iamgio.quarkdown.function.error.InvalidArgumentCountException +import eu.iamgio.quarkdown.function.error.InvalidFunctionCallException +import eu.iamgio.quarkdown.function.error.MismatchingArgumentTypeException +import eu.iamgio.quarkdown.function.error.UnresolvedReferenceException import eu.iamgio.quarkdown.pipeline.Pipeline import eu.iamgio.quarkdown.pipeline.PipelineHooks import eu.iamgio.quarkdown.pipeline.PipelineOptions +import eu.iamgio.quarkdown.pipeline.error.BasePipelineErrorHandler +import eu.iamgio.quarkdown.pipeline.error.PipelineErrorHandler import eu.iamgio.quarkdown.pipeline.error.StrictPipelineErrorHandler import eu.iamgio.quarkdown.stdlib.Stdlib import java.io.File import kotlin.test.Test +import kotlin.test.assertContains import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse @@ -36,6 +42,7 @@ class FullPipelineTest { * Executes a Quarkdown source. * @param source Quarkdown source to execute * @param options execution options + * @param errorHandler error handler to use * @param enableMediaStorage whether the media storage system should be enabled. * If enabled, nodes that reference media (e.g. images) will instead reference the path to the media on the local storage * @param hook action run after rendering. Parameters are the pipeline context and the rendered source @@ -43,6 +50,7 @@ class FullPipelineTest { private fun execute( source: String, options: MutableContextOptions = MutableContextOptions(enableAutomaticIdentifiers = false), + errorHandler: PipelineErrorHandler = StrictPipelineErrorHandler(), enableMediaStorage: Boolean = false, hook: Context.(CharSequence) -> Unit, ) { @@ -61,7 +69,7 @@ class FullPipelineTest { Pipeline( context, PipelineOptions( - errorHandler = StrictPipelineErrorHandler(), + errorHandler = errorHandler, workingDirectory = File(DATA_FOLDER), enableMediaStorage = enableMediaStorage, ), @@ -1102,4 +1110,64 @@ class FullPipelineTest { ) } } + + @Test + fun errors() { + assertFailsWith { + execute(".nonexistant") {} + } + + assertFailsWith { + execute(".sum {2}") {} + } + + assertFailsWith { + execute(".sum {2} {5} {9}") {} + } + + assertFailsWith { + execute(".sum {a} {3}") {} + } + + assertFailsWith { + execute(".if {hello}\n\t.sum {2} {3} {1}") {} + } + + assertFailsWith { + execute(".row alignment:{center} cross:{hello}\n\tHi") {} + } + + // Non-strict error handling. + + execute(".sum {a} {3}", errorHandler = BasePipelineErrorHandler()) { + assertTrue( + Regex( + "
" + + "

Error: sum

" + + "

" + + "Cannot call function sum\\(Number a, Number b\\) .+?" + + "

", + ).matches(it), + ) + } + + execute(".if {yes}\n\t.sum {a} {3}", errorHandler = BasePipelineErrorHandler()) { + assertContains(it, "

Error: sum

") + } + + execute(".if {yes}\n\t.row\n\t\t.sum {2} {1} {5}", errorHandler = BasePipelineErrorHandler()) { + assertContains(it, "

Error: sum

") + } + + execute(".if {yes}\n\t.column alignment:{x}\n\t\tHi", errorHandler = BasePipelineErrorHandler()) { + assertTrue( + Regex( + ".+?

Error: column

" + + ".+?" + + "Cannot call function column.+?No such element 'x' among values \\[.+?]" + + ".+", + ).matches(it), + ) + } + } }