Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize between -sys and -js tests #3164

Merged
merged 17 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 88 additions & 34 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ winapi = "0.3"
wat = "1.0"
tempfile = "3.1"
anyhow = "1.0"
macro-wasmer-universal-test = { version = "0.1.0", path = "./macro-wasmer-universal-test" }

# Dependencies and Develoment Dependencies for `js`.
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand All @@ -72,6 +73,7 @@ hashbrown = { version = "0.11", optional = true }
wat = "1.0"
anyhow = "1.0"
wasm-bindgen-test = "0.3.0"
macro-wasmer-universal-test = { version = "0.1.0", path = "./macro-wasmer-universal-test" }

# Specific to `js`.
#
Expand Down
15 changes: 15 additions & 0 deletions lib/api/macro-wasmer-universal-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "macro-wasmer-universal-test"
version = "0.1.0"
edition = "2021"
license = "MIT"
description = "Universal test macro for wasmer-test"

[lib]
proc-macro = true

[dependencies]
proc-quote = "0.4.0"
proc-macro2 = "1.0.43"
syn = { version = "1.0.99", features = ["full"] }
quote = "*"
36 changes: 36 additions & 0 deletions lib/api/macro-wasmer-universal-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};

#[proc_macro_attribute]
pub fn universal_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item_clone = item.clone();
let mut iter = item_clone.into_iter();
let _ = iter.next().unwrap(); // fn
let item_tree: proc_macro::TokenTree = iter.next().unwrap(); // fn ...
let n = match &item_tree {
proc_macro::TokenTree::Ident(i) => i.to_string(),
_ => panic!("expected fn ...() -> Result<(), String>"),
};

let function_name_normal = Ident::new(&n, Span::call_site());
let function_name_js = Ident::new(&format!("{}_js", n), Span::call_site());
let parsed = match syn::parse::<syn::ItemFn>(item) {
Ok(o) => o,
Err(e) => {
return proc_macro::TokenStream::from(e.to_compile_error());
}
};

let tokens = quote::quote! {
#[cfg(feature = "js")]
#[cfg_attr(feature = "js", wasm_bindgen_test)]
fn #function_name_js() { #function_name_normal().unwrap(); }

#[cfg_attr(feature = "sys", test)]
#parsed
};

proc_macro::TokenStream::from(tokens)
}
Loading