[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/lib/exe/ -> js_js_compress.test.php (source)

   1  <?php
   2  
   3  require_once  DOKU_INC.'lib/exe/js.php';
   4  
   5  class js_js_compress_test extends DokuWikiTest {
   6  
   7      function test_mlcom1(){
   8          $text = '/**
   9                    * A multi
  10                    * line *test*
  11                    * check
  12                    */';
  13          $this->assertEquals(js_compress($text), '');
  14      }
  15  
  16      function test_mlcom2(){
  17          $text = 'var foo=6;/* another comment */';
  18          $this->assertEquals(js_compress($text), 'var foo=6;');
  19      }
  20  
  21      function test_mlcomcond(){
  22          $text = '/*@if (@_win32)';
  23          $this->assertEquals(js_compress($text), '/*@if(@_win32)');
  24      }
  25  
  26      function test_slcom1(){
  27          $text = '// an comment';
  28          $this->assertEquals(js_compress($text), '');
  29      }
  30  
  31      function test_slcom2(){
  32          $text = 'var foo=6;// another comment ';
  33          $this->assertEquals(js_compress($text), 'var foo=6;');
  34      }
  35  
  36      function test_slcom3(){
  37          $text = 'var foo=6;// another comment / or something with // comments ';
  38          $this->assertEquals(js_compress($text), 'var foo=6;');
  39      }
  40  
  41      function test_regex1(){
  42          $text = 'foo.split( /[a-Z\/]*/ );';
  43          $this->assertEquals(js_compress($text), 'foo.split(/[a-Z\/]*/);');
  44      }
  45  
  46      function test_regex_in_array(){
  47          $text = '[/"/ , /"/ , /"/]';
  48          $this->assertEquals(js_compress($text), '[/"/,/"/,/"/]');
  49      }
  50  
  51      function test_regex_in_hash(){
  52          $text = '{ a : /"/ }';
  53          $this->assertEquals(js_compress($text), '{a:/"/}');
  54      }
  55  
  56      function test_regex_preceded_by_spaces_caracters(){
  57          $text = "text.replace( \t \r\n  /\"/ , ".'"//" )';
  58          $this->assertEquals(js_compress($text), 'text.replace(/"/,"//")');
  59      }
  60  
  61      function test_regex_after_and_with_slashes_outside_string(){
  62          $text = 'if ( peng == bla && /pattern\//.test(url)) request = new Something();';
  63          $this->assertEquals(js_compress($text),
  64                              'if(peng==bla&&/pattern\//.test(url))request=new Something();');
  65      }
  66  
  67      function test_regex_after_or_with_slashes_outside_string(){
  68          $text = 'if ( peng == bla || /pattern\//.test(url)) request = new Something();';
  69          $this->assertEquals(js_compress($text),
  70                              'if(peng==bla||/pattern\//.test(url))request=new Something();');
  71      }
  72  
  73      function test_dquot1(){
  74          $text = 'var foo="Now what \\" \'do we//get /*here*/ ?";';
  75          $this->assertEquals(js_compress($text), $text);
  76      }
  77  
  78      function test_dquot2(){
  79          $text = 'var foo="Now what \\\\\\" \'do we//get /*here*/ ?";';
  80          $this->assertEquals(js_compress($text), $text);
  81      }
  82  
  83      function test_dquotrunaway(){
  84          $text = 'var foo="Now where does it end';
  85          $this->assertEquals(js_compress($text), $text);
  86      }
  87  
  88      function test_squot1(){
  89          $text = "var foo='Now what \\' \"do we//get /*here*/ ?';";
  90          $this->assertEquals(js_compress($text), $text);
  91      }
  92  
  93      function test_squotrunaway(){
  94          $text = "var foo='Now where does it end";
  95          $this->assertEquals(js_compress($text), $text);
  96      }
  97  
  98      function test_nl1(){
  99          $text = "var foo=6;\nvar baz=7;";
 100          $this->assertEquals(js_compress($text), 'var foo=6;var baz=7;');
 101      }
 102  
 103      function test_lws1(){
 104          $text = "  \t  var foo=6;";
 105          $this->assertEquals(js_compress($text), 'var foo=6;');
 106      }
 107  
 108      function test_tws1(){
 109          $text = "var foo=6;  \t  ";
 110          $this->assertEquals(js_compress($text), 'var foo=6;');
 111      }
 112  
 113      function test_shortcond(){
 114          $text = "var foo = (baz) ? 'bar' : 'bla';";
 115          $this->assertEquals(js_compress($text), "var foo=(baz)?'bar':'bla';");
 116  
 117      }
 118  
 119      function test_complexminified(){
 120          $text = 'if(!k.isXML(a))try{if(e||!l.match.PSEUDO.test(c)&&!/!=/.test(c)){var f=b.call(a,c);if(f||!d||a.document&&a.document.nodeType!==11)return f}}catch(g){}return k(c,null,null,[a]).length>0}}}(),function(){var a=c.createElement("div");a.innerHTML="<div class=\'test e\'></div><div class=\'test\'></div>";if(!!a.getElementsByClassName&&a.getElementsByClassName("e").length!==0){a.lastChild.className="e";if(a.getElementsByClassName("e").length===1)return;foo="text/*";bla="*/"';
 121  
 122          $this->assertEquals(js_compress($text),$text);
 123      }
 124  
 125      function test_multilinestring(){
 126          $text = 'var foo = "this is a \\'."\n".'multiline string";';
 127          $this->assertEquals('var foo="this is a multiline string";',js_compress($text));
 128  
 129          $text = "var foo = 'this is a \\\nmultiline string';";
 130          $this->assertEquals("var foo='this is a multiline string';",js_compress($text));
 131      }
 132  
 133      function test_nocompress(){
 134          $text = <<<EOF
 135  var meh   =    'test' ;
 136  
 137  /* BEGIN NOCOMPRESS */
 138  
 139  
 140  var foo   =    'test' ;
 141  
 142  var bar   =    'test' ;
 143  
 144  
 145  /* END NOCOMPRESS */
 146  
 147  var moh   =    'test' ;
 148  EOF;
 149          $out = <<<EOF
 150  var meh='test';
 151  var foo   =    'test' ;
 152  
 153  var bar   =    'test' ;
 154  var moh='test';
 155  EOF;
 156  
 157          $this->assertEquals($out, js_compress($text));
 158      }
 159  
 160      function test_plusplus1(){
 161          $text = 'a = 5 + ++b;';
 162          $this->assertEquals('a=5+ ++b;',js_compress($text));
 163      }
 164  
 165      function test_plusplus2(){
 166          $text = 'a = 5+ ++b;';
 167          $this->assertEquals('a=5+ ++b;',js_compress($text));
 168      }
 169  
 170      function test_plusplus3(){
 171          $text = 'a = 5++ + b;';
 172          $this->assertEquals('a=5++ +b;',js_compress($text));
 173      }
 174  
 175      function test_plusplus4(){
 176          $text = 'a = 5++ +b;';
 177          $this->assertEquals('a=5++ +b;',js_compress($text));
 178      }
 179  
 180      function test_minusminus1(){
 181          $text = 'a = 5 - --b;';
 182          $this->assertEquals('a=5- --b;',js_compress($text));
 183      }
 184  
 185      function test_minusminus2(){
 186          $text = 'a = 5- --b;';
 187          $this->assertEquals('a=5- --b;',js_compress($text));
 188      }
 189  
 190      function test_minusminus3(){
 191          $text = 'a = 5-- - b;';
 192          $this->assertEquals('a=5-- -b;',js_compress($text));
 193      }
 194  
 195      function test_minusminus4(){
 196          $text = 'a = 5-- -b;';
 197          $this->assertEquals('a=5-- -b;',js_compress($text));
 198      }
 199  
 200      function test_minusplus1(){
 201          $text = 'a = 5-- +b;';
 202          $this->assertEquals('a=5--+b;',js_compress($text));
 203      }
 204  
 205      function test_minusplus2(){
 206          $text = 'a = 5-- + b;';
 207          $this->assertEquals('a=5--+b;',js_compress($text));
 208      }
 209  
 210      function test_plusminus1(){
 211          $text = 'a = 5++ - b;';
 212          $this->assertEquals('a=5++-b;',js_compress($text));
 213      }
 214  
 215      function test_plusminus2(){
 216          $text = 'a = 5++ -b;';
 217          $this->assertEquals('a=5++-b;',js_compress($text));
 218      }
 219  
 220      function test_unusual_signs(){
 221          $text='var π = Math.PI, τ = 2 * π, halfπ = π / 2, ε = 1e-6, ε2 = ε * ε, radians = π / 180, degrees = 180 / π;';
 222          $this->assertEquals(js_compress($text),
 223                              'var π=Math.PI,τ=2*π,halfπ=π/2,ε=1e-6,ε2=ε*ε,radians=π/180,degrees=180/π;');
 224      }
 225  
 226      /**
 227       * Test the files provided with the original JsStrip
 228       */
 229      function test_original(){
 230          $files = glob(dirname(__FILE__).'/js_js_compress/test-*-in.js');
 231  
 232          foreach($files as $file){
 233              $info = "Using file $file";
 234              $this->assertEquals(js_compress(file_get_contents($file)),
 235                                 file_get_contents(substr($file,0,-5).'out.js'), $info);
 236          };
 237      }
 238  }
 239  
 240  //Setup VIM: ex: et ts=4 :