[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/js/tests/cases/ -> lmbJsPreprocessorTest.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  lmb_require('limb/js/src/lmbJsPreprocessor.class.php');
  10  lmb_require('limb/fs/src/lmbFs.class.php');
  11  
  12  class CustomDirectiveProcessor
  13  {
  14    static function processDefine($name, $value)
  15    {
  16      return "var $name = $value;";
  17    }
  18  }
  19  
  20  class lmbJsPreprocessorTest extends UnitTestCase
  21  {
  22    var $created_files = array();
  23  
  24    function setUp()
  25    {
  26      $this->_cleanup();
  27    }
  28  
  29    function tearDown()
  30    {
  31      $this->_cleanup();
  32    }
  33  
  34    function testIncludeOneFile()
  35    {
  36      $this->_createFile($file = 'foo.js',
  37  <<<EOD
  38  //#include "bar.js"
  39  foo
  40  EOD
  41  );
  42  
  43      $this->_createFile('bar.js',
  44  <<<EOD
  45  bar
  46  EOD
  47  );
  48  
  49      $builder = new lmbJsPreprocessor();
  50      $this->assertEqual($builder->processFile(LIMB_VAR_DIR . $file),
  51  <<<EOD
  52  bar
  53  foo
  54  EOD
  55  );
  56    }
  57  
  58    function testIncludeSeveralFiles()
  59    {
  60      $this->_createFile($file1 = 'foo.js',
  61  <<<EOD
  62  //#include "bar.js"
  63  //#include "wow.js"
  64  foo
  65  EOD
  66  );
  67  
  68      $this->_createFile($file2 = 'bar.js',
  69  <<<EOD
  70  bar
  71  EOD
  72  );
  73  
  74      $this->_createFile($file3 = 'wow.js',
  75  <<<EOD
  76  //#include "bar.js"
  77  wow
  78  EOD
  79  );
  80  
  81  
  82      $builder = new lmbJsPreprocessor();
  83      $this->assertEqual($builder->processFiles(array(LIMB_VAR_DIR . $file1, LIMB_VAR_DIR . $file2)),
  84  <<<EOD
  85  bar
  86  wow
  87  foo
  88  
  89  EOD
  90  );
  91    }
  92  
  93    function testCyclicInclude()
  94    {
  95      $this->_createFile($file = 'foo.js',
  96  <<<EOD
  97  //#include "bar.js"
  98  foo
  99  EOD
 100  );
 101  
 102      $this->_createFile('bar.js',
 103  <<<EOD
 104  //#include "foo.js"
 105  bar
 106  EOD
 107  );
 108  
 109      $builder = new lmbJsPreprocessor();
 110      $this->assertEqual($builder->processFile(LIMB_VAR_DIR . $file),
 111  <<<EOD
 112  bar
 113  foo
 114  EOD
 115  );
 116    }
 117  
 118    function testDeeperInclude()
 119    {
 120      $this->_createFile($file = 'foo.js',
 121  <<<EOD
 122  //#include "bar.js"
 123  //#include "zoo.js"
 124  foo
 125  EOD
 126  );
 127  
 128      $this->_createFile('bar.js',
 129  <<<EOD
 130  //#include "baz.js"
 131  bar
 132  EOD
 133  );
 134  
 135      $this->_createFile('baz.js',
 136  <<<EOD
 137  baz
 138  EOD
 139  );
 140  
 141      $this->_createFile('zoo.js',
 142  <<<EOD
 143  zoo
 144  EOD
 145  );
 146  
 147      $builder = new lmbJsPreprocessor();
 148      $this->assertEqual($builder->processFile(LIMB_VAR_DIR . $file),
 149  <<<EOD
 150  baz
 151  bar
 152  zoo
 153  foo
 154  EOD
 155  );
 156    }
 157  
 158    function testIncludeWithWildcards()
 159    {
 160      $this->_createFile($file = 'foo.js',
 161  <<<EOD
 162  //#include "*.js"
 163  foo
 164  EOD
 165  );
 166  
 167      $this->_createFile('bar.js',
 168  <<<EOD
 169  bar
 170  EOD
 171  );
 172  
 173      $this->_createFile('baz.js',
 174  <<<EOD
 175  baz
 176  EOD
 177  );
 178  
 179      $this->_createFile('zoo.js',
 180  <<<EOD
 181  zoo
 182  EOD
 183  );
 184  
 185      $builder = new lmbJsPreprocessor();
 186      $this->assertEqual($builder->processFile(LIMB_VAR_DIR . $file),
 187  <<<EOD
 188  bar
 189  baz
 190  zoo
 191  foo
 192  EOD
 193  );
 194    }
 195  
 196    function testProcessCustomDirective()
 197    {
 198      $this->_createFile($file = 'foo.js',
 199  <<<EOD
 200  //#define wow "hey"
 201  foo
 202  EOD
 203  );
 204  
 205      $builder = new lmbJsPreprocessor();
 206      $builder->addDirective('define', array('CustomDirectiveProcessor', 'processDefine'));
 207  
 208  $expected = <<<EOD
 209  var wow = "hey";
 210  foo
 211  EOD;
 212      $this->assertEqual($builder->processFile(LIMB_VAR_DIR . $file), $expected);
 213    }
 214  
 215    protected function _createFile($file, $content)
 216    {
 217      lmbFs :: mkdir(LIMB_VAR_DIR);
 218  
 219      $fh = fopen(LIMB_VAR_DIR . $file, 'w');
 220      fwrite($fh, $content);
 221      fclose($fh);
 222      $this->created_files[] = LIMB_VAR_DIR . $file;
 223    }
 224  
 225    protected function _cleanup()
 226    {
 227      foreach($this->created_files as $file)
 228        unlink($file);
 229  
 230      $this->created_files = array();
 231    }
 232  }
 233  
 234  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7