parser tests for interpolated strings
authorArnaud Le Blanc <arnaud.lb@gmail.com>
Sat, 12 Nov 2011 15:55:07 +0000 (16:55 +0100)
committerArnaud Le Blanc <arnaud.lb@gmail.com>
Sat, 12 Nov 2011 15:55:07 +0000 (16:55 +0100)
test/Twig/Tests/ExpressionParserTest.php

index d36c9fd..f87dc78 100644 (file)
@@ -121,4 +121,62 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
             ),
         );
     }
+
+    /**
+     * @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_Constant('foo ', 1),
+                    new Twig_Node_Expression_Binary_Concat(
+                        new Twig_Node_Expression_Name('bar', 1),
+                        new Twig_Node_Expression_Constant(' baz', 1),
+                        1
+                    ),
+                    1
+                ),
+            ),
+            array(
+                '{{ "foo #{"foo #{bar} baz"} baz" }}', 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_Binary_Concat(
+                                new Twig_Node_Expression_Name('bar', 1),
+                                new Twig_Node_Expression_Constant(' baz', 1),
+                                1
+                            ),
+                            1
+                        ),
+                        new Twig_Node_Expression_Constant(' baz', 1),
+                        1
+                    ),
+                    1
+                ),
+            ),
+        );
+    }
 }