diff options
author | Richard van Velzen <rvanvelzen1@gmail.com> | 2016-01-14 08:54:40 +0100 |
---|---|---|
committer | Richard van Velzen <rvanvelzen1@gmail.com> | 2016-01-14 08:54:40 +0100 |
commit | ac8db977b95b447cba577b41285cb0fa9e65ebdd (patch) | |
tree | 76592b4c0effcc7cc2ef9aa3195e9ca557951a4a | |
parent | fe4e9f9d97dcc6594a8fc49e04630aa619ff1866 (diff) | |
parent | 88b77ddaa9d6b3d55e537dc21030ac58ddfcb86e (diff) | |
download | tracifyjs-ac8db977b95b447cba577b41285cb0fa9e65ebdd.tar.gz tracifyjs-ac8db977b95b447cba577b41285cb0fa9e65ebdd.zip |
Merge pull request #905 from avdg/unit-tests
Add unit tests
-rw-r--r-- | lib/parse.js | 2 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | test/mocha.js | 29 | ||||
-rw-r--r-- | test/mocha/string-literal.js | 34 | ||||
-rwxr-xr-x | test/run-tests.js | 3 |
5 files changed, 69 insertions, 2 deletions
diff --git a/lib/parse.js b/lib/parse.js index 7e7b2272..2218c00c 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -399,7 +399,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) { if (octal_len > 0) ch = String.fromCharCode(parseInt(ch, 8)); else ch = read_escaped_char(true); } - else if (ch == "\n") parse_error("Unterminated string constant"); + else if ("\r\n\u2028\u2029".indexOf(ch) >= 0) parse_error("Unterminated string constant"); else if (ch == quote) break; ret += ch; } diff --git a/package.json b/package.json index 6b0d2f40..bd4fb3e7 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,8 @@ "acorn": "~0.6.0", "escodegen": "~1.3.3", "esfuzz": "~0.3.1", - "estraverse": "~1.5.1" + "estraverse": "~1.5.1", + "mocha": "~2.3.4" }, "browserify": { "transform": [ diff --git a/test/mocha.js b/test/mocha.js new file mode 100644 index 00000000..411f52c5 --- /dev/null +++ b/test/mocha.js @@ -0,0 +1,29 @@ +var Mocha = require('mocha'), + fs = require('fs'), + path = require('path'); + +// Instantiate a Mocha instance. +var mocha = new Mocha({}); + +var testDir = __dirname + '/mocha/'; + +// Add each .js file to the mocha instance +fs.readdirSync(testDir).filter(function(file){ + // Only keep the .js files + return file.substr(-3) === '.js'; + +}).forEach(function(file){ + mocha.addFile( + path.join(testDir, file) + ); +}); + +module.exports = function() { + mocha.run(function(failures) { + if (failures !== 0) { + process.on('exit', function () { + process.exit(failures); + }); + } + }); +};
\ No newline at end of file diff --git a/test/mocha/string-literal.js b/test/mocha/string-literal.js new file mode 100644 index 00000000..84aaad7e --- /dev/null +++ b/test/mocha/string-literal.js @@ -0,0 +1,34 @@ +var UglifyJS = require('../../'); +var assert = require("assert"); + +describe("String literals", function() { + it("Should throw syntax error if a string literal contains a newline", function() { + var inputs = [ + "'\n'", + "'\r'", + '"\r\n"', + "'\u2028'", + '"\u2029"' + ]; + + var test = function(input) { + return function() { + var ast = UglifyJS.parse(input); + }; + }; + + var error = function(e) { + return e instanceof UglifyJS.JS_Parse_Error && + e.message === "Unterminated string constant"; + }; + + for (var input in inputs) { + assert.throws(test(inputs[input]), error); + } + }); + + it("Should not throw syntax error if a string has a line continuation", function() { + var output = UglifyJS.parse('var a = "a\\\nb";').print_to_string(); + assert.equal(output, 'var a="ab";'); + }); +});
\ No newline at end of file diff --git a/test/run-tests.js b/test/run-tests.js index 3ec04fda..b9a0f825 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -16,6 +16,9 @@ if (failures) { process.exit(1); } +var mocha_tests = require("./mocha.js"); +mocha_tests(); + var run_sourcemaps_tests = require('./sourcemaps'); run_sourcemaps_tests(); |