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 5bea714
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
* Add bindings for `CanvasTransform.setTransform(DOMMatrix2DInit)`.
[#3580](https://github.com/rustwasm/wasm-bindgen/pull/3580)

* Add attribute `crate` in the proc-macro `wasm_bindgen_test`.
[#3593](https://github.com/rustwasm/wasm-bindgen/pull/3593)

### Changed

* Updated the WebGPU WebIDL.
Expand Down
2 changes: 1 addition & 1 deletion crates/test-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", default-features = false, features = [ "parsing", "proc-macro", "derive" ] }
syn = { version = "2.0", default-features = false, features = [ "parsing", "proc-macro", "derive", "printing" ] }

[dev-dependencies]
wasm-bindgen-test = { path = "../test" }
Expand Down
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 5bea714

Please sign in to comment.