Skip to content

Commit

Permalink
Add attribute crate to proc-macro wasm_bindgen_test
Browse files Browse the repository at this point in the history
This attribute allow to specify a path where `wasm-bindgen-test` can be
accessed.

Closes #3588
  • Loading branch information
FirelightFlagboy committed Sep 4, 2023
1 parent cbb89a0 commit 4ae9cdd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
16 changes: 14 additions & 2 deletions crates/test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ pub fn wasm_bindgen_test(
// 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_ident!("__wbgt_{}_{}", ident, CNT.fetch_add(1, Ordering::SeqCst));
let wasm_bindgen_path = attributes.wasm_bindgen_path;
tokens.extend(
quote! {
#[no_mangle]
pub extern "C" fn #name(cx: &::wasm_bindgen_test::__rt::Context) {
pub extern "C" fn #name(cx: &#wasm_bindgen_path::__rt::Context) {
let test_name = ::core::concat!(::core::module_path!(), "::", ::core::stringify!(#ident));
#test_body
}
Expand Down Expand Up @@ -187,15 +188,26 @@ fn compile_error(span: Span, msg: &str) -> proc_macro::TokenStream {
quote_spanned! { span => compile_error!(#msg); }.into()
}

#[derive(Default)]
struct Attributes {
r#async: bool,
wasm_bindgen_path: syn::Path,
}

impl Default for Attributes {
fn default() -> Self {
Self {
r#async: false,
wasm_bindgen_path: syn::parse_quote!(::wasm_bindgen_test),
}
}
}

impl Attributes {
fn parse(&mut self, meta: syn::meta::ParseNestedMeta) -> syn::parse::Result<()> {
if meta.path.is_ident("async") {
self.r#async = true;
} else if meta.path.is_ident("crate") {
self.wasm_bindgen_path = meta.value()?.parse::<syn::Path>()?;
} else {
return Err(meta.error("unknown attribute"));
}
Expand Down
20 changes: 20 additions & 0 deletions crates/test-macro/ui-tests/crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![no_implicit_prelude]

extern crate wasm_bindgen_test_macro;
//
use wasm_bindgen_test_macro::wasm_bindgen_test;

pub mod wasm {
pub extern crate wasm_bindgen_test as test;
}

#[wasm_bindgen_test(crate = ::wasm_bindgen_test)]
fn success_1() {}

#[wasm_bindgen_test(crate = crate::wasm::test)]
fn success_2() {}

#[wasm_bindgen_test(crate = foo)]
fn failure_1() {}

fn main() {}
5 changes: 5 additions & 0 deletions crates/test-macro/ui-tests/crate.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
--> ui-tests/crate.rs:17:29
|
17 | #[wasm_bindgen_test(crate = foo)]
| ^^^ use of undeclared crate or module `foo`

0 comments on commit 4ae9cdd

Please sign in to comment.