From 328947719749286d59740cfc6731a831fa827268 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 7 Aug 2024 06:29:41 +0000 Subject: [PATCH] refactor(minifier): clean up tests (#4724) --- .../tests/closure/fold_conditions.rs | 7 +- .../closure/reorder_constant_expression.rs | 7 +- .../closure/substitute_alternate_syntax.rs | 7 +- crates/oxc_minifier/tests/mod.rs | 47 +------- crates/oxc_minifier/tests/oxc/booleans.rs | 7 +- crates/oxc_minifier/tests/oxc/code_removal.rs | 19 ++-- crates/oxc_minifier/tests/oxc/folding.rs | 34 ++---- .../tests/oxc/remove_dead_code.rs | 25 +---- .../tests/snapshots/addition_folding.snap | 104 ------------------ 9 files changed, 48 insertions(+), 209 deletions(-) delete mode 100644 crates/oxc_minifier/tests/snapshots/addition_folding.snap diff --git a/crates/oxc_minifier/tests/closure/fold_conditions.rs b/crates/oxc_minifier/tests/closure/fold_conditions.rs index fb0b62cda2a27..262f0910331bb 100644 --- a/crates/oxc_minifier/tests/closure/fold_conditions.rs +++ b/crates/oxc_minifier/tests/closure/fold_conditions.rs @@ -1,4 +1,9 @@ -use crate::test; +use crate::CompressOptions; + +fn test(source_text: &str, expected: &str) { + let options = CompressOptions::all_true(); + crate::test(source_text, expected, options); +} // TODO: PeepholeMinimizeConditions.java #[test] diff --git a/crates/oxc_minifier/tests/closure/reorder_constant_expression.rs b/crates/oxc_minifier/tests/closure/reorder_constant_expression.rs index bfc9ee6001244..eb11a75f9414c 100644 --- a/crates/oxc_minifier/tests/closure/reorder_constant_expression.rs +++ b/crates/oxc_minifier/tests/closure/reorder_constant_expression.rs @@ -1,6 +1,11 @@ //! [PeepholeReorderConstantExpression](https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeReorderConstantExpressionTest.java) -use crate::test; +use crate::CompressOptions; + +fn test(source_text: &str, expected: &str) { + let options = CompressOptions::all_true(); + crate::test(source_text, expected, options); +} #[test] #[ignore] diff --git a/crates/oxc_minifier/tests/closure/substitute_alternate_syntax.rs b/crates/oxc_minifier/tests/closure/substitute_alternate_syntax.rs index 9bf7869ba22d9..e1a6637625078 100644 --- a/crates/oxc_minifier/tests/closure/substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/tests/closure/substitute_alternate_syntax.rs @@ -1,6 +1,11 @@ //! -use crate::test; +use crate::CompressOptions; + +fn test(source_text: &str, expected: &str) { + let options = CompressOptions::all_true(); + crate::test(source_text, expected, options); +} #[test] fn fold_return_result() { diff --git a/crates/oxc_minifier/tests/mod.rs b/crates/oxc_minifier/tests/mod.rs index f4fdf273ab130..81291f246a917 100644 --- a/crates/oxc_minifier/tests/mod.rs +++ b/crates/oxc_minifier/tests/mod.rs @@ -10,16 +10,11 @@ use oxc_minifier::{CompressOptions, Compressor}; use oxc_parser::Parser; use oxc_span::SourceType; -pub(crate) fn test(source_text: &str, expected: &str) { - let options = CompressOptions::all_true(); - test_with_options(source_text, expected, options); +pub(crate) fn test_same(source_text: &str, options: CompressOptions) { + test(source_text, source_text, options); } -pub(crate) fn test_same(source_text: &str) { - test(source_text, source_text); -} - -pub(crate) fn test_with_options(source_text: &str, expected: &str, options: CompressOptions) { +pub(crate) fn test(source_text: &str, expected: &str, options: CompressOptions) { let source_type = SourceType::default(); let result = run(source_text, source_type, Some(options)); let expected = run(expected, source_type, None); @@ -29,11 +24,7 @@ pub(crate) fn test_with_options(source_text: &str, expected: &str, options: Comp ); } -pub(crate) fn run( - source_text: &str, - source_type: SourceType, - options: Option, -) -> String { +fn run(source_text: &str, source_type: SourceType, options: Option) -> String { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); @@ -45,33 +36,3 @@ pub(crate) fn run( .build(program) .source_text } - -pub(crate) fn test_snapshot(name: &str, sources: S) -where - S: IntoIterator, -{ - let source_type = SourceType::default(); - let options = CompressOptions::all_true(); - let snapshot: String = sources - .into_iter() - .map(|source| { - let minified = run(source, source_type, Some(options)); - format!( - "==================================== SOURCE ==================================== -{source} - -=================================== MINIFIED =================================== -{minified} - -" - ) - }) - .fold(String::new(), |mut acc, snapshot| { - acc.push_str(snapshot.as_str()); - acc - }); - insta::with_settings!({ prepend_module_to_snapshot => false }, { - - insta::assert_snapshot!(name, snapshot); - }); -} diff --git a/crates/oxc_minifier/tests/oxc/booleans.rs b/crates/oxc_minifier/tests/oxc/booleans.rs index f5ddde9f5725f..8630c5c1ea0f4 100644 --- a/crates/oxc_minifier/tests/oxc/booleans.rs +++ b/crates/oxc_minifier/tests/oxc/booleans.rs @@ -1,4 +1,9 @@ -use crate::test_same; +use crate::CompressOptions; + +fn test_same(source_text: &str) { + let options = CompressOptions::all_true(); + crate::test_same(source_text, options); +} #[test] fn cjs() { diff --git a/crates/oxc_minifier/tests/oxc/code_removal.rs b/crates/oxc_minifier/tests/oxc/code_removal.rs index 7f8b4fded35a4..22e458ed98fec 100644 --- a/crates/oxc_minifier/tests/oxc/code_removal.rs +++ b/crates/oxc_minifier/tests/oxc/code_removal.rs @@ -1,4 +1,9 @@ -use crate::{test, test_with_options, CompressOptions}; +use crate::CompressOptions; + +fn test(source_text: &str, expected: &str) { + let options = CompressOptions::all_true(); + crate::test(source_text, expected, options); +} #[test] fn undefined_assignment() { @@ -21,16 +26,12 @@ fn undefined_return() { #[test] fn console_removal() { let options = CompressOptions { drop_console: true, ..CompressOptions::default() }; - test_with_options("console.log('hi')", "", options); - test_with_options("let x = console.error('oops')", "let x", options); - test_with_options( - "function f() { return console.warn('problem') }", - "function f(){return}", - options, - ); + crate::test("console.log('hi')", "", options); + crate::test("let x = console.error('oops')", "let x", options); + crate::test("function f() { return console.warn('problem') }", "function f(){return}", options); // console isn't removed when drop_console is `false`. This is also the // default value. let options = CompressOptions::default(); - test_with_options("console.log('hi')", "console.log('hi')", options); + crate::test("console.log('hi')", "console.log('hi')", options); } diff --git a/crates/oxc_minifier/tests/oxc/folding.rs b/crates/oxc_minifier/tests/oxc/folding.rs index a68e3be0811e0..d04cfa48c20c9 100644 --- a/crates/oxc_minifier/tests/oxc/folding.rs +++ b/crates/oxc_minifier/tests/oxc/folding.rs @@ -1,6 +1,10 @@ use oxc_minifier::CompressOptions; -use crate::{test, test_snapshot, test_with_options}; +fn test(source_text: &str, expected: &str) { + let options = + CompressOptions { remove_syntax: true, fold_constants: true, ..CompressOptions::default() }; + crate::test(source_text, expected, options); +} #[test] fn addition_folding() { @@ -16,35 +20,11 @@ fn typeof_folding() { test("'undefined' === typeof x", "typeof x>'u'"); } -#[test] -fn addition_folding_snapshots() { - test_snapshot( - "addition_folding", - [ - "let x = 1 + 1", - "function foo() { return 1 + 1; }", - "'' + true", - "'' + false", - "'' + null", - "false + null", - "'1' + '1'", - "NaN + NaN", - "'' + NaN", - // identifiers - "let x = 1; let y = x + 1", - "var x = 1; x + 1 === 2", - "var y = 1; 1 + y === 2", - "null - Number(1)", - "1 + 1.0000001", - ], - ); -} - #[test] fn test_join_vars() { let options = CompressOptions { join_vars: false, ..CompressOptions::default() }; - test_with_options("var foo = 1; var bar = 2", "var foo=1;var bar=2", options); + crate::test("var foo = 1; var bar = 2", "var foo=1;var bar=2", options); // join_vars: true let options = CompressOptions::default(); - test_with_options("var foo = 1; var bar = 2", "var foo=1,bar=2", options); + crate::test("var foo = 1; var bar = 2", "var foo=1,bar=2", options); } diff --git a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs index a847a7200a9af..0c094450cf0fd 100644 --- a/crates/oxc_minifier/tests/oxc/remove_dead_code.rs +++ b/crates/oxc_minifier/tests/oxc/remove_dead_code.rs @@ -1,27 +1,8 @@ -use oxc_allocator::Allocator; -use oxc_codegen::{CodeGenerator, CodegenOptions}; -use oxc_minifier::{CompressOptions, Compressor}; -use oxc_parser::Parser; -use oxc_span::SourceType; - -fn print(source_text: &str, remove_dead_code: bool) -> String { - let source_type = SourceType::default(); - let allocator = Allocator::default(); - let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); - if remove_dead_code { - Compressor::new(&allocator, CompressOptions::dead_code_elimination()).build(program); - } - CodeGenerator::new() - .with_options(CodegenOptions { single_quote: true }) - .build(program) - .source_text -} +use oxc_minifier::CompressOptions; fn test(source_text: &str, expected: &str) { - let minified = print(source_text, true); - let expected = print(expected, false); - assert_eq!(minified, expected, "for source {source_text}"); + let options = CompressOptions::dead_code_elimination(); + crate::test(source_text, expected, options); } #[test] diff --git a/crates/oxc_minifier/tests/snapshots/addition_folding.snap b/crates/oxc_minifier/tests/snapshots/addition_folding.snap deleted file mode 100644 index 0197ac1d3ea41..0000000000000 --- a/crates/oxc_minifier/tests/snapshots/addition_folding.snap +++ /dev/null @@ -1,104 +0,0 @@ ---- -source: crates/oxc_minifier/tests/mod.rs -expression: snapshot ---- -==================================== SOURCE ==================================== -let x = 1 + 1 - -=================================== MINIFIED =================================== -let x = 2; - - -==================================== SOURCE ==================================== -function foo() { return 1 + 1; } - -=================================== MINIFIED =================================== -function foo() { - return 2; -} - - -==================================== SOURCE ==================================== -'' + true - -=================================== MINIFIED =================================== -'true'; - - -==================================== SOURCE ==================================== -'' + false - -=================================== MINIFIED =================================== -'false'; - - -==================================== SOURCE ==================================== -'' + null - -=================================== MINIFIED =================================== -'null'; - - -==================================== SOURCE ==================================== -false + null - -=================================== MINIFIED =================================== -!1 + null; - - -==================================== SOURCE ==================================== -'1' + '1' - -=================================== MINIFIED =================================== -'11'; - - -==================================== SOURCE ==================================== -NaN + NaN - -=================================== MINIFIED =================================== -NaN + NaN; - - -==================================== SOURCE ==================================== -'' + NaN - -=================================== MINIFIED =================================== -'NaN'; - - -==================================== SOURCE ==================================== -let x = 1; let y = x + 1 - -=================================== MINIFIED =================================== -let x = 1, y = x + 1; - - -==================================== SOURCE ==================================== -var x = 1; x + 1 === 2 - -=================================== MINIFIED =================================== -var x = 1; -x + 1 === 2; - - -==================================== SOURCE ==================================== -var y = 1; 1 + y === 2 - -=================================== MINIFIED =================================== -var y = 1; -1 + y === 2; - - -==================================== SOURCE ==================================== -null - Number(1) - -=================================== MINIFIED =================================== -null - Number(1); - - -==================================== SOURCE ==================================== -1 + 1.0000001 - -=================================== MINIFIED =================================== -2.0000001000000003;