math_expression_stack.test 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Tests the simple MathExpressionStack class.
  4. */
  5. class CtoolsMathExpressionStackTestCase extends DrupalWebTestCase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Math expressions stack',
  12. 'description' => 'Test the stack class of the math expression library.',
  13. 'group' => 'ctools',
  14. 'dependencies' => array('ctools'),
  15. );
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function setUp(array $modules = array()) {
  21. $modules[] = 'ctools';
  22. $modules[] = 'ctools_plugin_test';
  23. parent::setUp($modules);
  24. }
  25. /**
  26. * Test the math expression stack system.
  27. */
  28. public function testStack() {
  29. $stack = new ctools_math_expr_stack();
  30. // Test the empty stack.
  31. $this->assertNull($stack->last());
  32. $this->assertNull($stack->pop());
  33. // Add an element and see whether it's the right element.
  34. $value = $this->randomName();
  35. $stack->push($value);
  36. $this->assertIdentical($value, $stack->last());
  37. $this->assertIdentical($value, $stack->pop());
  38. $this->assertNull($stack->pop());
  39. // Add multiple elements and see whether they are returned in the right
  40. // order.
  41. $values = array($this->randomName(), $this->randomName(), $this->randomName());
  42. foreach ($values as $value) {
  43. $stack->push($value);
  44. }
  45. // Test the different elements at different positions with the last()
  46. // method.
  47. $count = count($values);
  48. foreach ($values as $key => $value) {
  49. $this->assertEqual($value, $stack->last($count - $key));
  50. }
  51. // Pass in a non-valid number to last.
  52. $non_valid_number = rand(10, 20);
  53. $this->assertNull($stack->last($non_valid_number));
  54. // Test the order of the poping.
  55. $values = array_reverse($values);
  56. foreach ($values as $key => $value) {
  57. $this->assertEqual($stack->last(), $value);
  58. $this->assertEqual($stack->pop(), $value);
  59. }
  60. $this->assertNull($stack->pop());
  61. }
  62. }