function CtoolsMathExpressionTestCase::testVariables

Test variable handling.

File

tests/math_expression.test, line 221

Class

CtoolsMathExpressionTestCase
Tests the MathExpression library of ctools.

Code

public function testVariables() {
  $math_expr = new ctools_math_expr();
  // We should have a definition of pi:
  $this->assertFloat(pi(), $math_expr->evaluate('pi'));
  // And a definition of e:
  $this->assertFloat(exp(1), $math_expr->evaluate('e'));
  $number_a = 5;
  $number_b = 10;
  // Store the first number and use it on a calculation.
  $math_expr->evaluate("var = {$number_a}");
  $this->assertEqual($number_a + $number_b, $math_expr->evaluate("var + {$number_b}"));
  // Change the value and check the new value is used.
  $math_expr->evaluate("var = {$number_b}");
  $this->assertEqual($number_b + $number_b, $math_expr->evaluate("var + {$number_b}"), "var + {$number_b}");
  // Store another number and use it on a calculation.
  $math_expr->evaluate("var = {$number_a}");
  $math_expr->evaluate("newvar = {$number_a}");
  $this->assertEqual($number_a + $number_a, $math_expr->evaluate('var + newvar'), 'var + newvar');
  $this->assertFloat($number_a / $number_b, $math_expr->evaluate("var / {$number_b}"), "var / {$number_b}");
}