A few basic tests.
This commit is contained in:
parent
0d1cff40ec
commit
1aa26cdef1
98
test/compress/strict-mode.js
Normal file
98
test/compress/strict-mode.js
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
keep_non_reserved_in_non_strict_mode: {
|
||||
input: {
|
||||
function outer() {
|
||||
var implements = 1;
|
||||
var interface = 1;
|
||||
var let = 1;
|
||||
var package = 1;
|
||||
var private = 1;
|
||||
var protected = 1;
|
||||
var public = 1;
|
||||
var static = 1;
|
||||
|
||||
function inner() {
|
||||
implements++;
|
||||
interface++;
|
||||
let++;
|
||||
package++;
|
||||
private++;
|
||||
protected++;
|
||||
public++;
|
||||
static++;
|
||||
}
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function outer() {
|
||||
var implements = 1;
|
||||
var interface = 1;
|
||||
var let = 1;
|
||||
var package = 1;
|
||||
var private = 1;
|
||||
var protected = 1;
|
||||
var public = 1;
|
||||
var static = 1;
|
||||
|
||||
function inner() {
|
||||
implements++;
|
||||
interface++;
|
||||
let++;
|
||||
package++;
|
||||
private++;
|
||||
protected++;
|
||||
public++;
|
||||
static++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mangle_non_reserved_in_non_strict_mode: {
|
||||
mangle = {}
|
||||
input: {
|
||||
function outer() {
|
||||
var implements = 1;
|
||||
var interface = 1;
|
||||
var let = 1;
|
||||
var package = 1;
|
||||
var private = 1;
|
||||
var protected = 1;
|
||||
var public = 1;
|
||||
var static = 1;
|
||||
|
||||
function inner() {
|
||||
implements++;
|
||||
interface++;
|
||||
let++;
|
||||
package++;
|
||||
private++;
|
||||
protected++;
|
||||
public++;
|
||||
static++;
|
||||
}
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function outer() {
|
||||
var r=1;
|
||||
var a=1;
|
||||
var v=1;
|
||||
var n=1;
|
||||
var o=1;
|
||||
var t=1;
|
||||
var u=1;
|
||||
var c=1;
|
||||
function f() {
|
||||
r++;
|
||||
a++;
|
||||
v++;
|
||||
n++;
|
||||
o++;
|
||||
t++;
|
||||
u++;
|
||||
c++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
77
test/strict-mode-mangler.js
Normal file
77
test/strict-mode-mangler.js
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
var UglifyJS = require("..");
|
||||
var assert = require("assert");
|
||||
|
||||
var alphabet = "abcdefghijklmnop".split("");
|
||||
var perms = [];
|
||||
var linesOfCode = [];
|
||||
var startTime = new Date().getTime();
|
||||
|
||||
console.log("--- Strict mode mangling tests");
|
||||
|
||||
function permute(text) {
|
||||
perms.push(text);
|
||||
if(text.length < 4) {
|
||||
for(var i = 0; i < alphabet.length; i++) {
|
||||
permute(text + alphabet[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
permute("");
|
||||
|
||||
var permuteTime = new Date().getTime() - startTime;
|
||||
|
||||
console.log(" - Generated perms in " + permuteTime + "ms")
|
||||
console.log(" - Testing with " + perms.length + " identifiers");
|
||||
console.log(" - Mangling (this will take some time)...");
|
||||
|
||||
function getCode(withStrictMode) {
|
||||
|
||||
linesOfCode = " function outer() {\n";
|
||||
if (!!withStrictMode) {
|
||||
linesOfCode += " \"use strict\";\n";
|
||||
}
|
||||
for (var i in perms) {
|
||||
linesOfCode += " var _" + perms[i] + " = 1;\n"
|
||||
}
|
||||
linesOfCode += " function inner() {\n";
|
||||
for (var i in perms) {
|
||||
linesOfCode += " _" + perms[i] + "++;\n";
|
||||
}
|
||||
linesOfCode += " }\n";
|
||||
linesOfCode += " }\n";
|
||||
|
||||
return linesOfCode;
|
||||
|
||||
}
|
||||
|
||||
module.exports = function () {
|
||||
|
||||
var startManglingTime = new Date().getTime();
|
||||
var mangled_code = mangle(getCode(false));
|
||||
|
||||
console.log(" - Found 'var let' at char: " + mangled_code.indexOf('var let'));
|
||||
console.log(" - Mangled code with no strict mode in " + Math.floor((new Date().getTime() - startManglingTime)/ 1000) + "s");
|
||||
assert(mangled_code.indexOf('var let') >= 0, "No 'var let' produced in the mangled code outside strict mode.");
|
||||
|
||||
startManglingTime = new Date().getTime();
|
||||
var mangled_strict_code = mangle(getCode(true));
|
||||
console.log(" - This should be -1: " + mangled_strict_code.indexOf('var let'));
|
||||
console.log(" - Mangled code with strict mode in " + Math.floor((new Date().getTime() - startManglingTime)/ 1000) + "s");
|
||||
assert(mangled_strict_code.indexOf('var let') === -1, "A 'var let' statement was produced inside strict mode.");
|
||||
|
||||
}
|
||||
|
||||
function mangle(js) {
|
||||
var stream = UglifyJS.OutputStream();
|
||||
var parsed = UglifyJS.parse(js);
|
||||
parsed.figure_out_scope();
|
||||
parsed.mangle_names();
|
||||
parsed.print(stream);
|
||||
return stream.toString();
|
||||
}
|
||||
|
||||
// Run standalone
|
||||
if (module.parent === null) {
|
||||
module.exports();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user