[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/tests/cases/filters/ -> math_filter.test.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/tests/cases/WactTemplateTestCase.class.php');
  11  
  12  class WactTemplateMathFilterTestCase extends WactTemplateTestCase {
  13  
  14    function testAdd()
  15    {
  16      $template = '{$val|math:"+3"}';
  17  
  18      $this->registerTestingTemplate('/template/filter/math_add.html', $template);
  19      $page = $this->initTemplate('/template/filter/math_add.html');
  20      $page->set('val',6);
  21  
  22      $output = $page->capture();
  23      $this->assertEqual($output, '9');
  24    }
  25  
  26    function testMult()
  27    {
  28      $template = '{$val|math:"*3"}';
  29  
  30      $this->registerTestingTemplate('/template/filter/math_mult.html', $template);
  31      $page = $this->initTemplate('/template/filter/math_mult.html');
  32      $page->set('val',6);
  33  
  34      $output = $page->capture();
  35      $this->assertEqual($output, '18');
  36    }
  37  
  38    function testDivVar()
  39    {
  40      $template = '<core:set exp="/{$val2}" />{$val|math:exp}';
  41  
  42      $this->registerTestingTemplate('/template/filter/math_divvar.html', $template);
  43      $page = $this->initTemplate('/template/filter/math_divvar.html');
  44      $page->set('val',14);
  45      $page->set('val2',8);
  46  
  47      $output = $page->capture();
  48      $this->assertEqual($output, '1.75');
  49    }
  50  
  51    function testDivVarLtOne()
  52    {
  53      //adds a number filter on output to eliminate leading 0 difference

  54      //between php4 and php5

  55      $template = '<core:set exp="/{$val2}" />{$val|math:exp|number:2}';
  56  
  57      $this->registerTestingTemplate('/template/filter/math_divvarlt1.html', $template);
  58      $page = $this->initTemplate('/template/filter/math_divvarlt1.html');
  59      $page->set('val',6);
  60      $page->set('val2',8);
  61  
  62      $output = $page->capture();
  63      $this->assertEqual($output, '0.75');
  64    }
  65  
  66    function testPctVar()
  67    {
  68      $template = '<core:set exp="/{$val2}*100" />{$val|math:exp|number:2}%';
  69  
  70      $this->registerTestingTemplate('/template/filter/math_pctvar.html', $template);
  71      $page = $this->initTemplate('/template/filter/math_pctvar.html');
  72      $page->set('val',63);
  73      $page->set('val2',85);
  74  
  75      $output = $page->capture();
  76      $this->assertEqual($output, '74.12%');
  77    }
  78  
  79    function testPctVar2Parm()
  80    {
  81      $template = '{$val}/{$val2}={$val|math:"/",val2,"*100"|number:3}%';
  82  
  83      $this->registerTestingTemplate('/template/filter/math_pctvar2parm.html', $template);
  84      $page = $this->initTemplate('/template/filter/math_pctvar2parm.html');
  85      $page->set('val',63);
  86      $page->set('val2',85);
  87  
  88      $output = $page->capture();
  89      $this->assertEqual($output, '63/85=74.118%');
  90    }
  91  
  92    function testBadExp()
  93    {
  94      $template = '<core:set exp="/bad*100" />{$val|math:exp|number:2}';
  95  
  96      $this->registerTestingTemplate('/template/filter/math_err.html', $template);
  97      $page = $this->initTemplate('/template/filter/math_err.html');
  98      $page->set('val',63);
  99  
 100      try
 101      {
 102        $output = $page->capture();
 103        $this->assertTrue(false);
 104      }
 105      catch(WactException $e)
 106      {
 107        $this->assertWantedPattern('/Interal Error/i', $e->getMessage());
 108        $this->assertWantedPattern('/Undefined operator(?U).*bad/i', $e->getMessage());
 109      }
 110    }
 111  
 112    function testChainedFilter()
 113    {
 114      $template = '<list:list from="data"><list:item>{$v1|stats:"v1"} {$v2|stats:"v2"}<br /></list:item></list:list>'
 115        .'<core:set div_v2=\'/{$v2|stats:"v2","sum"}*100\' />'
 116        .'v1/v2*100={$v1|stats:"v1","sum"|math:div_v2|number:2}';
 117  
 118      $this->registerTestingTemplate('/template/filter/math_chain.html', $template);
 119      $page = $this->initTemplate('/template/filter/math_chain.html');
 120      $page->set('data',new WactArrayIterator(array(
 121        array('v1'=>100, 'v2'=>150)
 122        ,array('v1'=>200, 'v2'=>175)
 123        ,array('v1'=>170, 'v2'=>150)
 124        )));
 125  
 126      $output = $page->capture();
 127      $this->assertWantedPattern('/98[.]95$/', $output);
 128    }
 129  }
 130  ?>


Generated: Mon Dec 1 03:56:46 2008 Cross-referenced by PHPXref 0.7