[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/tests/cases/compiler/expression/ -> WactExpressionValueParserTest.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/expression/WactExpressionValueParser.class.php';
  11  
  12  class WactExpressionValueParserTest extends UnitTestCase
  13  {
  14    protected $parser;
  15    protected $root;
  16    protected $context;
  17    protected $code;
  18  
  19    function setUp()
  20    {
  21      $this->code = new WactCodeWriter();
  22      // preparing tree nodes with runtime

  23      $this->root = new WactCompileTreeRootNode();
  24  
  25      $this->context = new WactCompileTreeNode();
  26      $this->context->setServerId('child');
  27      $this->context->parent = $this->root;
  28  
  29      $this->parser = new WactExpressionValueParser($this->context);
  30    }
  31  
  32    function tearDown()
  33    {
  34      unset($this->parser);
  35    }
  36  
  37    function _parseAndReturnGeneratedCode($expression)
  38    {
  39      $expr = $this->parser->parse($expression);
  40      $expr->generatePreStatement($this->code);
  41      $expr->generateExpression($this->code);
  42      $expr->generatePostStatement($this->code);
  43      return $this->code->getCode();
  44    }
  45  
  46    function testNull()
  47    {
  48      $code = $this->_parseAndReturnGeneratedCode('null');
  49      $this->assertEqual($code, '<?php NULL');
  50    }
  51  
  52    function testTrue()
  53    {
  54      $code = $this->_parseAndReturnGeneratedCode('true');
  55      $this->assertEqual($code, '<?php true');
  56    }
  57  
  58    function testFalse()
  59    {
  60      $code = $this->_parseAndReturnGeneratedCode('false');
  61      $this->assertEqual($code, '<?php false');
  62    }
  63  
  64    function testInteger()
  65    {
  66      $code = $this->_parseAndReturnGeneratedCode('1');
  67      $this->assertEqual($code, '<?php 1');
  68    }
  69  
  70    function testZero()
  71    {
  72      $code = $this->_parseAndReturnGeneratedCode('0');
  73      $this->assertEqual($code, '<?php 0');
  74    }
  75  
  76    function testFloat()
  77    {
  78      $code = $this->_parseAndReturnGeneratedCode('1.2');
  79      $this->assertEqual($code, '<?php 1.2');
  80    }
  81  
  82    function testStringDoubleQuotes()
  83    {
  84      $expr = $this->parser->parse('"hello"');
  85      $this->assertIdentical($expr->getValue(), "hello");
  86    }
  87  
  88    function testZeroDoubleQuotes()
  89    {
  90      $expr = $this->parser->parse('"0"');
  91      $this->assertIdentical($expr->getValue(), "0");
  92    }
  93  
  94    function testStringSingleQuotes()
  95    {
  96      $expr = $this->parser->parse("'hello'");
  97      $this->assertIdentical($expr->getValue(), "hello");
  98    }
  99  
 100    function testSeveralDots()
 101    {
 102      $expr = $this->parser->parse('"..."');
 103      $this->assertIdentical($expr->getValue(), '...');
 104    }
 105  
 106    function testAddition()
 107    {
 108      $code = $this->_parseAndReturnGeneratedCode('1+2');
 109      $this->assertEqual($code, "<?php 1+2");
 110    }
 111  
 112    function testSubtraction()
 113    {
 114      $code = $this->_parseAndReturnGeneratedCode('1-2');
 115      $this->assertEqual($code, "<?php 1-2");
 116    }
 117  
 118    function testMultiplication()
 119    {
 120      $code = $this->_parseAndReturnGeneratedCode('2*3');
 121      $this->assertEqual($code, "<?php 2*3");
 122    }
 123  
 124    function testDivision()
 125    {
 126      $code = $this->_parseAndReturnGeneratedCode('8/2');
 127      $this->assertEqual($code, "<?php 8/2");
 128    }
 129  
 130    function testModulo()
 131    {
 132      $code = $this->_parseAndReturnGeneratedCode('5%2');
 133      $this->assertEqual($code, "<?php 5%2");
 134    }
 135  
 136    function testMinus()
 137    {
 138      $code = $this->_parseAndReturnGeneratedCode('-2');
 139      $this->assertEqual($code, "<?php -2");
 140    }
 141  
 142    function testConcatination()
 143    {
 144      $code = $this->_parseAndReturnGeneratedCode('"head" & "tail"');
 145      $this->assertEqual($code, "<?php 'head'&'tail'");
 146    }
 147  
 148    function testLogicalAnd()
 149    {
 150      $code = $this->_parseAndReturnGeneratedCode('true && false');
 151      $this->assertEqual($code, "<?php true&&false");
 152    }
 153  
 154    function testLogicalOr()
 155    {
 156      $code = $this->_parseAndReturnGeneratedCode('true || false');
 157      $this->assertEqual($code, "<?php true||false");
 158    }
 159  
 160    function testLogicalNot()
 161    {
 162      $code = $this->_parseAndReturnGeneratedCode('!false');
 163      $this->assertEqual($code, "<?php !false");
 164    }
 165  
 166    function testLogicalEqual()
 167    {
 168      $code = $this->_parseAndReturnGeneratedCode('1 == 1');
 169      $this->assertEqual($code, "<?php 1==1");
 170    }
 171  
 172    function testLogicalNotEqual()
 173    {
 174      $code = $this->_parseAndReturnGeneratedCode('1!=2');
 175      $this->assertEqual($code, "<?php 1!=2");
 176    }
 177  
 178    function testLogicalLessThan()
 179    {
 180      $code = $this->_parseAndReturnGeneratedCode('1 < 2');
 181      $this->assertEqual($code, "<?php 1<2");
 182    }
 183  
 184    function testLogicalLessThanOrEqual()
 185    {
 186      $code = $this->_parseAndReturnGeneratedCode('2 <= 2');
 187      $this->assertEqual($code, "<?php 2<=2");
 188    }
 189  
 190    function testLogicalGreaterThan()
 191    {
 192      $code = $this->_parseAndReturnGeneratedCode('3 > 2');
 193      $this->assertEqual($code, "<?php 3>2");
 194    }
 195  
 196    function testLogicalGreaterThanOrEqual()
 197    {
 198      $code = $this->_parseAndReturnGeneratedCode('3 >= 2');
 199      $this->assertEqual($code, "<?php 3>=2");
 200    }
 201  
 202    function testMyDearAuntSally()
 203    {
 204      $code = $this->_parseAndReturnGeneratedCode('1+2*3');
 205      $this->assertEqual($code, "<?php 1+2*3");
 206    }
 207  
 208    function testMyDearAuntSally2()
 209    {
 210      $code = $this->_parseAndReturnGeneratedCode('2*3+1');
 211      $this->assertEqual($code, "<?php 2*3+1");
 212    }
 213  
 214    function testMyDearAuntSally3()
 215    {
 216      $code = $this->_parseAndReturnGeneratedCode('2*3+4*5');
 217      $this->assertEqual($code, "<?php 2*3+4*5");
 218    }
 219  
 220    function testMyDearAuntSally4()
 221    {
 222      $code = $this->_parseAndReturnGeneratedCode('8-4-2');
 223      $this->assertEqual($code, "<?php 8-4-2");
 224    }
 225  
 226    function testMyDearAuntSally5()
 227    {
 228      $code = $this->_parseAndReturnGeneratedCode('24*6*2');
 229      $this->assertEqual($code, "<?php 24*6*2");
 230    }
 231  
 232    function testMyDearAuntSally6()
 233    {
 234      $code = $this->_parseAndReturnGeneratedCode('24/6/2');
 235      $this->assertEqual($code, "<?php 24/6/2");
 236    }
 237  
 238    function testParenthesis()
 239    {
 240      $code = $this->_parseAndReturnGeneratedCode('8-(4-2)');
 241      $this->assertEqual($code, "<?php 8-(4-2)");
 242    }
 243  
 244    function testConstantPropertyFromContext()
 245    {
 246      $property = new WactConstantProperty('hello');
 247  
 248      $this->context->registerProperty('Test', $property);
 249  
 250      $expr = $this->parser->parse('Test');
 251      $this->assertIdentical($expr->getValue(),'hello');
 252    }
 253  
 254    function testConstantPropertyWithNewSyntax()
 255    {
 256      $property = new WactConstantProperty('hello');
 257  
 258      $this->root->registerProperty('Test', $property);
 259  
 260      $expr = $this->parser->parse(':Test');
 261      $this->assertIdentical($expr->getValue(),'hello');
 262    }
 263  
 264    function testConstantPropertyFromRootContext()
 265    {
 266      $property = new WactConstantProperty('hello');
 267  
 268      $this->root->registerProperty('Test', $property);
 269  
 270      $expr = $this->parser->parse('#Test');
 271      $this->assertIdentical($expr->getValue(),'hello');
 272    }
 273  
 274    function testConstantPropertyFromParentContext()
 275    {
 276      $property = new WactConstantProperty('hello');
 277  
 278      $this->root->registerProperty('Test', $property);
 279  
 280      $expr = $this->parser->parse('^Test');
 281      $this->assertIdentical($expr->getValue(),'hello');
 282    }
 283  
 284    function testConstantPropertyFromParentContextWithNewSyntax()
 285    {
 286      $property = new WactConstantProperty('hello');
 287  
 288      $this->root->registerProperty('Test', $property);
 289  
 290      $expr = $this->parser->parse('^:Test');
 291      $this->assertIdentical($expr->getValue(),'hello');
 292    }
 293  
 294    function testLocalVariableModifier()
 295    {
 296      $expr = $this->parser->parse('$Test');
 297  
 298      $code_writer = new WactCodeWriter();
 299  
 300      $root = new WactCompileTreeRootNode();
 301      $context = new WactCompileTreeNode();
 302      $context->parent = $root;
 303  
 304      $expr->generatePreStatement($code_writer);
 305      $expr->generateExpression($code_writer);
 306      $expr->generatePostStatement($code_writer);
 307  
 308      $this->assertEqual($code_writer->getCode(), '<?php $Test');
 309    }
 310  }
 311  
 312  ?>