From: Alex Lam S.L Date: Fri, 24 Mar 2017 05:19:50 +0000 (+0800) Subject: fix expect_stdout (#1642) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=701035621d708c39f4bdd2022fc02bdb4a967a39;p=UglifyJS.git fix expect_stdout (#1642) `compress()` may modify input ASTs add tests for #1627 & #1640 --- diff --git a/test/compress/transform.js b/test/compress/transform.js index 1cc72c07..48aa605e 100644 --- a/test/compress/transform.js +++ b/test/compress/transform.js @@ -30,7 +30,6 @@ booleans_global_defs: { expect: { console.log(!0); } - expect_stdout: true } condition_evaluate: { diff --git a/test/input/invalid/assign_1.js b/test/input/invalid/assign_1.js new file mode 100644 index 00000000..6d09d132 --- /dev/null +++ b/test/input/invalid/assign_1.js @@ -0,0 +1 @@ +console.log(1 || 5--); diff --git a/test/input/invalid/assign_2.js b/test/input/invalid/assign_2.js new file mode 100644 index 00000000..197bdc90 --- /dev/null +++ b/test/input/invalid/assign_2.js @@ -0,0 +1 @@ +console.log(2 || (Math.random() /= 2)); diff --git a/test/input/invalid/assign_3.js b/test/input/invalid/assign_3.js new file mode 100644 index 00000000..7c560e4b --- /dev/null +++ b/test/input/invalid/assign_3.js @@ -0,0 +1 @@ +console.log(3 || ++this); diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 2b44c901..33749045 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -251,4 +251,59 @@ describe("bin/uglifyjs", function () { done(); }); }); + it("Should support hyphen as shorthand", function(done) { + var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep-fnames=true'; + + 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(); + }); + }); + 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--);", + " ^", + "SyntaxError: 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));", + " ^", + "SyntaxError: 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,23", + "console.log(3 || ++this);", + " ^", + "SyntaxError: Invalid use of ++ operator" + ].join("\n")); + done(); + }); + }); }); diff --git a/test/run-tests.js b/test/run-tests.js index 09e70021..3d291416 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -105,18 +105,6 @@ function run_compress_tests() { function test_case(test) { log_test(test.name); U.base54.reset(); - var options = U.defaults(test.options, { - warnings: false - }); - var warnings_emitted = []; - var original_warn_function = U.AST_Node.warn_function; - if (test.expect_warnings) { - U.AST_Node.warn_function = function(text) { - warnings_emitted.push("WARN: " + text); - }; - if (!options.warnings) options.warnings = true; - } - var cmp = new U.Compressor(options, true); var output_options = test.beautify || {}; var expect; if (test.expect) { @@ -125,7 +113,8 @@ function run_compress_tests() { expect = test.expect_exact; } var input = as_toplevel(test.input, test.mangle); - var input_code = make_code(test.input, { + var input_code = make_code(input, output_options); + var input_formatted = make_code(test.input, { beautify: true, quote_style: 3, keep_quoted_props: true @@ -133,6 +122,18 @@ function run_compress_tests() { if (test.mangle_props) { input = U.mangle_properties(input, test.mangle_props); } + var options = U.defaults(test.options, { + warnings: false + }); + var warnings_emitted = []; + var original_warn_function = U.AST_Node.warn_function; + if (test.expect_warnings) { + U.AST_Node.warn_function = function(text) { + warnings_emitted.push("WARN: " + text); + }; + if (!options.warnings) options.warnings = true; + } + var cmp = new U.Compressor(options, true); var output = cmp.compress(input); output.figure_out_scope(test.mangle); if (test.mangle) { @@ -142,7 +143,7 @@ function run_compress_tests() { output = make_code(output, output_options); if (expect != output) { log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", { - input: input_code, + input: input_formatted, output: output, expected: expect }); @@ -155,7 +156,7 @@ function run_compress_tests() { var reparsed_ast = U.parse(output); } catch (ex) { log("!!! Test matched expected result but cannot parse output\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n--REPARSE ERROR--\n{error}\n\n", { - input: input_code, + input: input_formatted, output: output, error: ex.toString(), }); @@ -174,7 +175,7 @@ function run_compress_tests() { var actual_warnings = JSON.stringify(warnings_emitted); if (expected_warnings != actual_warnings) { log("!!! failed\n---INPUT---\n{input}\n---EXPECTED WARNINGS---\n{expected_warnings}\n---ACTUAL WARNINGS---\n{actual_warnings}\n\n", { - input: input_code, + input: input_formatted, expected_warnings: expected_warnings, actual_warnings: actual_warnings, }); @@ -183,13 +184,13 @@ function run_compress_tests() { } } if (test.expect_stdout) { - var stdout = run_code(make_code(input, output_options)); + var stdout = run_code(input_code); if (test.expect_stdout === true) { test.expect_stdout = stdout; } if (!same_stdout(test.expect_stdout, stdout)) { log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", { - input: input_code, + input: input_formatted, expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR", expected: test.expect_stdout, actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR", @@ -201,7 +202,7 @@ function run_compress_tests() { stdout = run_code(output); if (!same_stdout(test.expect_stdout, stdout)) { log("!!! failed\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", { - input: input_code, + input: input_formatted, expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR", expected: test.expect_stdout, actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR",