123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
-
- /*
- * This file is part of Twig.
- *
- * (c) Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @expectedException Twig_Error_Syntax
- * @dataProvider getFailingTestsForAssignment
- */
- public function testCanOnlyAssignToNames($template)
- {
- $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
- $parser = new Twig_Parser($env);
-
- $parser->parse($env->tokenize($template, 'index'));
- }
-
- public function getFailingTestsForAssignment()
- {
- return array(
- array('{% set false = "foo" %}'),
- array('{% set true = "foo" %}'),
- array('{% set none = "foo" %}'),
- array('{% set 3 = "foo" %}'),
- array('{% set 1 + 2 = "foo" %}'),
- array('{% set "bar" = "foo" %}'),
- array('{% set %}{% endset %}')
- );
- }
-
- /**
- * @dataProvider getTestsForArray
- */
- public function testArrayExpression($template, $expected)
- {
- $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
- $stream = $env->tokenize($template, 'index');
- $parser = new Twig_Parser($env);
-
- $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
- }
-
- /**
- * @expectedException Twig_Error_Syntax
- * @dataProvider getFailingTestsForArray
- */
- public function testArraySyntaxError($template)
- {
- $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
- $parser = new Twig_Parser($env);
-
- $parser->parse($env->tokenize($template, 'index'));
- }
-
- public function getFailingTestsForArray()
- {
- return array(
- array('{{ [1, "a": "b"] }}'),
- array('{{ {"a": "b", 2} }}'),
- );
- }
-
- public function getTestsForArray()
- {
- return array(
- // simple array
- array('{{ [1, 2] }}', new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant(0, 1),
- new Twig_Node_Expression_Constant(1, 1),
-
- new Twig_Node_Expression_Constant(1, 1),
- new Twig_Node_Expression_Constant(2, 1),
- ), 1),
- ),
-
- // array with trailing ,
- array('{{ [1, 2, ] }}', new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant(0, 1),
- new Twig_Node_Expression_Constant(1, 1),
-
- new Twig_Node_Expression_Constant(1, 1),
- new Twig_Node_Expression_Constant(2, 1),
- ), 1),
- ),
-
- // simple hash
- array('{{ {"a": "b", "b": "c"} }}', new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant('a', 1),
- new Twig_Node_Expression_Constant('b', 1),
-
- new Twig_Node_Expression_Constant('b', 1),
- new Twig_Node_Expression_Constant('c', 1),
- ), 1),
- ),
-
- // hash with trailing ,
- array('{{ {"a": "b", "b": "c", } }}', new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant('a', 1),
- new Twig_Node_Expression_Constant('b', 1),
-
- new Twig_Node_Expression_Constant('b', 1),
- new Twig_Node_Expression_Constant('c', 1),
- ), 1),
- ),
-
- // hash in an array
- array('{{ [1, {"a": "b", "b": "c"}] }}', new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant(0, 1),
- new Twig_Node_Expression_Constant(1, 1),
-
- new Twig_Node_Expression_Constant(1, 1),
- new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant('a', 1),
- new Twig_Node_Expression_Constant('b', 1),
-
- new Twig_Node_Expression_Constant('b', 1),
- new Twig_Node_Expression_Constant('c', 1),
- ), 1),
- ), 1),
- ),
-
- // array in a hash
- array('{{ {"a": [1, 2], "b": "c"} }}', new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant('a', 1),
- new Twig_Node_Expression_Array(array(
- new Twig_Node_Expression_Constant(0, 1),
- new Twig_Node_Expression_Constant(1, 1),
-
- new Twig_Node_Expression_Constant(1, 1),
- new Twig_Node_Expression_Constant(2, 1),
- ), 1),
- new Twig_Node_Expression_Constant('b', 1),
- new Twig_Node_Expression_Constant('c', 1),
- ), 1),
- ),
- );
- }
-
- /**
- * @expectedException Twig_Error_Syntax
- */
- public function testStringExpressionDoesNotConcatenateTwoConsecutiveStrings()
- {
- $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
- $stream = $env->tokenize('{{ "a" "b" }}', 'index');
- $parser = new Twig_Parser($env);
-
- $parser->parse($stream);
- }
-
- /**
- * @dataProvider getTestsForString
- */
- public function testStringExpression($template, $expected)
- {
- $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
- $stream = $env->tokenize($template, 'index');
- $parser = new Twig_Parser($env);
-
- $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
- }
-
- public function getTestsForString()
- {
- return array(
- array(
- '{{ "foo" }}', new Twig_Node_Expression_Constant('foo', 1),
- ),
- array(
- '{{ "foo #{bar}" }}', new Twig_Node_Expression_Binary_Concat(
- new Twig_Node_Expression_Constant('foo ', 1),
- new Twig_Node_Expression_Name('bar', 1),
- 1
- ),
- ),
- array(
- '{{ "foo #{bar} baz" }}', new Twig_Node_Expression_Binary_Concat(
- new Twig_Node_Expression_Binary_Concat(
- new Twig_Node_Expression_Constant('foo ', 1),
- new Twig_Node_Expression_Name('bar', 1),
- 1
- ),
- new Twig_Node_Expression_Constant(' baz', 1),
- 1
- )
- ),
-
- array(
- '{{ "foo #{"foo #{bar} baz"} baz" }}', new Twig_Node_Expression_Binary_Concat(
- new Twig_Node_Expression_Binary_Concat(
- new Twig_Node_Expression_Constant('foo ', 1),
- new Twig_Node_Expression_Binary_Concat(
- new Twig_Node_Expression_Binary_Concat(
- new Twig_Node_Expression_Constant('foo ', 1),
- new Twig_Node_Expression_Name('bar', 1),
- 1
- ),
- new Twig_Node_Expression_Constant(' baz', 1),
- 1
- ),
- 1
- ),
- new Twig_Node_Expression_Constant(' baz', 1),
- 1
- ),
- ),
- );
- }
- }
|