refactor(minifier): clean up some tests (#5910)

This commit is contained in:
Boshen 2024-09-20 05:55:05 +00:00
parent 144611ef49
commit cbaeea6d02
6 changed files with 1081 additions and 156 deletions

View file

@ -1,28 +0,0 @@
use oxc_minifier::CompressOptions;
fn test(source_text: &str, expected: &str) {
let options = CompressOptions::all_true();
crate::test(source_text, expected, options);
}
// TODO: PeepholeMinimizeConditions.java
#[test]
#[ignore]
fn test_fold_not() {
test("while(!(x==y)){a=b;}", "for(;x!=y;)a=b");
test("while(!(x!=y)){a=b;}", "for(;x==y;)a=b");
test("while(!(x===y)){a=b;}", "for(;x!==y;)a=b");
test("while(!(x!==y)){a=b;}", "for(;x===y;)a=b");
// Because !(x<NaN) != x>=NaN don't fold < and > cases.
test("while(!(x>y)){a=b;}", "for(;!(x>y);)a=b");
test("while(!(x>=y)){a=b;}", "for(;!(x>=y);)a=b");
test("while(!(x<y)){a=b;}", "for(;!(x<y);)a=b");
test("while(!(x<=y)){a=b;}", "for(;!(x<=y);)a=b");
test("while(!(x<=NaN)){a=b;}", "for(;!(x<=NaN);)a=b");
// NOT forces a boolean context
// test("x = !(y() && true)", "x=!y()");
// This will be further optimized by PeepholeFoldConstants.
// test("x = !true", "x=!1");
}

View file

@ -0,0 +1,72 @@
//! <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/MinimizedConditionTest.java>
use oxc_minifier::CompressOptions;
// TODO: handle negative cases
fn test(source_text: &str, positive: &str, _negative: &str) {
let options = CompressOptions {
remove_syntax: true,
minimize_conditions: true,
..CompressOptions::all_false()
};
crate::test(source_text, positive, options);
}
#[test]
#[ignore]
fn try_minimize_cond_simple() {
test("x", "x", "x");
test("!x", "!x", "!x");
test("!!x", "x", "x");
test("!(x && y)", "!x || !y", "!(x && y)");
}
#[test]
#[ignore]
fn minimize_demorgan_simple() {
test("!(x&&y)", "!x||!y", "!(x&&y)");
test("!(x||y)", "!x&&!y", "!(x||y)");
test("!x||!y", "!x||!y", "!(x&&y)");
test("!x&&!y", "!x&&!y", "!(x||y)");
test("!(x && y && z)", "!(x && y && z)", "!(x && y && z)");
test("(!a||!b)&&c", "(!a||!b)&&c", "!(a&&b||!c)");
test("(!a||!b)&&(c||d)", "!(a&&b||!c&&!d)", "!(a&&b||!c&&!d)");
}
#[test]
#[ignore]
fn minimize_bug8494751() {
test(
"x && (y===2 || !f()) && (y===3 || !h())",
// TODO(tbreisacher): The 'positive' option could be better:
// "x && !((y!==2 && f()) || (y!==3 && h()))",
"!(!x || (y!==2 && f()) || (y!==3 && h()))",
"!(!x || (y!==2 && f()) || (y!==3 && h()))",
);
test(
"x && (y===2 || !f?.()) && (y===3 || !h?.())",
"!(!x || (y!==2 && f?.()) || (y!==3 && h?.()))",
"!(!x || (y!==2 && f?.()) || (y!==3 && h?.()))",
);
}
#[test]
#[ignore]
fn minimize_complementable_operator() {
test("0===c && (2===a || 1===a)", "0===c && (2===a || 1===a)", "!(0!==c || 2!==a && 1!==a)");
}
#[test]
#[ignore]
fn minimize_hook() {
test("!(x ? y : z)", "(x ? !y : !z)", "!(x ? y : z)");
}
#[test]
#[ignore]
fn minimize_comma() {
test("!(inc(), test())", "inc(), !test()", "!(inc(), test())");
test("!(inc?.(), test?.())", "inc?.(), !test?.()", "!(inc?.(), test?.())");
test("!((x,y)&&z)", "(x,!y)||!z", "!((x,y)&&z)");
}

View file

@ -1,11 +1,10 @@
mod collapse_variable_declarations;
mod dead_code_elimination;
mod fold_conditions;
mod minimized_condition;
mod peephole_fold_constants;
mod peephole_minimize_conditions;
mod peephole_substitute_alternate_syntax;
mod remove_syntax;
mod reorder_constant_expression;
// Oxc Integration Tests

View file

@ -15,8 +15,6 @@ fn test_same(source_text: &str) {
test(source_text, source_text);
}
// Google Closure Compiler
#[test]
fn undefined_comparison1() {
test("undefined == undefined", "true");

View file

@ -1,86 +0,0 @@
//! [PeepholeReorderConstantExpression](https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeReorderConstantExpressionTest.java)
use crate::CompressOptions;
fn test(source_text: &str, expected: &str) {
let options = CompressOptions::all_true();
crate::test(source_text, expected, options);
}
#[test]
#[ignore]
fn symmetric_operations() {
set1_tests_same("==");
set2_tests("==");
set3_tests("==");
set1_tests_same("!=");
set2_tests("!=");
set3_tests("!=");
set1_tests_same("===");
set2_tests("===");
set3_tests("===");
set1_tests_same("!==");
set2_tests("!==");
set3_tests("!==");
// TODO: need to check operator precedence
set1_tests_same("*");
set2_tests("*");
set3_tests("*");
}
#[test]
#[ignore]
fn reorder_constant_doesnt_add_parens() {
test("a % b * 4", "a%b*4");
test("a * b * 4", "a*b*4");
}
fn set1_tests_same(op: &str) {
set1_tests(op, op);
}
// This set has a mutable on the right and an Immutable on the left.
// Applies for relational and symmetric operations.
fn set1_tests(op1: &str, op2: &str) {
test(&format!("a {op1} 0"), &format!("0{op2}a"));
test(&format!("a {op1} '0'"), &format!("'0'{op1}a"));
test(&format!("a {op1} ''"), &format!("''{op2}a"));
test(&format!("a {op1} -1.0"), &format!("-1{op2}a"));
test(&format!("function f(a){{a {op1} 0}}"), &format!("function f(a){{0{op2}a}}"));
test(&format!("f() {op1} 0"), &format!("0{op2}f()"));
test(&format!("(a + b) {op1} 0"), &format!("0{op2}(a+b)"));
test(&format!("(a + 1) {op1} 0"), &format!("0{op2}(a+1)"));
test(&format!("x++ {op1} 0"), &format!("0{op2}x++"));
test(
&format!("x = 0; function f(){{x++; return x}}; f() {op1} 0"),
&format!("x=0;function f(){{x++;return x}}0{op2}f()"),
);
}
// This set has a mutable on the right and an Immutable on the left.
// Applies only for symmetric operations.
fn set2_tests(op: &str) {
test(&format!("a {op} NaN"), &format!("NaN{op}a"));
// expect(&format!("a {op} Infinity"), &format!("(1/0){op}a"));
test(&format!("NaN {op} a"), &format!("NaN{op}a"));
// expect(&format!("Infinity {op} a"), &format!("(1/0){op}a"));
}
// This set has an the immutable on the left already, or both non-immutable.
fn set3_tests(op: &str) {
test(&format!("0 {op} a"), &format!("0{op}a"));
test(&format!("'0' {op} a"), &format!("'0'{op}a"));
test(&format!("'' {op} a"), &format!("''{op}a"));
test(&format!("-1.0 {op} a"), &format!("-1{op}a"));
test(&format!("0 {op} 1"), &format!("0{op}1"));
test(&format!("a {op} b"), &format!("a{op}b"));
}