diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/input/invalid/else.js | 1 | ||||
-rw-r--r-- | test/input/invalid/return.js | 1 | ||||
-rw-r--r-- | test/mocha/cli.js | 504 |
3 files changed, 269 insertions, 237 deletions
diff --git a/test/input/invalid/else.js b/test/input/invalid/else.js new file mode 100644 index 00000000..89e8e501 --- /dev/null +++ b/test/input/invalid/else.js @@ -0,0 +1 @@ +if (0) else 1; diff --git a/test/input/invalid/return.js b/test/input/invalid/return.js new file mode 100644 index 00000000..d232c623 --- /dev/null +++ b/test/input/invalid/return.js @@ -0,0 +1 @@ +return 42; diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 38f61f39..335b224d 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -55,15 +55,15 @@ describe("bin/uglifyjs", function () { }); }); it("Should append source map to output when using --source-map url=inline", function (done) { - var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map url=inline"; + var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map url=inline"; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n" + - "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxHQUFJQSxLQUFNLFdBQ04sUUFBU0MsS0FBS0QsS0FDVixNQUFPQSxLQUdYLE1BQU9DIn0=\n"); - done(); - }); + assert.strictEqual(stdout, "var bar=function(){function foo(bar){return bar}return foo}();\n" + + "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMTMyMy9zYW1wbGUuanMiXSwibmFtZXMiOlsiYmFyIiwiZm9vIl0sIm1hcHBpbmdzIjoiQUFBQSxHQUFJQSxLQUFNLFdBQ04sUUFBU0MsS0FBS0QsS0FDVixNQUFPQSxLQUdYLE1BQU9DIn0=\n"); + done(); + }); }); it("should not append source map to output when not using --source-map url=inline", function (done) { var command = uglifyjscmd + ' test/input/issue-1323/sample.js'; @@ -76,84 +76,84 @@ describe("bin/uglifyjs", function () { }); }); it("Should work with --keep-fnames (mangle only)", function (done) { - var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m'; + var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); - done(); - }); + assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); + done(); + }); }); it("Should work with --keep-fnames (mangle & compress)", function (done) { - var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false'; + var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n"); - done(); - }); + assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(5==f(g)());\n"); + done(); + }); }); it("Should work with keep_fnames under mangler options", function (done) { - var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fnames=true'; + var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep_fnames=true'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); - done(); - }); + assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n"); + done(); + }); }); it("Should work with --define (simple)", function (done) { - var command = uglifyjscmd + ' test/input/global_defs/simple.js --define D=5 -c'; + var command = uglifyjscmd + ' test/input/global_defs/simple.js --define D=5 -c'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "console.log(5);\n"); - done(); - }); + assert.strictEqual(stdout, "console.log(5);\n"); + done(); + }); }); it("Should work with --define (nested)", function (done) { - var command = uglifyjscmd + ' test/input/global_defs/nested.js --define C.D=5,C.V=3 -c'; + var command = uglifyjscmd + ' test/input/global_defs/nested.js --define C.D=5,C.V=3 -c'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "console.log(3,5);\n"); - done(); - }); + assert.strictEqual(stdout, "console.log(3,5);\n"); + done(); + }); }); it("Should work with --define (AST_Node)", function (done) { - var command = uglifyjscmd + ' test/input/global_defs/simple.js --define console.log=stdout.println -c'; + var command = uglifyjscmd + ' test/input/global_defs/simple.js --define console.log=stdout.println -c'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, "stdout.println(D);\n"); - done(); - }); + assert.strictEqual(stdout, "stdout.println(D);\n"); + done(); + }); }); it("Should work with `--beautify`", function (done) { - var command = uglifyjscmd + ' test/input/issue-1482/input.js -b'; + var command = uglifyjscmd + ' test/input/issue-1482/input.js -b'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, read("test/input/issue-1482/default.js")); - done(); - }); + assert.strictEqual(stdout, read("test/input/issue-1482/default.js")); + done(); + }); }); it("Should work with `--beautify bracketize`", function (done) { - var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize'; + var command = uglifyjscmd + ' test/input/issue-1482/input.js -b bracketize'; - exec(command, function (err, stdout) { - if (err) throw err; + exec(command, function (err, stdout) { + if (err) throw err; - assert.strictEqual(stdout, read("test/input/issue-1482/bracketize.js")); - done(); - }); + assert.strictEqual(stdout, read("test/input/issue-1482/bracketize.js")); + done(); + }); }); it("Should process inline source map", function(done) { var command = uglifyjscmd + " test/input/issue-520/input.js -mc toplevel --source-map content=inline,url=inline"; @@ -260,214 +260,244 @@ describe("bin/uglifyjs", function () { }); }); it("Should throw syntax error (5--)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_1.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_1.js:1,18", - "console.log(1 || 5--);", - " ^", - "ERROR: Invalid use of -- operator" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/assign_1.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_1.js:1,18", + "console.log(1 || 5--);", + " ^", + "ERROR: Invalid use of -- operator" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (Math.random() /= 2)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_2.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_2.js:1,32", - "console.log(2 || (Math.random() /= 2));", - " ^", - "ERROR: Invalid assignment" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/assign_2.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_2.js:1,32", + "console.log(2 || (Math.random() /= 2));", + " ^", + "ERROR: Invalid assignment" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (++this)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_3.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_3.js:1,17", - "console.log(3 || ++this);", - " ^", - "ERROR: Invalid use of ++ operator" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/assign_3.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_3.js:1,17", + "console.log(3 || ++this);", + " ^", + "ERROR: Invalid use of ++ operator" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (++null)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/assign_4.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/assign_4.js:1,0", - "++null", - "^", - "ERROR: Invalid use of ++ operator" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/assign_4.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/assign_4.js:1,0", + "++null", + "^", + "ERROR: Invalid use of ++ operator" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (a.=)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/dot_1.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/dot_1.js:1,2", - "a.=", - " ^", - "ERROR: Unexpected token: operator (=)" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/dot_1.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/dot_1.js:1,2", + "a.=", + " ^", + "ERROR: Unexpected token: operator (=)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (%.a)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/dot_2.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/dot_2.js:1,0", - "%.a;", - "^", - "ERROR: Unexpected token: operator (%)" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/dot_2.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/dot_2.js:1,0", + "%.a;", + "^", + "ERROR: Unexpected token: operator (%)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (a./();)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/dot_3.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/dot_3.js:1,2", - "a./();", - " ^", - "ERROR: Unexpected token: operator (/)" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/dot_3.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/dot_3.js:1,2", + "a./();", + " ^", + "ERROR: Unexpected token: operator (/)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error ({%: 1})", function(done) { - var command = uglifyjscmd + ' test/input/invalid/object.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/object.js:1,13", - "console.log({%: 1});", - " ^", - "ERROR: Unexpected token: operator (%)" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/object.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/object.js:1,13", + "console.log({%: 1});", + " ^", + "ERROR: Unexpected token: operator (%)" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (delete x)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/delete.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/delete.js:13,11", - " delete x;", - " ^", - "ERROR: Calling delete on expression not allowed in strict mode" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/delete.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/delete.js:13,11", + " delete x;", + " ^", + "ERROR: Calling delete on expression not allowed in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (function g(arguments))", function(done) { - var command = uglifyjscmd + ' test/input/invalid/function_1.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/function_1.js:4,11", - "function g(arguments) {", - " ^", - "ERROR: Unexpected arguments in strict mode" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/function_1.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/function_1.js:4,11", + "function g(arguments) {", + " ^", + "ERROR: Unexpected arguments in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (function eval())", function(done) { - var command = uglifyjscmd + ' test/input/invalid/function_2.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/function_2.js:4,9", - "function eval() {", - " ^", - "ERROR: Unexpected eval in strict mode" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/function_2.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/function_2.js:4,9", + "function eval() {", + " ^", + "ERROR: Unexpected eval in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (iife arguments())", function(done) { - var command = uglifyjscmd + ' test/input/invalid/function_3.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/function_3.js:4,10", - "!function arguments() {", - " ^", - "ERROR: Unexpected arguments in strict mode" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/function_3.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/function_3.js:4,10", + "!function arguments() {", + " ^", + "ERROR: Unexpected arguments in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (catch(eval))", function(done) { - var command = uglifyjscmd + ' test/input/invalid/try.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/try.js:7,18", - " try {} catch (eval) {}", - " ^", - "ERROR: Unexpected eval in strict mode" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/try.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/try.js:7,18", + " try {} catch (eval) {}", + " ^", + "ERROR: Unexpected eval in strict mode" + ].join("\n")); + done(); + }); }); it("Should throw syntax error (var eval)", function(done) { - var command = uglifyjscmd + ' test/input/invalid/var.js'; - - exec(command, function (err, stdout, stderr) { - assert.ok(err); - assert.strictEqual(stdout, ""); - assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ - "Parse error at test/input/invalid/var.js:7,8", - " var eval;", - " ^", - "ERROR: Unexpected eval in strict mode" - ].join("\n")); - done(); - }); + var command = uglifyjscmd + ' test/input/invalid/var.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/var.js:7,8", + " var eval;", + " ^", + "ERROR: Unexpected eval in strict mode" + ].join("\n")); + done(); + }); + }); + it("Should throw syntax error (else)", function(done) { + var command = uglifyjscmd + ' test/input/invalid/else.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/else.js:1,7", + "if (0) else 1;", + " ^", + "ERROR: Unexpected token: keyword (else)" + ].join("\n")); + done(); + }); + }); + it("Should throw syntax error (return)", function(done) { + var command = uglifyjscmd + ' test/input/invalid/return.js'; + + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [ + "Parse error at test/input/invalid/return.js:1,0", + "return 42;", + "^", + "ERROR: 'return' outside of function" + ].join("\n")); + done(); + }); }); it("Should handle literal string as source map input", function(done) { var command = [ |