[orx-expression-evaluator] Switch to antlr-kotlin for parser generation and make it a common kotlin module
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import org.amshove.kluent.invoking
|
||||
import org.amshove.kluent.`should throw`
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.openrndr.extra.expressions.ExpressionException
|
||||
import org.openrndr.extra.expressions.compileExpression
|
||||
|
||||
class TestCompiledExpression {
|
||||
@Test
|
||||
fun `a simple compiled expression`() {
|
||||
val expression = "someValue"
|
||||
val function = compileExpression(expression, constants = mutableMapOf("someValue" to 5.0))
|
||||
function().shouldBeEqualTo(5.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a compiled expression with updated context`() {
|
||||
val expression = "someValue"
|
||||
val context = mutableMapOf("someValue" to 5.0)
|
||||
val function = compileExpression(expression, constants = context)
|
||||
function().shouldBeEqualTo(5.0)
|
||||
context["someValue"] = 6.0
|
||||
function().shouldBeEqualTo(6.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an erroneous compiled expression`() {
|
||||
val expression = "1bork"
|
||||
invoking {
|
||||
compileExpression(expression, constants = mutableMapOf("someValue" to 5.0))
|
||||
} `should throw` ExpressionException::class
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import org.amshove.kluent.invoking
|
||||
import org.amshove.kluent.`should throw`
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.openrndr.extra.expressions.*
|
||||
|
||||
class TestCompiledFunctions {
|
||||
@Test
|
||||
fun `a simple compiled function1`() {
|
||||
val expression = "t"
|
||||
val function = compileFunction1(expression, "t")
|
||||
function(-5.0).shouldBeEqualTo(-5.0)
|
||||
function(5.0).shouldBeEqualTo(5.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a simple compiled function2`() {
|
||||
val expression = "x + y"
|
||||
val function = compileFunction2(expression, "x", "y")
|
||||
function(1.0, 2.0).shouldBeEqualTo(3.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a simple compiled function3`() {
|
||||
val expression = "x + y + z"
|
||||
val function = compileFunction3(expression, "x", "y", "z")
|
||||
function(1.0, 2.0, 3.0).shouldBeEqualTo(6.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.openrndr.extra.expressions.watchingExpression1
|
||||
import kotlin.test.Test
|
||||
|
||||
class TestExpressionDelegates {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
val state = object {
|
||||
var expression = "x * x"
|
||||
val function1 by watchingExpression1(::expression, "x")
|
||||
}
|
||||
state.function1(5.0).shouldBeEqualTo(25.0)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import org.amshove.kluent.`should throw`
|
||||
import org.amshove.kluent.`with message`
|
||||
import org.amshove.kluent.invoking
|
||||
import org.openrndr.extra.expressions.ExpressionException
|
||||
import org.openrndr.extra.expressions.evaluateExpression
|
||||
|
||||
import kotlin.test.Test
|
||||
|
||||
class TestExpressionErrors {
|
||||
|
||||
@Test
|
||||
fun `an expression with non-sensible writing`() {
|
||||
val expression = ")("
|
||||
invoking {
|
||||
evaluateExpression(expression)
|
||||
} `should throw` ExpressionException::class `with message` "parser error in expression: ')('; [line: 1, character: 0 , near: [@0,0:0=')',<21>,1:0] ]"
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an expression with equality instead of assign`() {
|
||||
val expression = "a == 5"
|
||||
invoking {
|
||||
evaluateExpression(expression)
|
||||
} `should throw` ExpressionException::class `with message` "parser error in expression: 'a == 5'; [line: 1, character: 3 , near: [@3,3:3='=',<19>,1:3] ]"
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an expression trying to reassign a number`() {
|
||||
val expression = "3 = 5"
|
||||
invoking {
|
||||
evaluateExpression(expression)
|
||||
} `should throw` ExpressionException::class `with message` "parser error in expression: '3 = 5'; [line: 1, character: 2 , near: [@2,2:2='=',<19>,1:2] ]"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an expression that uses non-existing functions`() {
|
||||
val expression = "notExisting(5)"
|
||||
invoking {
|
||||
evaluateExpression(expression)
|
||||
} `should throw` ExpressionException::class `with message` "error in evaluation of 'notExisting(5)': unresolved function: 'notExisting(x0)'"
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an expression that uses non-existing variables`() {
|
||||
val expression = "notExisting + 4"
|
||||
invoking {
|
||||
evaluateExpression(expression)
|
||||
} `should throw` ExpressionException::class `with message` "error in evaluation of 'notExisting+4': unresolved variable: 'notExisting'"
|
||||
}
|
||||
}
|
||||
134
orx-expression-evaluator/src/jvmTest/kotlin/TestExpressions.kt
Normal file
134
orx-expression-evaluator/src/jvmTest/kotlin/TestExpressions.kt
Normal file
@@ -0,0 +1,134 @@
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.amshove.kluent.shouldBeNear
|
||||
import org.openrndr.extra.expressions.FunctionExtensions
|
||||
import org.openrndr.extra.expressions.evaluateExpression
|
||||
|
||||
import kotlin.test.Test
|
||||
class TestExpressions {
|
||||
@Test
|
||||
fun `a value reference`() {
|
||||
val expression = "someValue"
|
||||
val result = evaluateExpression(expression, constants= mapOf("someValue" to 5.0))
|
||||
result?.shouldBeEqualTo(5.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a backticked value reference`() {
|
||||
val expression = "`some-value`"
|
||||
val result = evaluateExpression(expression, constants= mapOf("some-value" to 5.0))
|
||||
result?.shouldBeEqualTo(5.0)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `a function call`() {
|
||||
val expression = "sqrt(4.0)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a function call with the name in backticks`() {
|
||||
val expression = "`sqrt`(4.0)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two function calls`() {
|
||||
val expression = "sqrt(4.0) * sqrt(4.0)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(4.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two argument max function call`() {
|
||||
val expression = "max(0.0, 4.0)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(4.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two argument min function call`() {
|
||||
val expression = "min(8.0, 4.0)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(4.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `three argument function call`() {
|
||||
val expression = "mix(8.0, 4.0, 0.5)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(6.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `five argument function call`() {
|
||||
val expression = "map(0.0, 1.0, 0.0, 8.0, 0.5)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(4.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two argument function call, where argument order matters`() {
|
||||
val expression = "pow(2.0, 3.0)"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(8.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nested function call`() {
|
||||
val expression = "sqrt(min(8.0, 4.0))"
|
||||
val result = evaluateExpression(expression)
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension function0 call`() {
|
||||
val expression = "extension()"
|
||||
val result = evaluateExpression(expression, functions = FunctionExtensions(functions0 = mapOf("extension" to { 2.0 })))
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension function1 call`(){
|
||||
val expression = "extension(1.0)"
|
||||
val result = evaluateExpression(expression, functions = FunctionExtensions(functions1 = mapOf("extension" to { x -> x * 2.0 })))
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension function1 call with dashed name in backticks`(){
|
||||
val expression = "`extension-function`(1.0)"
|
||||
val result = evaluateExpression(expression, functions = FunctionExtensions(functions1 = mapOf("extension-function" to { x -> x * 2.0 })))
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension function2 call`() {
|
||||
val expression = "extension(1.0, 1.0)"
|
||||
val result = evaluateExpression(expression, functions = FunctionExtensions(functions2 = mapOf("extension" to { x, y -> x + y })))
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension function3 call`() {
|
||||
val expression = "extension(1.0, 1.0, 1.0)"
|
||||
val result = evaluateExpression(expression, functions = FunctionExtensions(functions3 = mapOf("extension" to { x, y, z -> x + y + z})))
|
||||
result?.shouldBeNear(3.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension function4 call`() {
|
||||
val expression = "extension(1.0, 1.0, 1.0, 1.0)"
|
||||
val result = evaluateExpression(expression, functions = FunctionExtensions(functions4 = mapOf("extension" to { x, y, z, w -> x + y + z + w})))
|
||||
result?.shouldBeNear(4.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension function5 call`() {
|
||||
val expression = "extension(1.0, 1.0, 1.0, 1.0, 1.0)"
|
||||
val result = evaluateExpression(expression, functions = FunctionExtensions(functions5 = mapOf("extension" to { x, y, z, w, u -> x + y + z + w + u})))
|
||||
result?.shouldBeNear(5.0, 10E-6)
|
||||
}
|
||||
}
|
||||
53
orx-expression-evaluator/src/jvmTest/kotlin/TestOperators.kt
Normal file
53
orx-expression-evaluator/src/jvmTest/kotlin/TestOperators.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
import org.amshove.kluent.shouldBeNear
|
||||
import org.openrndr.extra.expressions.evaluateExpression
|
||||
import kotlin.test.Test
|
||||
|
||||
class TestOperators {
|
||||
@Test
|
||||
fun `an addition operation`() {
|
||||
val result = evaluateExpression("1 + 2")
|
||||
result?.shouldBeNear(3.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a subtraction operation`() {
|
||||
val result = evaluateExpression("1 - 2")
|
||||
result?.shouldBeNear(-1.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a modulus operation`() {
|
||||
val result = evaluateExpression("4 % 2")
|
||||
result?.shouldBeNear(0.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a multiplication operation`() {
|
||||
val result = evaluateExpression("4 * 2")
|
||||
result?.shouldBeNear(8.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a division operation`() {
|
||||
val result = evaluateExpression("4 / 2")
|
||||
result?.shouldBeNear(2.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a multiplication and addition operation`() {
|
||||
val result = evaluateExpression("4 * 2 + 1")
|
||||
result?.shouldBeNear(9.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an addition and multiplication`() {
|
||||
val result = evaluateExpression("4 + 2 * 3")
|
||||
result?.shouldBeNear(10.0, 10E-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unary minus`() {
|
||||
val result = evaluateExpression("-4.0")
|
||||
result?.shouldBeNear(-4.0, 10E-6)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user