BinaryNodeTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ExpressionLanguage\Tests\Node;
  11. use Symfony\Component\ExpressionLanguage\Node\ArrayNode;
  12. use Symfony\Component\ExpressionLanguage\Node\BinaryNode;
  13. use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
  14. class BinaryNodeTest extends AbstractNodeTest
  15. {
  16. public function getEvaluateData()
  17. {
  18. $array = new ArrayNode();
  19. $array->addElement(new ConstantNode('a'));
  20. $array->addElement(new ConstantNode('b'));
  21. return [
  22. [true, new BinaryNode('or', new ConstantNode(true), new ConstantNode(false))],
  23. [true, new BinaryNode('||', new ConstantNode(true), new ConstantNode(false))],
  24. [false, new BinaryNode('and', new ConstantNode(true), new ConstantNode(false))],
  25. [false, new BinaryNode('&&', new ConstantNode(true), new ConstantNode(false))],
  26. [0, new BinaryNode('&', new ConstantNode(2), new ConstantNode(4))],
  27. [6, new BinaryNode('|', new ConstantNode(2), new ConstantNode(4))],
  28. [6, new BinaryNode('^', new ConstantNode(2), new ConstantNode(4))],
  29. [true, new BinaryNode('<', new ConstantNode(1), new ConstantNode(2))],
  30. [true, new BinaryNode('<=', new ConstantNode(1), new ConstantNode(2))],
  31. [true, new BinaryNode('<=', new ConstantNode(1), new ConstantNode(1))],
  32. [false, new BinaryNode('>', new ConstantNode(1), new ConstantNode(2))],
  33. [false, new BinaryNode('>=', new ConstantNode(1), new ConstantNode(2))],
  34. [true, new BinaryNode('>=', new ConstantNode(1), new ConstantNode(1))],
  35. [true, new BinaryNode('===', new ConstantNode(true), new ConstantNode(true))],
  36. [false, new BinaryNode('!==', new ConstantNode(true), new ConstantNode(true))],
  37. [false, new BinaryNode('==', new ConstantNode(2), new ConstantNode(1))],
  38. [true, new BinaryNode('!=', new ConstantNode(2), new ConstantNode(1))],
  39. [-1, new BinaryNode('-', new ConstantNode(1), new ConstantNode(2))],
  40. [3, new BinaryNode('+', new ConstantNode(1), new ConstantNode(2))],
  41. [4, new BinaryNode('*', new ConstantNode(2), new ConstantNode(2))],
  42. [1, new BinaryNode('/', new ConstantNode(2), new ConstantNode(2))],
  43. [1, new BinaryNode('%', new ConstantNode(5), new ConstantNode(2))],
  44. [25, new BinaryNode('**', new ConstantNode(5), new ConstantNode(2))],
  45. ['ab', new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'))],
  46. [true, new BinaryNode('in', new ConstantNode('a'), $array)],
  47. [false, new BinaryNode('in', new ConstantNode('c'), $array)],
  48. [true, new BinaryNode('not in', new ConstantNode('c'), $array)],
  49. [false, new BinaryNode('not in', new ConstantNode('a'), $array)],
  50. [[1, 2, 3], new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
  51. [1, new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+$/'))],
  52. ];
  53. }
  54. public function getCompileData()
  55. {
  56. $array = new ArrayNode();
  57. $array->addElement(new ConstantNode('a'));
  58. $array->addElement(new ConstantNode('b'));
  59. return [
  60. ['(true || false)', new BinaryNode('or', new ConstantNode(true), new ConstantNode(false))],
  61. ['(true || false)', new BinaryNode('||', new ConstantNode(true), new ConstantNode(false))],
  62. ['(true && false)', new BinaryNode('and', new ConstantNode(true), new ConstantNode(false))],
  63. ['(true && false)', new BinaryNode('&&', new ConstantNode(true), new ConstantNode(false))],
  64. ['(2 & 4)', new BinaryNode('&', new ConstantNode(2), new ConstantNode(4))],
  65. ['(2 | 4)', new BinaryNode('|', new ConstantNode(2), new ConstantNode(4))],
  66. ['(2 ^ 4)', new BinaryNode('^', new ConstantNode(2), new ConstantNode(4))],
  67. ['(1 < 2)', new BinaryNode('<', new ConstantNode(1), new ConstantNode(2))],
  68. ['(1 <= 2)', new BinaryNode('<=', new ConstantNode(1), new ConstantNode(2))],
  69. ['(1 <= 1)', new BinaryNode('<=', new ConstantNode(1), new ConstantNode(1))],
  70. ['(1 > 2)', new BinaryNode('>', new ConstantNode(1), new ConstantNode(2))],
  71. ['(1 >= 2)', new BinaryNode('>=', new ConstantNode(1), new ConstantNode(2))],
  72. ['(1 >= 1)', new BinaryNode('>=', new ConstantNode(1), new ConstantNode(1))],
  73. ['(true === true)', new BinaryNode('===', new ConstantNode(true), new ConstantNode(true))],
  74. ['(true !== true)', new BinaryNode('!==', new ConstantNode(true), new ConstantNode(true))],
  75. ['(2 == 1)', new BinaryNode('==', new ConstantNode(2), new ConstantNode(1))],
  76. ['(2 != 1)', new BinaryNode('!=', new ConstantNode(2), new ConstantNode(1))],
  77. ['(1 - 2)', new BinaryNode('-', new ConstantNode(1), new ConstantNode(2))],
  78. ['(1 + 2)', new BinaryNode('+', new ConstantNode(1), new ConstantNode(2))],
  79. ['(2 * 2)', new BinaryNode('*', new ConstantNode(2), new ConstantNode(2))],
  80. ['(2 / 2)', new BinaryNode('/', new ConstantNode(2), new ConstantNode(2))],
  81. ['(5 % 2)', new BinaryNode('%', new ConstantNode(5), new ConstantNode(2))],
  82. ['pow(5, 2)', new BinaryNode('**', new ConstantNode(5), new ConstantNode(2))],
  83. ['("a" . "b")', new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'))],
  84. ['in_array("a", [0 => "a", 1 => "b"])', new BinaryNode('in', new ConstantNode('a'), $array)],
  85. ['in_array("c", [0 => "a", 1 => "b"])', new BinaryNode('in', new ConstantNode('c'), $array)],
  86. ['!in_array("c", [0 => "a", 1 => "b"])', new BinaryNode('not in', new ConstantNode('c'), $array)],
  87. ['!in_array("a", [0 => "a", 1 => "b"])', new BinaryNode('not in', new ConstantNode('a'), $array)],
  88. ['range(1, 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
  89. ['preg_match("/^[a-z]+/i\$/", "abc")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+/i$/'))],
  90. ];
  91. }
  92. public function getDumpData()
  93. {
  94. $array = new ArrayNode();
  95. $array->addElement(new ConstantNode('a'));
  96. $array->addElement(new ConstantNode('b'));
  97. return [
  98. ['(true or false)', new BinaryNode('or', new ConstantNode(true), new ConstantNode(false))],
  99. ['(true || false)', new BinaryNode('||', new ConstantNode(true), new ConstantNode(false))],
  100. ['(true and false)', new BinaryNode('and', new ConstantNode(true), new ConstantNode(false))],
  101. ['(true && false)', new BinaryNode('&&', new ConstantNode(true), new ConstantNode(false))],
  102. ['(2 & 4)', new BinaryNode('&', new ConstantNode(2), new ConstantNode(4))],
  103. ['(2 | 4)', new BinaryNode('|', new ConstantNode(2), new ConstantNode(4))],
  104. ['(2 ^ 4)', new BinaryNode('^', new ConstantNode(2), new ConstantNode(4))],
  105. ['(1 < 2)', new BinaryNode('<', new ConstantNode(1), new ConstantNode(2))],
  106. ['(1 <= 2)', new BinaryNode('<=', new ConstantNode(1), new ConstantNode(2))],
  107. ['(1 <= 1)', new BinaryNode('<=', new ConstantNode(1), new ConstantNode(1))],
  108. ['(1 > 2)', new BinaryNode('>', new ConstantNode(1), new ConstantNode(2))],
  109. ['(1 >= 2)', new BinaryNode('>=', new ConstantNode(1), new ConstantNode(2))],
  110. ['(1 >= 1)', new BinaryNode('>=', new ConstantNode(1), new ConstantNode(1))],
  111. ['(true === true)', new BinaryNode('===', new ConstantNode(true), new ConstantNode(true))],
  112. ['(true !== true)', new BinaryNode('!==', new ConstantNode(true), new ConstantNode(true))],
  113. ['(2 == 1)', new BinaryNode('==', new ConstantNode(2), new ConstantNode(1))],
  114. ['(2 != 1)', new BinaryNode('!=', new ConstantNode(2), new ConstantNode(1))],
  115. ['(1 - 2)', new BinaryNode('-', new ConstantNode(1), new ConstantNode(2))],
  116. ['(1 + 2)', new BinaryNode('+', new ConstantNode(1), new ConstantNode(2))],
  117. ['(2 * 2)', new BinaryNode('*', new ConstantNode(2), new ConstantNode(2))],
  118. ['(2 / 2)', new BinaryNode('/', new ConstantNode(2), new ConstantNode(2))],
  119. ['(5 % 2)', new BinaryNode('%', new ConstantNode(5), new ConstantNode(2))],
  120. ['(5 ** 2)', new BinaryNode('**', new ConstantNode(5), new ConstantNode(2))],
  121. ['("a" ~ "b")', new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'))],
  122. ['("a" in ["a", "b"])', new BinaryNode('in', new ConstantNode('a'), $array)],
  123. ['("c" in ["a", "b"])', new BinaryNode('in', new ConstantNode('c'), $array)],
  124. ['("c" not in ["a", "b"])', new BinaryNode('not in', new ConstantNode('c'), $array)],
  125. ['("a" not in ["a", "b"])', new BinaryNode('not in', new ConstantNode('a'), $array)],
  126. ['(1 .. 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
  127. ['("abc" matches "/^[a-z]+/i$/")', new BinaryNode('matches', new ConstantNode('abc'), new ConstantNode('/^[a-z]+/i$/'))],
  128. ];
  129. }
  130. }