Skip to content

Commit

Permalink
Merge pull request #3164 from wasmerio/sync-js-sys-tests
Browse files Browse the repository at this point in the history
Synchronize between -sys and -js tests
  • Loading branch information
syrusakbary authored Sep 9, 2022
2 parents 6b40f4c + a2ce2de commit e6857d1
Show file tree
Hide file tree
Showing 14 changed files with 995 additions and 2,572 deletions.
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

0 comments on commit e6857d1

Please sign in to comment.