function CtoolsMathExpressionTestCase::testIf

Test conditional handling.

File

tests/math_expression.test, line 290

Class

CtoolsMathExpressionTestCase
Tests the MathExpression library of ctools.

Code

public function testIf() {
    $math_expr = new ctools_math_expr();
    $number_a = mt_rand(1, 10);
    $number_b = mt_rand(11, 20);
    foreach (range(1, 4) as $n) {
        // @todo: Doesn't work with negative numbers.
        if ($n == 2 || $n == 4) {
            
            //$number_a = -$number_a;
        }
        if ($n == 3 || $n == 4) {
            
            //$number_b = -$number_b;
        }
        $this->assertEqual($number_a, $math_expr->evaluate("if(1, {$number_a}, {$number_b})"), "if(1, {$number_a}, {$number_b})");
        $this->assertEqual($number_a, $math_expr->evaluate("if(1, {$number_a})", "if(1, {$number_a})"));
        $this->assertEqual($number_b, $math_expr->evaluate("if(0, {$number_a}, {$number_b})"), "if(0, {$number_a}, {$number_b})");
        // Also add an expression so ensure it's evaluated.
        $this->assertEqual($number_b, $math_expr->evaluate("if({$number_a} > {$number_b}, {$number_a}, {$number_b})"), "if({$number_a} > {$number_b}, {$number_a}, {$number_b})");
        $this->assertEqual($number_b, $math_expr->evaluate("if({$number_a} < {$number_b}, {$number_b}, {$number_a})"), "if({$number_a} < {$number_b}, {$number_b}, {$number_a})");
    }
}