[orx-expression-evaluator, orx-keyframer] Split expression evaluator from orx-keyframer
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
import org.amshove.kluent.`should throw`
|
||||
import org.amshove.kluent.`with message`
|
||||
import org.amshove.kluent.invoking
|
||||
import org.openrndr.extra.keyframer.ExpressionException
|
||||
import org.openrndr.extra.keyframer.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'"
|
||||
}
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
import org.amshove.kluent.shouldBe
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.amshove.kluent.shouldBeNear
|
||||
import org.openrndr.extra.keyframer.FunctionExtensions
|
||||
import org.openrndr.extra.keyframer.evaluateExpression
|
||||
import org.openrndr.math.map
|
||||
|
||||
import kotlin.test.Test
|
||||
class TestExpressions {
|
||||
|
||||
|
||||
@Test
|
||||
fun `a value reference`() {
|
||||
val expression = "someValue"
|
||||
val result = evaluateExpression(expression, variables= mapOf("someValue" to 5.0))
|
||||
result?.shouldBeEqualTo(5.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a backticked value reference`() {
|
||||
val expression = "`some-value`"
|
||||
val result = evaluateExpression(expression, variables= 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)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import org.amshove.kluent.`should throw`
|
||||
import org.amshove.kluent.invoking
|
||||
import org.openrndr.extra.expressions.ExpressionException
|
||||
import kotlin.test.Test
|
||||
import org.openrndr.extra.keyframer.ExpressionException
|
||||
|
||||
import org.openrndr.extra.keyframer.Keyframer
|
||||
import org.openrndr.extra.keyframer.KeyframerFormat
|
||||
import java.io.File
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
import org.amshove.kluent.shouldBeNear
|
||||
import org.openrndr.extra.keyframer.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