math_expression_stack.test 1.8 KB

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