diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-07-23 08:47:07 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-07-23 08:47:07 +0800 |
| commit | 63dc4e38e602023d9d9b8b55fdfbe4e70f7aea2e (patch) | |
| tree | 4468d36862d785082531327d33f45951ab46e3d1 | |
| parent | 4152c5f8bded87ef60a236e00a44d1799704f6c6 (diff) | |
7
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | test/Cargo.toml | 3 | ||||
| -rw-r--r-- | test/src/lib.rs | 115 | ||||
| -rw-r--r-- | tests/test_func.rs | 45 | ||||
| -rw-r--r-- | tests/test_invoke.rs | 52 | ||||
| -rw-r--r-- | tests/test_select.rs | 105 |
6 files changed, 313 insertions, 10 deletions
@@ -6,6 +6,9 @@ edition = "2024" [lib] proc-macro = true +[features] +async = [] + [dependencies] proc-macro2 = "1.0.106" quote = "1.0.46" diff --git a/test/Cargo.toml b/test/Cargo.toml index e926a09..49ee2e9 100644 --- a/test/Cargo.toml +++ b/test/Cargo.toml @@ -3,5 +3,8 @@ name = "might_be_async_test" version = "0.1.0" edition = "2024" +[features] +async = [] + [dependencies] might_be_async = "../" diff --git a/test/src/lib.rs b/test/src/lib.rs index b93cf3f..bb83e6b 100644 --- a/test/src/lib.rs +++ b/test/src/lib.rs @@ -1,14 +1,109 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +// ─── #[func] tests ───────────────────────────────────────────────────────────────────── + +#[might_be_async::func] +fn identity(x: i32) -> i32 { + x +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_func_identity() { + assert_eq!(identity(42), 42); +} + +#[might_be_async::func] +fn first<T: PartialEq + Clone>(a: T, _b: T) -> T +where + T: std::fmt::Debug, +{ + a.clone() +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_func_generic() { + assert_eq!(first(1, 2), 1); + assert_eq!(first("a", "b"), "a"); +} + +// ─── invoke! tests ────────────────────────────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +fn double(x: i32) -> i32 { + x * 2 +} + +#[cfg(feature = "async")] +async fn double(x: i32) -> i32 { + x * 2 +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_invoke_default() { + assert_eq!(might_be_async::invoke!(double(5)), 10); +} + +#[cfg(feature = "async")] +#[test] +fn test_invoke_default() { + assert_eq!(might_be_async::invoke!(double(5)), 10); +} + +// ─── select! tests ────────────────────────────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_explicit() { + let r = might_be_async::select! { "async" => 1, "sync" => 2 }; + assert_eq!(r, 2); +} + +#[cfg(feature = "async")] +#[test] +fn test_select_explicit() { + let r = might_be_async::select! { "async" => 1, "sync" => 2 }; + assert_eq!(r, 1); +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_not() { + let r = might_be_async::select! { "async" => 10, ! => 20 }; + assert_eq!(r, 20); } -#[cfg(test)] -mod tests { - use super::*; +#[cfg(feature = "async")] +#[test] +fn test_select_not() { + let r = might_be_async::select! { "async" => 10, ! => 20 }; + assert_eq!(r, 10); +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_not_first() { + let r = might_be_async::select! { ! => 30, "async" => 40 }; + assert_eq!(r, 30); +} + +#[cfg(feature = "async")] +#[test] +fn test_select_not_first() { + let r = might_be_async::select! { ! => 30, "async" => 40 }; + assert_eq!(r, 40); +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_implicit() { + let r = might_be_async::select! { 1 + 2, 3 + 4 }; + assert_eq!(r, 7); +} - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +#[cfg(feature = "async")] +#[test] +fn test_select_implicit() { + let r = might_be_async::select! { 1 + 2, 3 + 4 }; + assert_eq!(r, 3); } diff --git a/tests/test_func.rs b/tests/test_func.rs new file mode 100644 index 0000000..d6555f3 --- /dev/null +++ b/tests/test_func.rs @@ -0,0 +1,45 @@ +//! Tests for the `#[func]` attribute macro. +//! +//! Run default (sync path): +//! cargo test --test test_func +//! +//! Run async path: +//! cargo test --features async --test test_func + +#[might_be_async::func] +fn identity(x: i32) -> i32 { + x +} + +#[test] +fn test_identity() { + assert_eq!(identity(42), 42); +} + +// ─── With generics and where clause ───────────────────────────────────────────────────── + +#[might_be_async::func] +fn first<T: PartialEq + Clone>(a: T, _b: T) -> T +where + T: std::fmt::Debug, +{ + a.clone() +} + +#[test] +fn test_first() { + assert_eq!(first(1, 2), 1); + assert_eq!(first("hello", "world"), "hello"); +} + +// ─── With explicit feature name ───────────────────────────────────────────────────────── + +#[might_be_async::func("async")] +fn add(x: i32, y: i32) -> i32 { + x + y +} + +#[test] +fn test_add() { + assert_eq!(add(2, 3), 5); +} diff --git a/tests/test_invoke.rs b/tests/test_invoke.rs new file mode 100644 index 0000000..da96ecc --- /dev/null +++ b/tests/test_invoke.rs @@ -0,0 +1,52 @@ +//! Tests for the `invoke!` macro. + +/// A mock "async" function — in sync mode it just returns directly. +#[cfg(not(feature = "async"))] +fn compute(x: i32) -> i32 { + x * 2 +} + +#[cfg(feature = "async")] +async fn compute(x: i32) -> i32 { + x * 2 +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_invoke_default() { + let r = might_be_async::invoke!(compute(5)); + assert_eq!(r, 10); +} + +#[cfg(feature = "async")] +#[test] +fn test_invoke_default() { + let r = might_be_async::invoke!(compute(5)); + assert_eq!(r, 10); +} + +// ─── Explicit feature name ────────────────────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +fn square(x: i32) -> i32 { + x * x +} + +#[cfg(feature = "async")] +async fn square(x: i32) -> i32 { + x * x +} + +#[cfg(not(feature = "async"))] +#[test] +fn test_invoke_explicit() { + let r = might_be_async::invoke!("async" => square(6)); + assert_eq!(r, 36); +} + +#[cfg(feature = "async")] +#[test] +fn test_invoke_explicit() { + let r = might_be_async::invoke!("async" => square(6)); + assert_eq!(r, 36); +} diff --git a/tests/test_select.rs b/tests/test_select.rs new file mode 100644 index 0000000..c707de4 --- /dev/null +++ b/tests/test_select.rs @@ -0,0 +1,105 @@ +//! Tests for the `select!` macro. + +// ─── Explicit mode ────────────────────────────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_explicit_sync() { + let r = might_be_async::select! { + "async" => 100, + "sync" => 200 + }; + assert_eq!(r, 200); +} + +#[cfg(feature = "async")] +#[test] +fn test_select_explicit_async() { + let r = might_be_async::select! { + "async" => 100, + "sync" => 200 + }; + assert_eq!(r, 100); +} + +// ─── Explicit + Not ───────────────────────────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_not_sync() { + let r = might_be_async::select! { + "async" => 10, + ! => 20 + }; + assert_eq!(r, 20); +} + +#[cfg(feature = "async")] +#[test] +fn test_select_not_async() { + let r = might_be_async::select! { + "async" => 10, + ! => 20 + }; + assert_eq!(r, 10); +} + +// ─── Not + explicit ───────────────────────────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_not_first_sync() { + let r = might_be_async::select! { + ! => 30, + "async" => 40 + }; + assert_eq!(r, 30); +} + +#[cfg(feature = "async")] +#[test] +fn test_select_not_first_async() { + let r = might_be_async::select! { + ! => 30, + "async" => 40 + }; + assert_eq!(r, 40); +} + +// ─── Implicit mode: neither has .await ────────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_implicit_no_await_sync() { + let r = might_be_async::select! { 10 + 20, 30 + 40 }; + assert_eq!(r, 70); +} + +#[cfg(feature = "async")] +#[test] +fn test_select_implicit_no_await_async() { + let r = might_be_async::select! { 10 + 20, 30 + 40 }; + assert_eq!(r, 30); +} + +// ─── Implicit mode with block expressions ─────────────────────────────────────────────── + +#[cfg(not(feature = "async"))] +#[test] +fn test_select_implicit_block_sync() { + let r = might_be_async::select! { + { 50 }, + { 60 } + }; + assert_eq!(r, 60); +} + +#[cfg(feature = "async")] +#[test] +fn test_select_implicit_block_async() { + let r = might_be_async::select! { + { 50 }, + { 60 } + }; + assert_eq!(r, 50); +} |
