From db623f63ee7d1cf47e220fab5f0d6b5b37bad950 Mon Sep 17 00:00:00 2001 From: Systemcluster Date: Thu, 8 Aug 2024 06:23:01 +0200 Subject: [PATCH] Deduplicate snapshot test setup --- crates/cli-support/tests/common/mod.rs | 69 ++++++++ ...d__bundler_js.snap => bundler_js@add.snap} | 0 ...ler_js.snap => bundler_js@native_add.snap} | 0 ...est_add__deno_js.snap => deno_js@add.snap} | 0 ...__deno_js.snap => deno_js@native_add.snap} | 0 ...add__nodejs_js.snap => nodejs_js@add.snap} | 0 ...dejs_js.snap => nodejs_js@native_add.snap} | 0 ...dule_js.snap => nodejs_module_js@add.snap} | 0 ....snap => nodejs_module_js@native_add.snap} | 0 ...tart.snap => nodejs_module_start@add.snap} | 0 ...ap => nodejs_module_start@native_add.snap} | 0 ..._nomodule_js.snap => nomodule_js@add.snap} | 0 ...{test_add__web_js.snap => web_js@add.snap} | 0 ...dd__web_js.snap => web_js@native_add.snap} | 0 crates/cli-support/tests/test_add.rs | 161 +++++------------- crates/cli-support/tests/test_native_add.rs | 159 +++++------------ 16 files changed, 147 insertions(+), 242 deletions(-) create mode 100644 crates/cli-support/tests/common/mod.rs rename crates/cli-support/tests/snapshots/{test_add__bundler_js.snap => bundler_js@add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_native_add__bundler_js.snap => bundler_js@native_add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_add__deno_js.snap => deno_js@add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_native_add__deno_js.snap => deno_js@native_add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_add__nodejs_js.snap => nodejs_js@add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_native_add__nodejs_js.snap => nodejs_js@native_add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_add__nodejs_module_js.snap => nodejs_module_js@add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_native_add__nodejs_module_js.snap => nodejs_module_js@native_add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_add__nodejs_module_start.snap => nodejs_module_start@add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_native_add__nodejs_module_start.snap => nodejs_module_start@native_add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_add__nomodule_js.snap => nomodule_js@add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_add__web_js.snap => web_js@add.snap} (100%) rename crates/cli-support/tests/snapshots/{test_native_add__web_js.snap => web_js@native_add.snap} (100%) diff --git a/crates/cli-support/tests/common/mod.rs b/crates/cli-support/tests/common/mod.rs new file mode 100644 index 000000000000..b0025c64624b --- /dev/null +++ b/crates/cli-support/tests/common/mod.rs @@ -0,0 +1,69 @@ +#[macro_export] +#[allow(unused)] +macro_rules! test_output_snapshot { + ($name:ident, $start_output:literal, $init:expr) => { + #[test] + fn $name() { + use std::path::PathBuf; + use wasm_bindgen_cli_support::Bindgen; + + let mut bindgen = Bindgen::new(); + $init(&mut bindgen); + bindgen.input_path(WASM.concat()); + + let output = bindgen.generate_output().unwrap(); + let mut settings = insta::Settings::clone_current(); + settings.set_snapshot_suffix( + PathBuf::from(WASM[1]) + .file_stem() + .unwrap() + .to_str() + .unwrap(), + ); + settings.set_prepend_module_to_snapshot(false); + if $start_output { + settings.bind(|| insta::assert_snapshot!(output.start().unwrap())); + } else { + settings.bind(|| insta::assert_snapshot!(output.js())); + } + } + }; +} +#[macro_export] +#[allow(unused)] +macro_rules! test_output_empty { + ($name:ident, $start_output:literal, $init:expr) => { + #[test] + fn $name() { + use wasm_bindgen_cli_support::Bindgen; + + let mut bindgen = Bindgen::new(); + $init(&mut bindgen); + bindgen.input_path(WASM.concat()); + + let output = bindgen.generate_output().unwrap(); + if $start_output { + assert!(output.start().is_none()); + } else { + assert!(output.js().is_empty()); + } + } + }; +} +#[macro_export] +#[allow(unused)] +macro_rules! test_output_error { + ($name:ident, $init:expr) => { + #[test] + fn $name() { + use wasm_bindgen_cli_support::Bindgen; + + let mut bindgen = Bindgen::new(); + $init(&mut bindgen); + bindgen.input_path(WASM.concat()); + + let output = bindgen.generate_output(); + assert!(output.is_err()); + } + }; +} diff --git a/crates/cli-support/tests/snapshots/test_add__bundler_js.snap b/crates/cli-support/tests/snapshots/bundler_js@add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_add__bundler_js.snap rename to crates/cli-support/tests/snapshots/bundler_js@add.snap diff --git a/crates/cli-support/tests/snapshots/test_native_add__bundler_js.snap b/crates/cli-support/tests/snapshots/bundler_js@native_add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_native_add__bundler_js.snap rename to crates/cli-support/tests/snapshots/bundler_js@native_add.snap diff --git a/crates/cli-support/tests/snapshots/test_add__deno_js.snap b/crates/cli-support/tests/snapshots/deno_js@add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_add__deno_js.snap rename to crates/cli-support/tests/snapshots/deno_js@add.snap diff --git a/crates/cli-support/tests/snapshots/test_native_add__deno_js.snap b/crates/cli-support/tests/snapshots/deno_js@native_add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_native_add__deno_js.snap rename to crates/cli-support/tests/snapshots/deno_js@native_add.snap diff --git a/crates/cli-support/tests/snapshots/test_add__nodejs_js.snap b/crates/cli-support/tests/snapshots/nodejs_js@add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_add__nodejs_js.snap rename to crates/cli-support/tests/snapshots/nodejs_js@add.snap diff --git a/crates/cli-support/tests/snapshots/test_native_add__nodejs_js.snap b/crates/cli-support/tests/snapshots/nodejs_js@native_add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_native_add__nodejs_js.snap rename to crates/cli-support/tests/snapshots/nodejs_js@native_add.snap diff --git a/crates/cli-support/tests/snapshots/test_add__nodejs_module_js.snap b/crates/cli-support/tests/snapshots/nodejs_module_js@add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_add__nodejs_module_js.snap rename to crates/cli-support/tests/snapshots/nodejs_module_js@add.snap diff --git a/crates/cli-support/tests/snapshots/test_native_add__nodejs_module_js.snap b/crates/cli-support/tests/snapshots/nodejs_module_js@native_add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_native_add__nodejs_module_js.snap rename to crates/cli-support/tests/snapshots/nodejs_module_js@native_add.snap diff --git a/crates/cli-support/tests/snapshots/test_add__nodejs_module_start.snap b/crates/cli-support/tests/snapshots/nodejs_module_start@add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_add__nodejs_module_start.snap rename to crates/cli-support/tests/snapshots/nodejs_module_start@add.snap diff --git a/crates/cli-support/tests/snapshots/test_native_add__nodejs_module_start.snap b/crates/cli-support/tests/snapshots/nodejs_module_start@native_add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_native_add__nodejs_module_start.snap rename to crates/cli-support/tests/snapshots/nodejs_module_start@native_add.snap diff --git a/crates/cli-support/tests/snapshots/test_add__nomodule_js.snap b/crates/cli-support/tests/snapshots/nomodule_js@add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_add__nomodule_js.snap rename to crates/cli-support/tests/snapshots/nomodule_js@add.snap diff --git a/crates/cli-support/tests/snapshots/test_add__web_js.snap b/crates/cli-support/tests/snapshots/web_js@add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_add__web_js.snap rename to crates/cli-support/tests/snapshots/web_js@add.snap diff --git a/crates/cli-support/tests/snapshots/test_native_add__web_js.snap b/crates/cli-support/tests/snapshots/web_js@native_add.snap similarity index 100% rename from crates/cli-support/tests/snapshots/test_native_add__web_js.snap rename to crates/cli-support/tests/snapshots/web_js@native_add.snap diff --git a/crates/cli-support/tests/test_add.rs b/crates/cli-support/tests/test_add.rs index cd3a528c8cd3..32f60e933854 100644 --- a/crates/cli-support/tests/test_add.rs +++ b/crates/cli-support/tests/test_add.rs @@ -1,126 +1,43 @@ -use wasm_bindgen_cli_support::Bindgen; - -const ADD_WASM: &[&str] = &[ +const WASM: &[&str] = &[ std::env!("CARGO_MANIFEST_DIR"), "/../../examples/wasm-in-wasm/src/add.wasm", ]; -#[test] -fn test_bundler_start() { - let mut bindgen = Bindgen::new(); - bindgen.bundler(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_bundler_js() { - let mut bindgen = Bindgen::new(); - bindgen.bundler(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_deno_start() { - let mut bindgen = Bindgen::new(); - bindgen.deno(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_deno_js() { - let mut bindgen = Bindgen::new(); - bindgen.deno(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_nodejs_start() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_nodejs_js() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_nodejs_module_start() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs_module(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.start().unwrap()); -} - -#[test] -fn test_nodejs_module_js() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs_module(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_web_start() { - let mut bindgen = Bindgen::new(); - bindgen.web(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_web_js() { - let mut bindgen = Bindgen::new(); - bindgen.web(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_nomodule_start() { - let mut bindgen = Bindgen::new(); - bindgen.no_modules(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_nomodule_js() { - let mut bindgen = Bindgen::new(); - bindgen.no_modules(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} +mod common; + +test_output_snapshot!(test_bundler_js, false, |b: &mut Bindgen| { + b.bundler(true).unwrap(); +}); +test_output_empty!(test_bundler_start, true, |b: &mut Bindgen| { + b.bundler(true).unwrap(); +}); +test_output_snapshot!(test_deno_js, false, |b: &mut Bindgen| { + b.deno(true).unwrap(); +}); +test_output_empty!(test_deno_start, true, |b: &mut Bindgen| { + b.deno(true).unwrap(); +}); +test_output_snapshot!(test_nodejs_js, false, |b: &mut Bindgen| { + b.nodejs(true).unwrap(); +}); +test_output_empty!(test_nodejs_start, true, |b: &mut Bindgen| { + b.nodejs(true).unwrap(); +}); +test_output_snapshot!(test_nodejs_module_js, false, |b: &mut Bindgen| { + b.nodejs_module(true).unwrap(); +}); +test_output_snapshot!(test_nodejs_module_start, true, |b: &mut Bindgen| { + b.nodejs_module(true).unwrap(); +}); +test_output_snapshot!(test_web_js, false, |b: &mut Bindgen| { + b.web(true).unwrap(); +}); +test_output_empty!(test_web_start, true, |b: &mut Bindgen| { + b.web(true).unwrap(); +}); +test_output_snapshot!(test_nomodule_js, false, |b: &mut Bindgen| { + b.no_modules(true).unwrap(); +}); +test_output_empty!(test_nomodule_start, true, |b: &mut Bindgen| { + b.no_modules(true).unwrap(); +}); diff --git a/crates/cli-support/tests/test_native_add.rs b/crates/cli-support/tests/test_native_add.rs index 228ecced30dc..e4c31ca99e1a 100644 --- a/crates/cli-support/tests/test_native_add.rs +++ b/crates/cli-support/tests/test_native_add.rs @@ -1,126 +1,45 @@ -use wasm_bindgen_cli_support::Bindgen; - -const ADD_WASM: &[&str] = &[ +const WASM: &[&str] = &[ std::env!("CARGO_MANIFEST_DIR"), "/../../examples/wasm-in-wasm-imports/src/native_add.wasm", ]; -#[test] -fn test_bundler_start() { - let mut bindgen = Bindgen::new(); - bindgen.bundler(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_bundler_js() { - let mut bindgen = Bindgen::new(); - bindgen.bundler(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_deno_start() { - let mut bindgen = Bindgen::new(); - bindgen.deno(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_deno_js() { - let mut bindgen = Bindgen::new(); - bindgen.deno(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_nodejs_start() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_nodejs_js() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_nodejs_module_start() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs_module(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.start().unwrap()); -} - -#[test] -fn test_nodejs_module_js() { - let mut bindgen = Bindgen::new(); - bindgen.nodejs_module(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_web_start() { - let mut bindgen = Bindgen::new(); - bindgen.web(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - assert!(output.start().is_none()); -} - -#[test] -fn test_web_js() { - let mut bindgen = Bindgen::new(); - bindgen.web(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - - let output = bindgen.generate_output().unwrap(); - insta::assert_snapshot!(output.js()); -} - -#[test] -fn test_nomodule_start() { - let mut bindgen = Bindgen::new(); - bindgen.no_modules(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - +mod common; + +test_output_snapshot!(test_bundler_js, false, |b: &mut Bindgen| { + b.bundler(true).unwrap(); +}); +test_output_empty!(test_bundler_start, true, |b: &mut Bindgen| { + b.bundler(true).unwrap(); +}); +test_output_snapshot!(test_deno_js, false, |b: &mut Bindgen| { + b.deno(true).unwrap(); +}); +test_output_empty!(test_deno_start, true, |b: &mut Bindgen| { + b.deno(true).unwrap(); +}); +test_output_snapshot!(test_nodejs_js, false, |b: &mut Bindgen| { + b.nodejs(true).unwrap(); +}); +test_output_empty!(test_nodejs_start, true, |b: &mut Bindgen| { + b.nodejs(true).unwrap(); +}); +test_output_snapshot!(test_nodejs_module_js, false, |b: &mut Bindgen| { + b.nodejs_module(true).unwrap(); +}); +test_output_snapshot!(test_nodejs_module_start, true, |b: &mut Bindgen| { + b.nodejs_module(true).unwrap(); +}); +test_output_snapshot!(test_web_js, false, |b: &mut Bindgen| { + b.web(true).unwrap(); +}); +test_output_empty!(test_web_start, true, |b: &mut Bindgen| { + b.web(true).unwrap(); +}); +test_output_error!(test_nomodule_js, |b: &mut Bindgen| { // This test is expected to fail because the `no_modules` option cannot import - assert!(bindgen.generate_output().is_err()); -} - -#[test] -fn test_nomodule_js() { - let mut bindgen = Bindgen::new(); - bindgen.no_modules(true).unwrap(); - bindgen.input_path(ADD_WASM.concat()); - + b.no_modules(true).unwrap(); +}); +test_output_error!(test_nomodule_start, |b: &mut Bindgen| { // This test is expected to fail because the `no_modules` option cannot import - assert!(bindgen.generate_output().is_err()); -} + b.no_modules(true).unwrap(); +});