fixed deep nested defined tests
authorFabien Potencier <fabien.potencier@gmail.com>
Wed, 26 Oct 2011 07:33:02 +0000 (09:33 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Wed, 26 Oct 2011 09:20:38 +0000 (11:20 +0200)
lib/Twig/Node/Expression/Test.php
test/Twig/Tests/Fixtures/filters/default.test

index 1c000ac..6af4bd0 100644 (file)
@@ -16,14 +16,25 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
 
         // defined is a special case
         if ('defined' === $name) {
-            if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) {
+            if ($node instanceof Twig_Node_Expression_Name) {
                 $node->setAttribute('is_defined_test', true);
+            } elseif ($node instanceof Twig_Node_Expression_GetAttr) {
+                $this->changeIsDefined($node);
             } else {
                 throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
             }
         }
     }
 
+    protected function changeIsDefined(Twig_Node_Expression_GetAttr $node)
+    {
+        $node->setAttribute('is_defined_test', true);
+
+        if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
+            $this->changeIsDefined($node->getNode('node'));
+        }
+    }
+
     public function compile(Twig_Compiler $compiler)
     {
         $testMap = $compiler->getEnvironment()->getTests();
index f6bfb8f..d518332 100644 (file)
@@ -23,7 +23,7 @@ Precedence:
 {{ 'o' ~ nullVar|default('k') }}
 {{ 'o' ~ nested.nullVar|default('k') }}
 Deep nested:
-{{ nested.definedVar.undefinedVar.foo.bar|default('default') is sameas('default') ? 'ok' : 'ko' }}
+{{ nested.undefinedVar.foo.bar|default('default') is sameas('default') ? 'ok' : 'ko' }}
 --DATA--
 return array(
     'definedVar' => 'defined',
@@ -63,3 +63,42 @@ ok
 ok
 Deep nested:
 ok
+--DATA--
+return array(
+    'definedVar' => 'defined',
+    'zeroVar'    => 0,
+    'emptyVar'   => '',
+    'nullVar'    => null,
+    'nested'     => array(
+        'definedVar' => 'defined',
+        'zeroVar'    => 0,
+        'emptyVar'   => '',
+        'nullVar'    => null,
+    ),
+)
+--CONFIG--
+return array('strict_variables' => true)
+--EXPECT--
+Variable:
+ok
+ok
+ok
+ok
+ok
+Array access:
+ok
+ok
+ok
+ok
+ok
+ok
+Plain values:
+ok
+ok
+ok
+ok
+Precedence:
+ok
+ok
+Deep nested:
+ok