refactor(minifier): clean up tests (#4724)

This commit is contained in:
Boshen 2024-08-07 06:29:41 +00:00
parent 94d3c31933
commit 3289477197
9 changed files with 48 additions and 209 deletions

View file

@ -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]

View file

@ -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]

View file

@ -1,6 +1,11 @@
//! <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntaxTest.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]
fn fold_return_result() {

View file

@ -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<CompressOptions>,
) -> String {
fn run(source_text: &str, source_type: SourceType, options: Option<CompressOptions>) -> 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<S>(name: &str, sources: S)
where
S: IntoIterator<Item = &'static str>,
{
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);
});
}

View file

@ -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() {

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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]

View file

@ -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;