wiki.techinc.nl/tests/phpunit/includes/libs/JavaScriptMinifierTest.php
Brion Vibber 6b06770b6d Followup r103865 and r103915
r103915 added a parse error for 'more than 2 decimal points' in a number; this is the wrong place to check for that. Should only check whether there's more digits or identifier chars -- identifier chars would be illegal.

Added test cases for the exponent missing fails, tweaked it to be more consistent (only need to check for one e; if we have more we can lump them in with 'not digits' :)


Also cleaned up no-longer-needed suppress/restore warnings around JS parser invocation
2011-11-22 23:10:22 +00:00

151 lines
5.9 KiB
PHP

<?php
class JavaScriptMinifierTest extends MediaWikiTestCase {
function provideCases() {
return array(
// Basic tokens
array( "\r\t\f \v\n\r", "" ),
array( "/* Foo *\n*bar\n*/", "" ),
/**
* ' Foo \' bar \
* baz \' quox ' .
*/
array( "' Foo \\' bar \\\n baz \\' quox ' .length", "' Foo \\' bar \\\n baz \\' quox '.length" ),
array( "\" Foo \\\" bar \\\n baz \\\" quox \" .length", "\" Foo \\\" bar \\\n baz \\\" quox \".length" ),
array( "// Foo b/ar baz", "" ),
array( "/ Foo \\/ bar [ / \\] / ] baz / .length", "/ Foo \\/ bar [ / \\] / ] baz /.length" ),
// HTML comments
array( "<!-- Foo bar", "" ),
array( "<!-- Foo --> bar", "" ),
array( "--> Foo", "" ),
array( "x --> y", "x-->y" ),
// Semicolon insertion
array( "(function(){return\nx;})", "(function(){return\nx;})" ),
array( "throw\nx;", "throw\nx;" ),
array( "while(p){continue\nx;}", "while(p){continue\nx;}" ),
array( "while(p){break\nx;}", "while(p){break\nx;}" ),
array( "var\nx;", "var x;" ),
array( "x\ny;", "x\ny;" ),
array( "x\n++y;", "x\n++y;" ),
array( "x\n!y;", "x\n!y;" ),
array( "x\n{y}", "x\n{y}" ),
array( "x\n+y;", "x+y;" ),
array( "x\n(y);", "x(y);" ),
array( "5.\nx;", "5.\nx;" ),
array( "0xFF.\nx;", "0xFF.x;" ),
array( "5.3.\nx;", "5.3.x;" ),
// Token separation
array( "x in y", "x in y" ),
array( "/x/g in y", "/x/g in y" ),
array( "x in 30", "x in 30" ),
array( "x + ++ y", "x+ ++y" ),
array( "x / /y/.exec(z)", "x/ /y/.exec(z)" ),
// State machine
array( "/ x/g", "/ x/g" ),
array( "(function(){return/ x/g})", "(function(){return/ x/g})" ),
array( "+/ x/g", "+/ x/g" ),
array( "++/ x/g", "++/ x/g" ),
array( "x/ x/g", "x/x/g" ),
array( "(/ x/g)", "(/ x/g)" ),
array( "if(/ x/g);", "if(/ x/g);" ),
array( "(x/ x/g)", "(x/x/g)" ),
array( "([/ x/g])", "([/ x/g])" ),
array( "+x/ x/g", "+x/x/g" ),
array( "{}/ x/g", "{}/ x/g" ),
array( "+{}/ x/g", "+{}/x/g" ),
array( "(x)/ x/g", "(x)/x/g" ),
array( "if(x)/ x/g", "if(x)/ x/g" ),
array( "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ),
array( "x;x;{}/ x/g", "x;x;{}/ x/g" ),
array( "x:{}/ x/g", "x:{}/ x/g" ),
array( "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ),
array( "function x(){}/ x/g", "function x(){}/ x/g" ),
array( "+function x(){}/ x/g", "+function x(){}/x/g" ),
// Tests for things that broke in the past
// Multiline quoted string
array( "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ),
// Multiline quoted string followed by string with spaces
array( "var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n", "var foo=\"\\\nblah\\\n\";var baz=\" foo \";" ),
// URL in quoted string ( // is not a comment)
array( "aNode.setAttribute('href','http://foo.bar.org/baz');", "aNode.setAttribute('href','http://foo.bar.org/baz');" ),
// URL in quoted string after multiline quoted string
array( "var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');", "var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');" ),
// Division vs. regex nastiness
array( "alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );", "alert((10+10)/'/'.charCodeAt(0)+'//');" ),
array( "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ),
// newline insertion after 1000 chars: break after the "++", not before
array( str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ),
// Unicode letter characters should pass through ok in identifiers (bug 31187)
array( "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}'),
// And also per spec unicode char escape values should work in identifiers,
// as long as it's a valid char. In future it might get normalized.
array( "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}'),
/* Some structures that might look invalid at first sight */
array( "var a = 5.;", "var a=5.;" ),
array( "5.0.toString();", "5.0.toString();" ),
array( "5..toString();", "5..toString();" ),
array( "5.\n.toString();", '5..toString();' ),
/* Some structures that are invalid, that we may not detect */
//array( "5...toString();", false ), // can't detect this yet, though JSParser will throw error
array( '5e1', '5e1' ),
array( "5e", false ), // no exponent (end)
array( "5eee1", false ), // no exponent (multiple e)
array( "5e++", false ), // no exponent (other symbol)
);
}
/**
* @dataProvider provideCases
*/
function testJavaScriptMinifierOutput( $code, $expectedOutput ) {
$minified = JavaScriptMinifier::minify( $code );
// JSMin+'s parser will throw an exception if output is not valid JS.
$parser = new JSParser();
$parser->parse( $minified, 'minify-test.js', 1 );
$this->assertEquals( $expectedOutput, $minified, "Minified output should be in the form expected." );
}
/**
* @dataProvider provideBug32548
*/
function testBug32548Exponent($num) {
// Long line breaking was being incorrectly done between the base and
// exponent part of a number, causing a syntax error. The line should
// instead break at the start of the number.
$prefix = 'var longVarName' . str_repeat('_', 973) . '=';
$suffix = ',shortVarName=0;';
$input = $prefix . $num . $suffix;
$expected = $prefix . "\n" . $num . $suffix;
$minified = JavaScriptMinifier::minify( $input );
$this->assertEquals( $expected, $minified, "Line breaks must not occur in middle of exponent");
}
function provideBug32548() {
return array(
array(
// This one gets interpreted all together by the prior code;
// no break at the 'E' happens.
'1.23456789E55',
),
array(
// This one breaks under the bad code; splits between 'E' and '+'
'1.23456789E+5',
),
array(
// This one breaks under the bad code; splits between 'E' and '-'
'1.23456789E-5',
),
);
}
}