Skip to content

Commit

Permalink
refactor(minifier): clean up tests (#4724)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Aug 7, 2024
1 parent 94d3c31 commit 3289477
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 209 deletions.
7 changes: 6 additions & 1 deletion crates/oxc_minifier/tests/closure/fold_conditions.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
47 changes: 4 additions & 43 deletions crates/oxc_minifier/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
});
}
7 changes: 6 additions & 1 deletion crates/oxc_minifier/tests/oxc/booleans.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
19 changes: 10 additions & 9 deletions crates/oxc_minifier/tests/oxc/code_removal.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
34 changes: 7 additions & 27 deletions crates/oxc_minifier/tests/oxc/folding.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
25 changes: 3 additions & 22 deletions crates/oxc_minifier/tests/oxc/remove_dead_code.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
104 changes: 0 additions & 104 deletions crates/oxc_minifier/tests/snapshots/addition_folding.snap

This file was deleted.

0 comments on commit 3289477

Please sign in to comment.