cross_locks/utils.rs
1/*────────────────── cross-platform test helpers ──────────────────*/
2
3/// Expands to `tokio::test` on native targets and to a
4/// `wasm_bindgen_test` **module** on `wasm32` (so the name doesn’t
5/// clash with the outer function space).
6///
7/// ```rust
8/// cross_locks::async_test!(my_test {
9/// /* … runs on both platforms … */
10/// });
11/// ```
12#[macro_export]
13macro_rules! async_test {
14 ($name:ident $body:block) => {
15 /* ---- Browser (wasm32 + feature=browser) ------------------- */
16 #[cfg(target_arch = "wasm32")]
17 mod $name {
18 use super::*;
19 use wasm_bindgen_test::*;
20 #[cfg(feature = "browser")]
21 wasm_bindgen_test_configure!(run_in_browser);
22
23 #[wasm_bindgen_test]
24 async fn run() $body
25 }
26
27 /* ---- Native (everything else) ---------------------------- */
28 #[cfg(not(all(target_arch = "wasm32")))]
29 #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
30 async fn $name() $body
31 };
32}
33
34/// Shorthand when you **don’t** need a dedicated module on wasm
35/// (i.e. the test name is unique anyway).
36/// Generates either a `tokio::test` **or** a `wasm_bindgen_test`
37/// for the same async function.
38///
39/// ```rust
40/// cross_locks::dual_test! { my_other_test {
41/// // …
42/// }}
43/// ```
44#[macro_export]
45macro_rules! dual_test {
46 ($name:ident $body:block) => {
47 #[cfg_attr(
48 all(target_arch = "wasm32", feature = "browser"),
49 wasm_bindgen_test::wasm_bindgen_test
50 )]
51 #[cfg_attr(
52 not(all(target_arch = "wasm32", feature = "browser")),
53 tokio::test(flavor = "multi_thread", worker_threads = 4)
54 )]
55 async fn $name() $body
56 };
57}