Skip to content

Commit

Permalink
fix(es): Fix plugin template & restore test! as test_inline! (#8508)
Browse files Browse the repository at this point in the history
**Description:**

I made a bad decision while migrating tests in the core repository to snapshot testing. So, I'm adding the `test!` macro back with a slight renaming.

**Related issue:**

 - Closes #8504
  • Loading branch information
kdy1 authored Jan 16, 2024
1 parent 99d74f3 commit 10449e0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/swc_cli_impl/src/commands/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ build-wasm32 = "build --target wasm32-unknown-unknown"
src_path.join("lib.rs"),
r##"use swc_core::ecma::{
ast::Program,
transforms::testing::test,
transforms::testing::test_inline,
visit::{as_folder, FoldWith, VisitMut},
};
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
Expand Down Expand Up @@ -275,7 +275,7 @@ pub fn process_transform(program: Program, _metadata: TransformPluginProgramMeta
// Recommended strategy to test plugin's transform is verify
// the Visitor's behavior, instead of trying to run `process_transform` with mocks
// unless explicitly required to do so.
test!(
test_inline!(
Default::default(),
|_| as_folder(TransformVisitor),
boo,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ base64 = "0.21"
hex = "0.4.3"
serde = "1"
serde_json = "1"
sha2 = "0.10"
sha2 = "0.10.8"
sourcemap = "6.2"
tempfile = "3.6.0"

Expand Down
120 changes: 109 additions & 11 deletions crates/swc_ecma_transforms_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,77 @@ pub fn test_transform<F, P>(
/// NOT A PUBLIC API. DO NOT USE.
#[doc(hidden)]
#[track_caller]
pub fn test_inlined_transform<F, P>(
test_name: &str,
syntax: Syntax,
tr: F,
input: &str,
_always_ok_if_code_eq: bool,
) where
pub fn test_inline_input_output<F, P>(syntax: Syntax, tr: F, input: &str, output: &str)
where
F: FnOnce(&mut Tester) -> P,
P: Fold,
{
let _logger = testing::init();

let expected = read_to_string(output);
let _is_really_expected = expected.is_ok();
let expected = expected.unwrap_or_default();

let expected_src = Tester::run(|tester| {
let expected_module = tester.apply_transform(noop(), "expected.js", syntax, &expected)?;

let expected_src = tester.print(&expected_module, &Default::default());

println!(
"----- {} -----\n{}",
Color::Green.paint("Expected"),
expected_src
);

Ok(expected_src)
});

let actual_src = Tester::run_captured(|tester| {
println!("----- {} -----\n{}", Color::Green.paint("Input"), input);

let tr = tr(tester);

println!("----- {} -----", Color::Green.paint("Actual"));

let actual = tester.apply_transform(tr, "input.js", syntax, input)?;

match ::std::env::var("PRINT_HYGIENE") {
Ok(ref s) if s == "1" => {
let hygiene_src = tester.print(
&actual.clone().fold_with(&mut HygieneVisualizer),
&Default::default(),
);
println!(
"----- {} -----\n{}",
Color::Green.paint("Hygiene"),
hygiene_src
);
}
_ => {}
}

let actual = actual
.fold_with(&mut crate::hygiene::hygiene())
.fold_with(&mut crate::fixer::fixer(Some(&tester.comments)));

let actual_src = tester.print(&actual, &Default::default());

Ok(actual_src)
})
.1
.to_string();

assert_eq!(
expected_src, actual_src,
"Exepcted:\n{expected_src}\nActual:\n{actual_src}\n",
);
}

/// NOT A PUBLIC API. DO NOT USE.
#[doc(hidden)]
#[track_caller]
pub fn test_inlined_transform<F, P>(test_name: &str, syntax: Syntax, tr: F, input: &str)
where
F: FnOnce(&mut Tester) -> P,
P: Fold,
{
Expand Down Expand Up @@ -394,28 +458,62 @@ macro_rules! test_location {
}};
}

/// Test transformation.
#[macro_export]
macro_rules! test_inline {
(ignore, $syntax:expr, $tr:expr, $test_name:ident, $input:expr, $output:expr) => {
#[test]
#[ignore]
fn $test_name() {
$crate::test_inline_input_output($syntax, $tr, $input, $output)
}
};

($syntax:expr, $tr:expr, $test_name:ident, $input:expr, $output:expr) => {
#[test]
fn $test_name() {
$crate::test_inline_input_output($syntax, $tr, $input, $output)
}
};
}

test_inline!(
ignore,
Syntax::default(),
|_| noop(),
noop_ignored,
"class Foo {}",
"class Foo {}"
);

test_inline!(
Syntax::default(),
|_| noop(),
noop_test,
"class Foo {}",
"class Foo {}"
);

#[macro_export]
macro_rules! test {
(ignore, $syntax:expr, $tr:expr, $test_name:ident, $input:expr) => {
#[test]
#[ignore]
fn $test_name() {
$crate::test_inlined_transform(stringify!($test_name), $syntax, $tr, $input, false)
$crate::test_inlined_transform(stringify!($test_name), $syntax, $tr, $input)
}
};

($syntax:expr, $tr:expr, $test_name:ident, $input:expr) => {
#[test]
fn $test_name() {
$crate::test_inlined_transform(stringify!($test_name), $syntax, $tr, $input, false)
$crate::test_inlined_transform(stringify!($test_name), $syntax, $tr, $input)
}
};

($syntax:expr, $tr:expr, $test_name:ident, $input:expr, ok_if_code_eq) => {
#[test]
fn $test_name() {
$crate::test_inlined_transform(stringify!($test_name), $syntax, $tr, $input, true)
$crate::test_inlined_transform(stringify!($test_name), $syntax, $tr, $input)
}
};
}
Expand Down

0 comments on commit 10449e0

Please sign in to comment.