mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-30 17:01:06 +00:00
* Add a test harness to directly execute wasm tests This commits adds a few new crates and infrastructure to enable comands like: cargo test --target wasm32-unknown-unknown The intention here is to make it as low-friction as possible to write wasm tests and also have them execute in a reasonable amount of time. Eventually this is also hopefully enough support to do things like headless testing! For now though this is defintely MVP status rather than fully fleshed out. There's some more information at `crates/test/README.md` about how it works and how to use it, but for now this is mainly intended to play around with locally in this repository for our own tests. * Port a numbe of `js-sys` tests to the new test framework This commit ports a number of existing tests for the `js-sys` crate over to the new test framework created in the previous commit, showing off how they can be executed as well as drastictlly simplifying the tests themselves! This is intended to be a proof of concept for now which we can refine over time. This should also show off that it's possible to incrementally move over to the new test framework.
55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
//! See the README for `wasm-bindgen-test` for a bit more info about what's
|
|
//! going on here.
|
|
|
|
extern crate proc_macro;
|
|
extern crate proc_macro2;
|
|
#[macro_use]
|
|
extern crate quote;
|
|
|
|
use std::sync::atomic::*;
|
|
use proc_macro2::*;
|
|
|
|
static CNT: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
#[proc_macro_attribute]
|
|
pub fn wasm_bindgen_test(
|
|
attr: proc_macro::TokenStream,
|
|
body: proc_macro::TokenStream,
|
|
) -> proc_macro::TokenStream {
|
|
if attr.into_iter().next().is_some() {
|
|
panic!("this attribute currently takes no arguments");
|
|
}
|
|
|
|
let mut body = TokenStream::from(body).into_iter();
|
|
|
|
// Assume the input item is of the form `fn #ident ...`, and extract
|
|
// `#ident`
|
|
let fn_tok = body.next();
|
|
let ident = match body.next() {
|
|
Some(TokenTree::Ident(token)) => token,
|
|
_ => panic!("expected a function name"),
|
|
};
|
|
|
|
let mut tokens = Vec::<TokenTree>::new();
|
|
|
|
// We generate a `#[no_mangle]` with a known prefix so the test harness can
|
|
// later slurp up all of these functions and pass them as arguments to the
|
|
// main test harness. This is the entry point for all tests.
|
|
let name = format!("__wbg_test_{}_{}", ident, CNT.fetch_add(1, Ordering::SeqCst));
|
|
let name = Ident::new(&name, Span::call_site());
|
|
tokens.extend((quote! {
|
|
#[no_mangle]
|
|
pub extern fn #name(cx: *const ::wasm_bindgen_test::__rt::Context) {
|
|
unsafe {
|
|
(*cx).execute(concat!(module_path!(), "::", stringify!(#ident)), #ident);
|
|
}
|
|
}
|
|
}).into_iter());
|
|
|
|
tokens.extend(fn_tok);
|
|
tokens.push(ident.into());
|
|
tokens.extend(body);
|
|
|
|
tokens.into_iter().collect::<TokenStream>().into()
|
|
}
|