[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/tests/cases/compiler/expression/ -> WactExpressionTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  require_once 'limb/wact/src/compiler/templatecompiler.inc.php';
  11  
  12  class WactExpressionTestingFilter extends WactCompilerFilter
  13  {
  14    public static $calls = array();
  15  
  16    function generatePreStatement($code_writer)
  17    {
  18      if(!is_object($code_writer))
  19        throw new Exception('Code writer was not passed!');
  20  
  21      self :: $calls['generatePreStatement']++;
  22  
  23      parent :: generatePreStatement($code_writer);
  24    }
  25  
  26    function generateExpression($code_writer)
  27    {
  28      if(!is_object($code_writer))
  29        throw new Exception('Code writer was not passed!');
  30  
  31      self :: $calls['generateExpression']++;
  32  
  33      parent :: generateExpression($code_writer);
  34    }
  35  
  36    function generatePostStatement($code_writer)
  37    {
  38      if(!is_object($code_writer))
  39        throw new Exception('Code writer was not passed!');
  40  
  41      self :: $calls['generatePostStatement']++;
  42  
  43      parent :: generatePostStatement($code_writer);
  44    }
  45  
  46    function prepare()
  47    {
  48      self :: $calls['prepare']++;
  49  
  50      parent :: prepare();
  51    }
  52  
  53    function getValue()
  54    {
  55      return $this->base->getValue();
  56    }
  57  
  58    static function reset()
  59    {
  60      self :: $calls = array('generatePreStatement' => 0,
  61                             'generateExpression' => 0,
  62                             'generatePostStatement' => 0,
  63                              'prepare' => 0);
  64    }
  65  }
  66  
  67  class WactExpressionTest extends UnitTestCase
  68  {
  69    protected $filter_dictionary;
  70  
  71    function setUp()
  72    {
  73      WactExpressionTestingFilter :: reset();
  74      $this->filter_dictionary = new WactFilterDictionary();
  75      $filter_info = new WactFilterInfo('expression_testing_filter', 'WactExpressionTestingFilter', 0, 3);
  76      $this->filter_dictionary->registerFilterInfo($filter_info, __FILE__);
  77    }
  78  
  79    protected function _createExpression($expression_text, $default_filter = 'raw')
  80    {
  81      $location = new WactSourceLocation('my_testing_file', 10);
  82      $context_node = new WactCompileTreeNode($location);
  83      return new WactExpression($expression_text, $context_node, $this->filter_dictionary, $default_filter);
  84    }
  85  
  86    function _parseAndReturnGeneratedCode($expression_text)
  87    {
  88      $code = new WactCodeWriter();
  89      $expression = $this->_createExpression($expression_text);
  90      $expression->generatePreStatement($code);
  91      $expression->generateExpression($code);
  92      $expression->generatePostStatement($code);
  93      return $code->getCode();
  94    }
  95  
  96    function testCreateValueInteger()
  97    {
  98      $expression = $this->_createExpression('29');
  99      $this->assertTrue($expression->isConstant());
 100      $this->assertEqual($expression->getValue(), 29);
 101    }
 102  
 103    function testCreateValueFloat()
 104    {
 105      $expression = $this->_createExpression('1.5');
 106      $this->assertTrue($expression->isConstant());
 107      $this->assertEqual($expression->getValue(), 1.5);
 108    }
 109  
 110    function testCreateValueNegInteger()
 111    {
 112      $code = $this->_parseAndReturnGeneratedCode('-29');
 113      $this->assertEqual($code, '<?php -29');
 114    }
 115  
 116    function testCreateValueNegFloat()
 117    {
 118      $code = $this->_parseAndReturnGeneratedCode('-1.5');
 119      $this->assertEqual($code, '<?php -1.5');
 120    }
 121  
 122    function testCreateValueSingleQuoteString()
 123    {
 124      $expression = $this->_createExpression("'hello'");
 125      $this->assertTrue($expression->isConstant());
 126      $this->assertEqual($expression->getValue(), 'hello');
 127    }
 128  
 129    function testCreateValueDoubleQuoteString()
 130    {
 131      $expression = $this->_createExpression('"hello"');
 132      $this->assertTrue($expression->isConstant());
 133      $this->assertEqual($expression->getValue(), 'hello');
 134    }
 135  
 136    function testMissingFilter()
 137    {
 138      try
 139      {
 140        $expression = $this->_createExpression('Test|no_such_filter');
 141        $this->assertTrue(false);
 142      }
 143      catch(WactException $e)
 144      {
 145        $this->assertWantedPattern('/Unknown filter/', $e->getMessage());
 146        $this->assertEqual($e->getParam('filter'), 'no_such_filter');
 147      }
 148    }
 149  
 150    function testInvalidFilter()
 151    {
 152      try
 153      {
 154        $expression = $this->_createExpression('Test|99');
 155        $this->assertTrue(false);
 156      }
 157      catch(WactException $e)
 158      {
 159        $this->assertWantedPattern('/Filter name expected/', $e->getMessage());
 160      }
 161    }
 162  
 163    function testIsConstant()
 164    {
 165      $expression = $this->_createExpression('"Test"');
 166      $this->assertTrue($expression->isConstant());
 167    }
 168  
 169    function testGetValueForConstant()
 170    {
 171      $expression = $this->_createExpression('"Test"');
 172      $this->assertEqual($expression->getValue(), 'Test');
 173    }
 174  
 175    function testGetValueForDBE()
 176    {
 177      $context = new WactCompileTreeNode();
 178      $property = new WactConstantProperty('hello');
 179      $context->registerProperty('Test', $property);
 180  
 181      $expression = new WactExpression('Test', $context, $this->filter_dictionary);
 182      $this->assertEqual($expression->getValue(), 'hello');
 183    }
 184  
 185    function testGenerateMethods()
 186    {
 187      $code_writer = new WactCodeWriter();
 188  
 189      $expression = $this->_createExpression('"Test"|expression_testing_filter');
 190  
 191      $expression->generatePreStatement($code_writer);
 192      $expression->generateExpression($code_writer);
 193      $expression->generatePostStatement($code_writer);
 194      $expression->prepare();
 195  
 196      $this->assertEqual(WactExpressionTestingFilter :: $calls['generatePreStatement'], 1);
 197      $this->assertEqual(WactExpressionTestingFilter :: $calls['generateExpression'], 1);
 198      $this->assertEqual(WactExpressionTestingFilter :: $calls['generatePostStatement'], 1);
 199      $this->assertEqual(WactExpressionTestingFilter :: $calls['prepare'], 1);
 200    }
 201  
 202    function testApplyDefaultFilter()
 203    {
 204      $expression = $this->_createExpression('"Test"|expression_testing_filter', 'expression_testing_filter');
 205      $expression->prepare();
 206      $this->assertEqual(WactExpressionTestingFilter :: $calls['prepare'], 1);
 207    }
 208  
 209    function testFilterWithParams()
 210    {
 211      $expression = $this->_createExpression('"Test"|expression_testing_filter:"..."');
 212      $expression->prepare();
 213      $this->assertEqual(WactExpressionTestingFilter :: $calls['prepare'], 1);
 214    }
 215  
 216    function testDontApplyDefaultFilter()
 217    {
 218      $expression = $this->_createExpression('"Test"|raw', 'expression_testing_filter');
 219      $expression->prepare();
 220      $this->assertEqual(WactExpressionTestingFilter :: $calls['prepare'], 0);
 221    }
 222  
 223    function testDontApplyDefaultFilter2()
 224    {
 225      $expression = $this->_createExpression('"Test"|expression_testing_filter|raw', 'expression_testing_filter');
 226      $expression->prepare();
 227      $this->assertEqual(WactExpressionTestingFilter :: $calls['prepare'], 1);
 228    }
 229  
 230    function testDontApplyDefaultFilterIsTheSameFilterIsTheLastAmongExpressionFilters()
 231    {
 232      $expression = $this->_createExpression('"Test"|expression_testing_filter', 'expression_testing_filter');
 233      $expression->prepare();
 234      $this->assertEqual(WactExpressionTestingFilter :: $calls['prepare'], 1);
 235    }
 236  }
 237  ?>


Generated: Sun Oct 12 04:41:30 2008 Cross-referenced by PHPXref 0.7