Skip to content

Commit

Permalink
test(minifier): add and enable some tests in fold_constants (#8769)
Browse files Browse the repository at this point in the history
Ported some tests that was missing from closure compiler.
Also enabled some tests.
  • Loading branch information
sapphi-red committed Jan 28, 2025
1 parent 8cf9d34 commit 79d5481
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions crates/oxc_minifier/src/peephole/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,19 @@ mod test {
fold_same("'' + x === !y");
}

#[test]
fn test_number_number_comparison() {
fold("1 > 1", "!1");
fold("2 == 3", "!1");
fold("3.6 === 3.6", "!0");
fold_same("+x > +y");
fold_same("+x == +y");
fold("+x === +y", "+x == +y");
fold_same("+x > +x"); // foldable to false
fold_same("+x == +x");
fold("+x === +x", "+x == +x");
}

#[test]
fn test_string_string_comparison() {
fold("'a' < 'b'", "!0");
Expand Down Expand Up @@ -1117,6 +1130,22 @@ mod test {
fold("NaN==foo()", "foo()==NaN");
}

#[test]
#[ignore]
fn test_object_comparison1() {
fold("!new Date()", "false");
fold("!!new Date()", "true");

fold("new Date() == null", "false");
fold("new Date() == undefined", "false");
fold("new Date() != null", "true");
fold("new Date() != undefined", "true");
fold("null == new Date()", "false");
fold("undefined == new Date()", "false");
fold("null != new Date()", "true");
fold("undefined != new Date()", "true");
}

#[test]
fn js_typeof() {
fold("x = typeof 1", "x = \"number\"");
Expand Down Expand Up @@ -1162,7 +1191,7 @@ mod test {
fold("a=+false", "a=0");
fold_same("a=+foo()");
fold_same("a=+f");
// test("a=+(f?true:false)", "a=+(f?1:0)");
fold("a=+(f?true:false)", "a=+!!f");
fold("a=+0", "a=0");
fold("a=+Infinity", "a=Infinity");
fold("a=+NaN", "a=NaN");
Expand All @@ -1175,7 +1204,7 @@ mod test {
fold_same("a=~~foo()");
fold("a=~0xffffffff", "a=0");
fold("a=~~0xffffffff", "a=-1");
// test_same("a=~.5");
fold("a=~.5", "a=-1");
}

#[test]
Expand Down Expand Up @@ -1381,6 +1410,8 @@ mod test {
// these cases look strange because bitwise OR converts unsigned numbers to be signed
fold("x = 1 | 3000000001", "x = -1294967295");
fold("x = 4294967295 | 0", "x = -1");

fold("x = -1 | 0", "x = -1");
}

#[test]
Expand Down Expand Up @@ -1482,39 +1513,45 @@ mod test {
fold("8589934591 >>> 0", "4294967295");
fold("8589934592 >>> 0", "0");
fold("8589934593 >>> 0", "1");

fold("x = -1 << 1", "x = -2");
fold("x = -1 << 8", "x = -256");
fold("x = -1 >> 1", "x = -1");
fold("x = -2 >> 1", "x = -1");
fold("x = -1 >> 0", "x = -1");
}

#[test]
fn test_string_add() {
fold("x = 'a' + 'bc'", "x = 'abc'");
fold("x = 'a' + 5", "x = 'a5'");
fold("x = 5 + 'a'", "x = '5a'");
// test("x = 'a' + 5n", "x = 'a5n'");
// test("x = 5n + 'a'", "x = '5na'");
fold("x = 'a' + 5n", "x = 'a5'");
fold("x = 5n + 'a'", "x = '5a'");
fold("x = 'a' + ''", "x = 'a'");
fold("x = 'a' + foo()", "x = 'a'+foo()");
fold("x = foo() + 'a' + 'b'", "x = foo()+'ab'");
fold("x = (foo() + 'a') + 'b'", "x = foo()+'ab'"); // believe it!
fold("x = foo() + 'a' + 'b' + 'cd' + bar()", "x = foo()+'abcd'+bar()");
fold("x = foo() + 2 + 'b'", "x = foo()+2+\"b\""); // don't fold!

// test("x = foo() + 'a' + 2", "x = foo()+\"a2\"");
// fold("x = foo() + 'a' + 2", "x = foo()+\"a2\"");
fold("x = '' + null", "x = 'null'");
fold("x = true + '' + false", "x = 'truefalse'");
// test("x = '' + []", "x = ''");
// test("x = foo() + 'a' + 1 + 1", "x = foo() + 'a11'");
// fold("x = '' + []", "x = ''");
// fold("x = foo() + 'a' + 1 + 1", "x = foo() + 'a11'");
fold("x = 1 + 1 + 'a'", "x = '2a'");
fold("x = 1 + 1 + 'a'", "x = '2a'");
fold("x = 'a' + (1 + 1)", "x = 'a2'");
// test("x = '_' + p1 + '_' + ('' + p2)", "x = '_' + p1 + '_' + p2");
// test("x = 'a' + ('_' + 1 + 1)", "x = 'a_11'");
// test("x = 'a' + ('_' + 1) + 1", "x = 'a_11'");
// test("x = 1 + (p1 + '_') + ('' + p2)", "x = 1 + (p1 + '_') + p2");
// test("x = 1 + p1 + '_' + ('' + p2)", "x = 1 + p1 + '_' + p2");
// test("x = 1 + 'a' + p1", "x = '1a' + p1");
// test("x = (p1 + (p2 + 'a')) + 'b'", "x = (p1 + (p2 + 'ab'))");
// test("'a' + ('b' + p1) + 1", "'ab' + p1 + 1");
// test("x = 'a' + ('b' + p1 + 'c')", "x = 'ab' + (p1 + 'c')");
// fold("x = '_' + p1 + '_' + ('' + p2)", "x = '_' + p1 + '_' + p2");
// fold("x = 'a' + ('_' + 1 + 1)", "x = 'a_11'");
// fold("x = 'a' + ('_' + 1) + 1", "x = 'a_11'");
// fold("x = 1 + (p1 + '_') + ('' + p2)", "x = 1 + (p1 + '_') + p2");
// fold("x = 1 + p1 + '_' + ('' + p2)", "x = 1 + p1 + '_' + p2");
// fold("x = 1 + 'a' + p1", "x = '1a' + p1");
// fold("x = (p1 + (p2 + 'a')) + 'b'", "x = (p1 + (p2 + 'ab'))");
// fold("'a' + ('b' + p1) + 1", "'ab' + p1 + 1");
// fold("x = 'a' + ('b' + p1 + 'c')", "x = 'ab' + (p1 + 'c')");
fold("void 0 + ''", "'undefined'");

fold_same("x = 'a' + (4 + p1 + 'a')");
Expand Down Expand Up @@ -1556,6 +1593,11 @@ mod test {
fold("x = null + 1", "x = 1");
}

#[test]
fn test_fold_sub() {
fold("x = 10 - 20", "x = -10");
}

#[test]
fn test_fold_multiply() {
fold_same("x = 2.25 * 3");
Expand Down

0 comments on commit 79d5481

Please sign in to comment.