wgpu_macros/
lib.rs

1use heck::ToSnakeCase;
2use proc_macro::TokenStream;
3use quote::quote;
4use syn::Ident;
5
6/// Creates a test that will run on all gpus on a given system.
7///
8/// Apply this macro to a static variable with a type that can be converted to a `GpuTestConfiguration`.
9#[proc_macro_attribute]
10pub fn gpu_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
11    let input_static = syn::parse_macro_input!(item as syn::ItemStatic);
12    let vis = input_static.vis;
13    let expr = &input_static.expr;
14    let ident = &input_static.ident;
15    let ident_str = ident.to_string();
16    let ident_lower = ident_str.to_snake_case();
17
18    let register_test_name = Ident::new(&format!("{ident}"), ident.span());
19    let test_name_webgl = Ident::new(&format!("{ident_lower}_webgl"), ident.span());
20
21    quote! {
22        #[allow(non_snake_case)]
23        #vis fn #register_test_name() -> ::wgpu_test::GpuTestConfiguration {
24            struct S;
25
26            // Allow any type that can be converted to a GpuTestConfiguration
27            ::wgpu_test::GpuTestConfiguration::from(#expr).name_from_init_function_typename::<S>(#ident_lower)
28        }
29
30        #[cfg(target_arch = "wasm32")]
31        #[wasm_bindgen_test::wasm_bindgen_test]
32        #vis async fn #test_name_webgl() {
33            struct S;
34
35            // Allow any type that can be converted to a GpuTestConfiguration
36            let test_config = ::wgpu_test::GpuTestConfiguration::from(#expr).name_from_init_function_typename::<S>(#ident_lower);
37
38            ::wgpu_test::execute_test(None, test_config, None).await;
39        }
40    }
41    .into()
42}