migrated most tests to proper Node classes

This commit is contained in:
Fabien Potencier
2011-11-01 08:57:23 +01:00
parent c749c6b822
commit c2371dbbd6
6 changed files with 174 additions and 90 deletions
+6 -90
View File
@@ -108,14 +108,14 @@ class Twig_Extension_Core extends Twig_Extension
public function getTests()
{
return array(
'even' => new Twig_Test_Function('twig_test_even'),
'odd' => new Twig_Test_Function('twig_test_odd'),
'even' => new Twig_Test_Node('Twig_Node_Expression_Test_Even'),
'odd' => new Twig_Test_Node('Twig_Node_Expression_Test_Odd'),
'defined' => new Twig_Test_Node('Twig_Node_Expression_Test_Defined'),
'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'),
'none' => new Twig_Test_Function('twig_test_none'),
'null' => new Twig_Test_Function('twig_test_none'),
'divisibleby' => new Twig_Test_Function('twig_test_divisibleby'),
'constant' => new Twig_Test_Function('twig_test_constant'),
'none' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'),
'null' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'),
'divisibleby' => new Twig_Test_Node('Twig_Node_Expression_Test_Divisibleby'),
'constant' => new Twig_Test_Node('Twig_Node_Expression_Test_Constant'),
'empty' => new Twig_Test_Function('twig_test_empty'),
);
}
@@ -753,90 +753,6 @@ function twig_test_sameas($value, $test)
return $value === $test;
}
/**
* Checks that a variable is null.
*
* <pre>
* {{ var is none }}
* </pre>
*
* @param mixed $value a PHP variable.
*
* @return Boolean true if the value is null, false otherwise
*/
function twig_test_none($value)
{
return null === $value;
}
/**
* Checks if a variable is divisible by a number.
*
* <pre>
* {% if loop.index is divisibleby(3) %}
* </pre>
*
* @param integer $value A PHP value
* @param integer $num A number
*
* @return Boolean true if the value is divisible by the number, false otherwise
*/
function twig_test_divisibleby($value, $num)
{
return 0 == $value % $num;
}
/**
* Checks if a number is even.
*
* <pre>
* {{ var is even }}
* </pre>
*
* @param integer $value An integer
*
* @return Boolean true if the value is even, false otherwise
*/
function twig_test_even($value)
{
return $value % 2 == 0;
}
/**
* Checks if a number is odd.
*
* <pre>
* {{ var is odd }}
* </pre>
*
* @param integer $value An integer
*
* @return Boolean true if the value is odd, false otherwise
*/
function twig_test_odd($value)
{
return $value % 2 == 1;
}
/**
* Checks if a variable is the exact same value as a constant.
*
* <pre>
* {% if post.status is constant('Post::PUBLISHED') %}
* the status attribute is exactly the same as Post::PUBLISHED
* {% endif %}
* </pre>
*
* @param mixed $value A PHP value
* @param mixed $constant The constant to test against
*
* @return Boolean true if the value is the same as the constant, false otherwise
*/
function twig_test_constant($value, $constant)
{
return constant($constant) === $value;
}
/**
* Checks if a variable is empty.
*
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Checks if a variable is the exact same value as a constant.
*
* <pre>
* {% if post.status is constant('Post::PUBLISHED') %}
* the status attribute is exactly the same as Post::PUBLISHED
* {% endif %}
* </pre>
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Expression_Test_Constant extends Twig_Node_Expression_Test
{
public function compile(Twig_Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' === constant(')
->subcompile($this->getNode('arguments')->getNode(0))
->raw('))')
;
}
}
@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Checks if a variable is divisible by a number.
*
* <pre>
* {% if loop.index is divisibleby(3) %}
* </pre>
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Expression_Test_Divisibleby extends Twig_Node_Expression_Test
{
public function compile(Twig_Compiler $compiler)
{
$compiler
->raw('(0 == ')
->subcompile($this->getNode('node'))
->raw(' % ')
->subcompile($this->getNode('arguments')->getNode(0))
->raw(')')
;
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Checks if a number is even.
*
* <pre>
* {{ var is even }}
* </pre>
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Expression_Test_Even extends Twig_Node_Expression_Test
{
public function compile(Twig_Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' % 2 == 0')
->raw(')')
;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Checks that a variable is null.
*
* <pre>
* {{ var is none }}
* </pre>
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Expression_Test_Null extends Twig_Node_Expression_Test
{
public function compile(Twig_Compiler $compiler)
{
$compiler
->raw('(null === ')
->subcompile($this->getNode('node'))
->raw(')')
;
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Checks if a number is odd.
*
* <pre>
* {{ var is odd }}
* </pre>
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Expression_Test_Odd extends Twig_Node_Expression_Test
{
public function compile(Twig_Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' % 2 == 1')
->raw(')')
;
}
}