aboutsummaryrefslogtreecommitdiff
path: root/test/mocha/templates.js
blob: 169e791e70d9afdccd7a7570db9c61077b0e71cc (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var assert = require("assert");
var run_code = require("../sandbox").run_code;
var semver = require("semver");
var UglifyJS = require("../node");

describe("Template literals", function() {
    it("Should reject invalid literal", function() {
        [
            "`foo\\`",
            "`foo${bar`",
            "`foo${bar}",
        ].forEach(function(input) {
            assert.throws(function() {
                UglifyJS.parse(input);
            }, function(e) {
                return e instanceof UglifyJS.JS_Parse_Error
                    && e.message === "Unterminated template literal";
            }, input);
        });
    });
    it("Should reject invalid expression", function() {
        [
            "`foo${bar;}`",
            "`foo${42bar}`",
        ].forEach(function(input) {
            assert.throws(function() {
                UglifyJS.parse(input);
            }, function(e) {
                return e instanceof UglifyJS.JS_Parse_Error;
            }, input);
        });
    });
    it("Should process line-break characters correctly", function() {
        [
            // native line breaks
            [ "`foo\nbar`", "`foo\nbar`" ],
            [ "`foo\rbar`", "`foo\rbar`" ],
            [ "`foo\r\nbar`", "`foo\nbar`" ],
            [ "`foo\r\n\rbar`", "`foo\n\rbar`" ],
            // escaped line breaks
            [ "`foo\\nbar`", "`foo\\nbar`" ],
            [ "`foo\\rbar`", "`foo\\rbar`" ],
            [ "`foo\r\\nbar`", "`foo\r\\nbar`" ],
            [ "`foo\\r\nbar`", "`foo\\r\nbar`" ],
            [ "`foo\\r\\nbar`", "`foo\\r\\nbar`" ],
            // continuation
            [ "`foo\\\nbar`", "`foo\\\nbar`" ],
            [ "`foo\\\rbar`", "`foo\\\rbar`" ],
            [ "`foo\\\r\nbar`", "`foo\\\nbar`" ],
            [ "`foo\\\r\n\rbar`", "`foo\\\n\rbar`" ],
            [ "`foo\\\\nbar`", "`foo\\\\nbar`" ],
            [ "`foo\\\\rbar`", "`foo\\\\rbar`" ],
            [ "`foo\\\\r\nbar`", "`foo\\\\r\nbar`" ],
        ].forEach(function(test) {
            var input = "console.log(" + test[0] + ");";
            var result = UglifyJS.minify(input, {
                compress: false,
                mangle: false,
            });
            if (result.error) throw result.error;
            var expected = "console.log(" + test[1] + ");";
            assert.strictEqual(result.code, expected, test[0]);
            if (semver.satisfies(process.version, "<4")) return;
            assert.strictEqual(run_code(result.code), run_code(input), test[0]);
        });
    });
});