From 0111497fc98d5098f81bc749f77da5734add37bb Mon Sep 17 00:00:00 2001 From: Anthony Van de Gejuchte Date: Sat, 3 Sep 2016 23:26:31 +0200 Subject: Make all comment options in cli available in js api Also removing more code within "loop" while at it. --- test/input/comments/filter.js | 3 +++ test/mocha/cli.js | 34 ++++++++++++++++++++++++++++++++-- test/mocha/comment-filter.js | 15 +++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/input/comments/filter.js (limited to 'test') diff --git a/test/input/comments/filter.js b/test/input/comments/filter.js new file mode 100644 index 00000000..c752080f --- /dev/null +++ b/test/input/comments/filter.js @@ -0,0 +1,3 @@ +// foo +/*@preserve*/ +// bar diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 38b57cd7..495b0076 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -2,11 +2,11 @@ var assert = require("assert"); var exec = require("child_process").exec; describe("bin/uglifyjs", function () { + var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs'; it("should produce a functional build when using --self", function (done) { this.timeout(5000); - var uglifyjs = '"' + process.argv[0] + '" bin/uglifyjs'; - var command = uglifyjs + ' --self -cm --wrap WrappedUglifyJS'; + var command = uglifyjscmd + ' --self -cm --wrap WrappedUglifyJS'; exec(command, function (err, stdout) { if (err) throw err; @@ -19,4 +19,34 @@ describe("bin/uglifyjs", function () { done(); }); }); + it("Should be able to filter comments correctly with `--comment all`", function (done) { + var command = uglifyjscmd + ' test/input/comments/filter.js --comments all'; + + exec(command, function (err, stdout) { + if (err) throw err; + + assert.strictEqual(stdout, "// foo\n/*@preserve*/\n// bar\n\n"); + done(); + }); + }); + it("Should be able to filter comments correctly with `--comment `", function (done) { + var command = uglifyjscmd + ' test/input/comments/filter.js --comments /r/'; + + exec(command, function (err, stdout) { + if (err) throw err; + + assert.strictEqual(stdout, "/*@preserve*/\n// bar\n\n"); + done(); + }); + }); + it("Should be able to filter comments correctly with just `--comment`", function (done) { + var command = uglifyjscmd + ' test/input/comments/filter.js --comments'; + + exec(command, function (err, stdout) { + if (err) throw err; + + assert.strictEqual(stdout, "/*@preserve*/\n\n"); + done(); + }); + }); }); diff --git a/test/mocha/comment-filter.js b/test/mocha/comment-filter.js index ea2ec2eb..4330d1eb 100644 --- a/test/mocha/comment-filter.js +++ b/test/mocha/comment-filter.js @@ -7,6 +7,16 @@ describe("comment filters", function() { assert.strictEqual(ast.print_to_string({comments: /^!/}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n"); }); + it("Should be able to filter comments with the 'all' option", function() { + var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); + assert.strictEqual(ast.print_to_string({comments: "all"}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n"); + }); + + it("Should be able to filter commments with the 'some' option", function() { + var ast = UglifyJS.parse("// foo\n/*@preserve*/\n// bar\n/*@license*/\n//@license with the wrong comment type\n/*@cc_on something*/"); + assert.strictEqual(ast.print_to_string({comments: "some"}), "/*@preserve*/\n/*@license*/\n/*@cc_on something*/\n"); + }); + it("Should be able to filter comments by passing a function", function() { var ast = UglifyJS.parse("/*TEST 123*/\n//An other comment\n//8 chars."); var f = function(node, comment) { @@ -16,6 +26,11 @@ describe("comment filters", function() { assert.strictEqual(ast.print_to_string({comments: f}), "/*TEST 123*/\n//8 chars.\n"); }); + it("Should be able to filter comments by passing regex in string format", function() { + var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); + assert.strictEqual(ast.print_to_string({comments: "/^!/"}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n"); + }); + it("Should be able to get the comment and comment type when using a function", function() { var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\ntest7\n-->!test8"); var f = function(node, comment) { -- cgit v1.2.3