function CtoolsMathExpressionTestCase::testCustomFunctions

Test custom function handling.

File

tests/math_expression.test, line 262

Class

CtoolsMathExpressionTestCase
Tests the MathExpression library of ctools.

Code

public function testCustomFunctions() {
    $math_expr = new ctools_math_expr();
    $number_a = mt_rand(5, 10);
    $number_b = mt_rand(5, 10);
    // Create a one-argument function.
    $math_expr->evaluate("f(x) = 2 * x");
    $this->assertEqual($number_a * 2, $math_expr->evaluate("f({$number_a})"));
    $this->assertEqual($number_b * 2, $math_expr->evaluate("f({$number_b})"));
    // Create a two-argument function.
    $math_expr->evaluate("g(x, y) = 2 * x + y");
    $this->assertEqual($number_a * 2 + $number_b, $math_expr->evaluate("g({$number_a}, {$number_b})"), "g({$number_a}, {$number_b})");
    // Use a custom function in another function.
    $this->assertEqual(($number_a * 2 + $number_b) * 2, $math_expr->evaluate("f(g({$number_a}, {$number_b}))"), "f(g({$number_a}, {$number_b}))");
}