From 68a652ed2f51d366bb8033497e6dfe545895410e Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 23 Jul 2026 08:08:33 +0800 Subject: feat: scaffold crate structure and implement core macros Add the project skeleton, LICENSE files, README, Makefile, doc examples, and the initial implementation of `#[func]`, `invoke!`, and `select!` procedural macros. --- test/src/lib.rs | 3 ++ test/src/test_func.rs | 36 +++++++++++++++++++ test/src/test_invoke.rs | 50 +++++++++++++++++++++++++++ test/src/test_select.rs | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 test/src/lib.rs create mode 100644 test/src/test_func.rs create mode 100644 test/src/test_invoke.rs create mode 100644 test/src/test_select.rs (limited to 'test/src') diff --git a/test/src/lib.rs b/test/src/lib.rs new file mode 100644 index 0000000..5e91425 --- /dev/null +++ b/test/src/lib.rs @@ -0,0 +1,3 @@ +include!("test_func.rs"); +include!("test_invoke.rs"); +include!("test_select.rs"); diff --git a/test/src/test_func.rs b/test/src/test_func.rs new file mode 100644 index 0000000..71fe365 --- /dev/null +++ b/test/src/test_func.rs @@ -0,0 +1,36 @@ +#[might_be_async::func] +fn identity(x: i32) -> i32 { + x +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_func_identity() { + assert_eq!(identity(42), 42); +} + +#[might_be_async::func] +fn first(a: T, _b: T) -> T +where + T: std::fmt::Debug, +{ + a.clone() +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_func_generic() { + assert_eq!(first(1, 2), 1); + assert_eq!(first("a", "b"), "a"); +} + +#[might_be_async::func("custom_ft")] +fn triple(x: i32) -> i32 { + x * 3 +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_func_custom_feature() { + assert_eq!(triple(3), 9); +} diff --git a/test/src/test_invoke.rs b/test/src/test_invoke.rs new file mode 100644 index 0000000..b8506e9 --- /dev/null +++ b/test/src/test_invoke.rs @@ -0,0 +1,50 @@ +// ─── 测试 1: 默认 feature 名 ───────────────────────────────────────────────── +// +// 不指定 feature → 用 Cargo.toml 的 default_feature_name = "metadata_async" +// +// 展开(feature "metadata_async" 关闭时): +// { #[cfg(feature = "metadata_async")] { double(5).await } +// #[cfg(not(feature = "metadata_async"))] { double(5) } } + +#[cfg(not(feature = "metadata_async"))] +fn double(x: i32) -> i32 { + x * 2 +} + +#[cfg(feature = "metadata_async")] +async fn double(x: i32) -> i32 { + x * 2 +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_invoke_default() { + assert_eq!(might_be_async::invoke!(double(5)), 10); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_invoke_default() { + let result = futures::executor::block_on(async { might_be_async::invoke!(double(5)) }); + assert_eq!(result, 10); +} + +#[might_be_async::func] +fn square(x: i32) -> i32 { + x * x +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_invoke_explicit() { + assert_eq!(might_be_async::invoke!("metadata_async" => square(6)), 36); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_invoke_explicit() { + let result = futures::executor::block_on(async { + might_be_async::invoke!("metadata_async" => square(6)) + }); + assert_eq!(result, 36); +} diff --git a/test/src/test_select.rs b/test/src/test_select.rs new file mode 100644 index 0000000..2cf3bfe --- /dev/null +++ b/test/src/test_select.rs @@ -0,0 +1,92 @@ +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_explicit() { + let r = might_be_async::select! { "metadata_async" => { 1 } else ! => { 2 } }; + assert_eq!(r, 2); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_explicit() { + let r = might_be_async::select! { "metadata_async" => { 1 } else ! => { 2 } }; + assert_eq!(r, 1); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_not() { + let r = might_be_async::select! { "metadata_async" => { 10 } else ! => { 20 } }; + assert_eq!(r, 20); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_not() { + let r = might_be_async::select! { "metadata_async" => { 10 } else ! => { 20 } }; + assert_eq!(r, 10); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_not_first() { + let r = might_be_async::select! { ! => { 30 } else "metadata_async" => { 40 } }; + assert_eq!(r, 30); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_not_first() { + let r = might_be_async::select! { ! => { 30 } else "metadata_async" => { 40 } }; + assert_eq!(r, 40); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_both_explicit() { + let r = might_be_async::select! { "metadata_async" => { 100 } else "custom_ft" => { 200 } }; + assert_eq!(r, 100); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_implicit() { + let r = might_be_async::select! { { 1 + 2 } else { 3 + 4 } }; + assert_eq!(r, 7); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_implicit() { + let r = futures::executor::block_on(async { + might_be_async::select! { { 1 + 2 } else { 3 + 4 } } + }); + assert_eq!(r, 3); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_mixed_explicit_first() { + let r = might_be_async::select! { "metadata_async" => { 100 } else { 200 } }; + assert_eq!(r, 200); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_mixed_explicit_first() { + let r = might_be_async::select! { "metadata_async" => { 100 } else { 200 } }; + assert_eq!(r, 100); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_metadata_default() { + let r = might_be_async::select! { { 50 } else { 60 } }; + assert_eq!(r, 60); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_metadata_two_not() { + let r = might_be_async::select! { ! => { 70 } else ! => { 80 } }; + assert_eq!(r, 80); +} -- cgit