-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
constant-folding-test.js
106 lines (92 loc) · 1.97 KB
/
constant-folding-test.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
jest.autoMockOff();
const babel = require("babel-core");
const unpad = require("../../../utils/unpad");
function transform(code) {
return babel.transform(code, {
plugins: [require("../src/index")]
}).code;
}
describe("constant-folding-plugin", () => {
it("should evaluate some expressions", () => {
const source = unpad(`
"a" + "b"
2 * 3;
1/3;
4 | 3;
a(), b();
var x = 1;
foo(x);
"b" + a + "c" + "d" + g + z + "f" + "h" + "z"
`);
const expected = unpad(`
"ab";
6;
1 / 3;
7;
a(), b();
var x = 1;
foo(x);
"b" + a + "cd" + g + z + "fhz";
`);
expect(transform(source)).toBe(expected);
});
it("should skip -0", () => {
const source = unpad(`
-0;
+-0;
+0;
`);
const expected = unpad(`
-0;
-0;
0;
`);
expect(transform(source)).toBe(expected);
});
it("should handle runtime errors", () => {
const source = unpad(`
try {
x({
toString: 0
} + '');
} catch (e) {}
`);
expect(transform(source)).toBe(source);
});
xit("should handle script escape", () => {
const source = unpad(`
"</" + "script"
`);
const expected = unpad(`
"<\\\\/script";
`);
expect(transform(source)).toBe(expected);
});
xit("should handle style escape", () => {
const source = unpad(`
"</" + "style"
`);
const expected = unpad(`
"<\\\\/style";
`);
expect(transform(source)).toBe(expected);
});
xit("should handle html comment escape", () => {
const source = unpad(`
"<!" + "--"
`);
const expected = unpad(`
"\\\\x3C!--";
`);
expect(transform(source)).toBe(expected);
});
it("should fix #440", () => {
const source = unpad(`
var x = "'cool'" + "test";
`);
const expected = unpad(`
var x = "'cool'test";
`);
expect(transform(source)).toBe(expected);
});
});