Skip to content

Commit

Permalink
Fix append of VoidValue
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Jun 24, 2024
1 parent 0c6ed4a commit 9ad0bc4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ import eu.iamgio.quarkdown.function.value.VoidValue
* These two values are then joined together by this [AppendExpressionVisitor], producing:
* `StringValue(three plus two is 5)`
*
* The same principle applies to 'block expressions':
* ```
* .if {...}
* Item 1
* .foreach {2..4}
* n:
* Item .n
* Item 5
* ```
* The previous example contains a body composed of multiple expressions:
* - `StringValue(Item 1)`;
* - `FunctionCall(foreach)` which returns an `IterableValue` of 3 elements;
* - `StringValue(Item 5)`.
* After appending these values, the resulting expression is an `IterableValue` (a [GeneralCollectionValue] in particular)
* which contains: `Item 1`, `Item 2`, `Item 3`, `Item 4`, `Item 5`.
*
* @param other expression to append to the visited expression
* @see ComposedExpression
*/
Expand All @@ -51,6 +67,10 @@ class AppendExpressionVisitor(private val other: Expression) : ExpressionVisitor
private fun Value<*>.concatenate(): InputValue<*> {
val otherEval = other.eval() // Evaluate the next expression.

// Void values are ignored.
if (this is VoidValue) return otherEval as InputValue<*>
if (otherEval is VoidValue) return this as InputValue<*>

// If the other value is a collection, add the current value to it as the first element.
if (otherEval is IterableValue<*> && this is OutputValue<*>) {
return GeneralCollectionValue(listOf(this, *otherEval.unwrappedValue.toList().toTypedArray()))
Expand Down
26 changes: 26 additions & 0 deletions test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ class FullPipelineTest {
assertFailsWith<InvalidArgumentCountException> {
execute(".function {hello}\n target:\n `Hello` .target!\n\n.hello") {}
}

execute(
"""
.if {yes}
.function {hello}
name:
Hello, *.name*!
# .hello {world}
.hello {iamgio}
""".trimIndent(),
) {
assertEquals("<h1>Hello, <em>world</em>!</h1><p>Hello, <em>iamgio</em>!</p>", it)
}
}

@Test
Expand All @@ -253,6 +267,18 @@ class FullPipelineTest {
) {
assertEquals("<p>Hello</p>", it)
}

execute(
"""
.var {x} {no}
.if {.x}
Hi
.ifnot {.x}
Hello
""".trimIndent(),
) {
assertEquals("<p>Hello</p>", it)
}
}

@Test
Expand Down

0 comments on commit 9ad0bc4

Please sign in to comment.