Skip to content

Commit

Permalink
Deduplicate snapshot test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Systemcluster committed Aug 8, 2024
1 parent 4b8f6a4 commit db623f6
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 242 deletions.
69 changes: 69 additions & 0 deletions crates/cli-support/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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());
}
};
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
161 changes: 39 additions & 122 deletions crates/cli-support/tests/test_add.rs
Original file line number Diff line number Diff line change
@@ -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();
});
Loading

0 comments on commit db623f6

Please sign in to comment.