ExpressionParserTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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"} }}'),
  59. array('{{ {"a": "b", 2} }}'),
  60. );
  61. }
  62. public function getTestsForArray()
  63. {
  64. return array(
  65. // simple array
  66. array('{{ [1, 2] }}', new Twig_Node_Expression_Array(array(
  67. new Twig_Node_Expression_Constant(1, 1),
  68. new Twig_Node_Expression_Constant(2, 1),
  69. ), 1),
  70. ),
  71. // array with trailing ,
  72. array('{{ [1, 2, ] }}', new Twig_Node_Expression_Array(array(
  73. new Twig_Node_Expression_Constant(1, 1),
  74. new Twig_Node_Expression_Constant(2, 1),
  75. ), 1),
  76. ),
  77. // simple hash
  78. array('{{ {"a": "b", "b": "c"} }}', new Twig_Node_Expression_Array(array(
  79. 'a' => new Twig_Node_Expression_Constant('b', 1),
  80. 'b' => new Twig_Node_Expression_Constant('c', 1),
  81. ), 1),
  82. ),
  83. // hash with trailing ,
  84. array('{{ {"a": "b", "b": "c", } }}', new Twig_Node_Expression_Array(array(
  85. 'a' => new Twig_Node_Expression_Constant('b', 1),
  86. 'b' => new Twig_Node_Expression_Constant('c', 1),
  87. ), 1),
  88. ),
  89. // hash in an array
  90. array('{{ [1, {"a": "b", "b": "c"}] }}', new Twig_Node_Expression_Array(array(
  91. new Twig_Node_Expression_Constant(1, 1),
  92. new Twig_Node_Expression_Array(array(
  93. 'a' => new Twig_Node_Expression_Constant('b', 1),
  94. 'b' => new Twig_Node_Expression_Constant('c', 1),
  95. ), 1),
  96. ), 1),
  97. ),
  98. // array in a hash
  99. array('{{ {"a": [1, 2], "b": "c"} }}', new Twig_Node_Expression_Array(array(
  100. 'a' => new Twig_Node_Expression_Array(array(
  101. new Twig_Node_Expression_Constant(1, 1),
  102. new Twig_Node_Expression_Constant(2, 1),
  103. ), 1),
  104. 'b' => new Twig_Node_Expression_Constant('c', 1),
  105. ), 1),
  106. ),
  107. );
  108. }
  109. }