aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-10-29 16:53:48 +0800
committerGitHub <noreply@github.com>2019-10-29 16:53:48 +0800
commitbad664c6322538f1a5eec7f76dc44f81fa05c9e0 (patch)
treec8bb72ced239cd86cdb36f2efdf668f7df8a56df /test
parent8a191c0a8486bfd2a51562f0c072e00507abea6e (diff)
downloadtracifyjs-bad664c6322538f1a5eec7f76dc44f81fa05c9e0.tar.gz
tracifyjs-bad664c6322538f1a5eec7f76dc44f81fa05c9e0.zip
compress object literals (#3546)
Diffstat (limited to 'test')
-rw-r--r--test/compress/objects.js223
1 files changed, 223 insertions, 0 deletions
diff --git a/test/compress/objects.js b/test/compress/objects.js
new file mode 100644
index 00000000..fb1e5865
--- /dev/null
+++ b/test/compress/objects.js
@@ -0,0 +1,223 @@
+duplicate_key: {
+ options = {
+ objects: true,
+ side_effects: true,
+ }
+ input: {
+ var o = {
+ a: 1,
+ b: 2,
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ a: 3,
+ b: 2,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "a 3",
+ "b 2",
+ ]
+}
+
+duplicate_key_strict: {
+ options = {
+ objects: true,
+ side_effects: true,
+ }
+ input: {
+ "use strict";
+ var o = {
+ a: 1,
+ b: 2,
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ "use strict";
+ var o = {
+ a: 1,
+ b: 2,
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: true
+}
+
+duplicate_key_side_effect: {
+ options = {
+ objects: true,
+ side_effects: true,
+ }
+ input: {
+ var o = {
+ a: 1,
+ b: o = 2,
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ a: 1,
+ b: o = 2,
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "a 3",
+ "b 2",
+ ]
+}
+
+duplicate_key_with_accessor: {
+ options = {
+ objects: true,
+ side_effects: true,
+ }
+ input: {
+ [
+ {
+ a: 0,
+ b: 1,
+ a: 2,
+ set b(v) {},
+ },
+ {
+ a: 3,
+ b: 4,
+ get a() {
+ return 5;
+ },
+ a: 6,
+ b: 7,
+ a: 8,
+ b: 9,
+ },
+ ].forEach(function(o) {
+ for (var k in o)
+ console.log(k, o[k]);
+ });
+ }
+ expect: {
+ [
+ {
+ a: 2,
+ b: 1,
+ set b(v) {},
+ },
+ {
+ a: 3,
+ b: 4,
+ get a() {
+ return 5;
+ },
+ a: 8,
+ b: 9,
+ },
+ ].forEach(function(o) {
+ for (var k in o)
+ console.log(k, o[k]);
+ });
+ }
+ expect_stdout: true
+}
+
+unsafe_object_repeated: {
+ options = {
+ evaluate: true,
+ objects: true,
+ reduce_funcs: true,
+ reduce_vars: true,
+ side_effects: true,
+ toplevel: true,
+ unsafe: true,
+ }
+ input: {
+ var o = { a: { b: 1 }, a: 1 };
+ console.log(
+ o + 1,
+ o.a + 1,
+ o.b + 1,
+ o.a.b + 1
+ );
+ }
+ expect: {
+ var o = { a: 1 };
+ console.log(
+ o + 1,
+ 2,
+ o.b + 1,
+ NaN
+ );
+ }
+ expect_stdout: true
+}
+
+numeric_literal: {
+ options = {
+ objects: true,
+ side_effects: true,
+ }
+ mangle = {
+ properties: true,
+ }
+ beautify = {
+ beautify: true,
+ }
+ input: {
+ var obj = {
+ 0: 0,
+ "-0": 1,
+ 42: 2,
+ "42": 3,
+ 0x25: 4,
+ "0x25": 5,
+ 1E42: 6,
+ "1E42": 7,
+ "1e+42": 8,
+ };
+ console.log(obj[-0], obj[-""], obj["-0"]);
+ console.log(obj[42], obj["42"]);
+ console.log(obj[0x25], obj["0x25"], obj[37], obj["37"]);
+ console.log(obj[1E42], obj["1E42"], obj["1e+42"]);
+ }
+ expect_exact: [
+ 'var obj = {',
+ ' 0: 0,',
+ ' "-0": 1,',
+ ' 42: 3,',
+ ' 37: 4,',
+ ' o: 5,',
+ ' 1e42: 8,',
+ ' b: 7',
+ '};',
+ '',
+ 'console.log(obj[-0], obj[-""], obj["-0"]);',
+ '',
+ 'console.log(obj[42], obj["42"]);',
+ '',
+ 'console.log(obj[37], obj["o"], obj[37], obj["37"]);',
+ '',
+ 'console.log(obj[1e42], obj["b"], obj["1e+42"]);',
+ ]
+ expect_stdout: [
+ "0 0 1",
+ "3 3",
+ "4 5 4 4",
+ "8 7 8",
+ ]
+}