ExpressionParserTest.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @expectedException Twig_Error_Syntax
  14. * @dataProvider getFailingTestsForAssignment
  15. */
  16. public function testCanOnlyAssignToNames($template)
  17. {
  18. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
  19. $parser = new Twig_Parser($env);
  20. $parser->parse($env->tokenize($template, 'index'));
  21. }
  22. public function getFailingTestsForAssignment()
  23. {
  24. return array(
  25. array('{% set false = "foo" %}'),
  26. array('{% set true = "foo" %}'),
  27. array('{% set none = "foo" %}'),
  28. array('{% set 3 = "foo" %}'),
  29. array('{% set 1 + 2 = "foo" %}'),
  30. array('{% set "bar" = "foo" %}'),
  31. array('{% set %}{% endset %}')
  32. );
  33. }
  34. /**
  35. * @dataProvider getTestsForArray
  36. */
  37. public function testArrayExpression($template, $expected)
  38. {
  39. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
  40. $stream = $env->tokenize($template, 'index');
  41. $parser = new Twig_Parser($env);
  42. $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
  43. }
  44. /**
  45. * @expectedException Twig_Error_Syntax
  46. * @dataProvider getFailingTestsForArray
  47. */
  48. public function testArraySyntaxError($template)
  49. {
  50. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
  51. $parser = new Twig_Parser($env);
  52. $parser->parse($env->tokenize($template, 'index'));
  53. }
  54. public function getFailingTestsForArray()
  55. {
  56. return array(
  57. array('{{ [1, "a": "b"] }}'),
  58. array('{{ {"a": "b", 2} }}'),
  59. );
  60. }
  61. public function getTestsForArray()
  62. {
  63. return array(
  64. // simple array
  65. array('{{ [1, 2] }}', new Twig_Node_Expression_Array(array(
  66. new Twig_Node_Expression_Constant(0, 1),
  67. new Twig_Node_Expression_Constant(1, 1),
  68. new Twig_Node_Expression_Constant(1, 1),
  69. new Twig_Node_Expression_Constant(2, 1),
  70. ), 1),
  71. ),
  72. // array with trailing ,
  73. array('{{ [1, 2, ] }}', new Twig_Node_Expression_Array(array(
  74. new Twig_Node_Expression_Constant(0, 1),
  75. new Twig_Node_Expression_Constant(1, 1),
  76. new Twig_Node_Expression_Constant(1, 1),
  77. new Twig_Node_Expression_Constant(2, 1),
  78. ), 1),
  79. ),
  80. // simple hash
  81. array('{{ {"a": "b", "b": "c"} }}', new Twig_Node_Expression_Array(array(
  82. new Twig_Node_Expression_Constant('a', 1),
  83. new Twig_Node_Expression_Constant('b', 1),
  84. new Twig_Node_Expression_Constant('b', 1),
  85. new Twig_Node_Expression_Constant('c', 1),
  86. ), 1),
  87. ),
  88. // hash with trailing ,
  89. array('{{ {"a": "b", "b": "c", } }}', new Twig_Node_Expression_Array(array(
  90. new Twig_Node_Expression_Constant('a', 1),
  91. new Twig_Node_Expression_Constant('b', 1),
  92. new Twig_Node_Expression_Constant('b', 1),
  93. new Twig_Node_Expression_Constant('c', 1),
  94. ), 1),
  95. ),
  96. // hash in an array
  97. array('{{ [1, {"a": "b", "b": "c"}] }}', new Twig_Node_Expression_Array(array(
  98. new Twig_Node_Expression_Constant(0, 1),
  99. new Twig_Node_Expression_Constant(1, 1),
  100. new Twig_Node_Expression_Constant(1, 1),
  101. new Twig_Node_Expression_Array(array(
  102. new Twig_Node_Expression_Constant('a', 1),
  103. new Twig_Node_Expression_Constant('b', 1),
  104. new Twig_Node_Expression_Constant('b', 1),
  105. new Twig_Node_Expression_Constant('c', 1),
  106. ), 1),
  107. ), 1),
  108. ),
  109. // array in a hash
  110. array('{{ {"a": [1, 2], "b": "c"} }}', new Twig_Node_Expression_Array(array(
  111. new Twig_Node_Expression_Constant('a', 1),
  112. new Twig_Node_Expression_Array(array(
  113. new Twig_Node_Expression_Constant(0, 1),
  114. new Twig_Node_Expression_Constant(1, 1),
  115. new Twig_Node_Expression_Constant(1, 1),
  116. new Twig_Node_Expression_Constant(2, 1),
  117. ), 1),
  118. new Twig_Node_Expression_Constant('b', 1),
  119. new Twig_Node_Expression_Constant('c', 1),
  120. ), 1),
  121. ),
  122. );
  123. }
  124. /**
  125. * @expectedException Twig_Error_Syntax
  126. */
  127. public function testStringExpressionDoesNotConcatenateTwoConsecutiveStrings()
  128. {
  129. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
  130. $stream = $env->tokenize('{{ "a" "b" }}', 'index');
  131. $parser = new Twig_Parser($env);
  132. $parser->parse($stream);
  133. }
  134. /**
  135. * @dataProvider getTestsForString
  136. */
  137. public function testStringExpression($template, $expected)
  138. {
  139. $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
  140. $stream = $env->tokenize($template, 'index');
  141. $parser = new Twig_Parser($env);
  142. $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
  143. }
  144. public function getTestsForString()
  145. {
  146. return array(
  147. array(
  148. '{{ "foo" }}', new Twig_Node_Expression_Constant('foo', 1),
  149. ),
  150. array(
  151. '{{ "foo #{bar}" }}', new Twig_Node_Expression_Binary_Concat(
  152. new Twig_Node_Expression_Constant('foo ', 1),
  153. new Twig_Node_Expression_Name('bar', 1),
  154. 1
  155. ),
  156. ),
  157. array(
  158. '{{ "foo #{bar} baz" }}', new Twig_Node_Expression_Binary_Concat(
  159. new Twig_Node_Expression_Binary_Concat(
  160. new Twig_Node_Expression_Constant('foo ', 1),
  161. new Twig_Node_Expression_Name('bar', 1),
  162. 1
  163. ),
  164. new Twig_Node_Expression_Constant(' baz', 1),
  165. 1
  166. )
  167. ),
  168. array(
  169. '{{ "foo #{"foo #{bar} baz"} baz" }}', new Twig_Node_Expression_Binary_Concat(
  170. new Twig_Node_Expression_Binary_Concat(
  171. new Twig_Node_Expression_Constant('foo ', 1),
  172. new Twig_Node_Expression_Binary_Concat(
  173. new Twig_Node_Expression_Binary_Concat(
  174. new Twig_Node_Expression_Constant('foo ', 1),
  175. new Twig_Node_Expression_Name('bar', 1),
  176. 1
  177. ),
  178. new Twig_Node_Expression_Constant(' baz', 1),
  179. 1
  180. ),
  181. 1
  182. ),
  183. new Twig_Node_Expression_Constant(' baz', 1),
  184. 1
  185. ),
  186. ),
  187. );
  188. }
  189. }