From 11a88682524b5f274252157c8f78d8bb96fd3a50 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Mon, 26 Feb 2024 11:43:18 +0100 Subject: [PATCH] [tests] tests for function_name! and function_name_short! macros --- src/lib.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index c29517e..ec4ed2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,3 +47,101 @@ macro_rules! DBG { println!(concat!("{}():{} ", $fmt), $crate::function_name!(), line!() $(,$arg)*); }; } + +#[cfg(test)] +mod tests_function_name_full_path { + + use super::*; + + trait MyTrait { + fn trait_fn_1(&self) -> &'static str { + function_name!() + } + + fn trait_fn_2(&self) -> &'static str { + function_name!() + } + } + + struct MyStruct; + + impl MyStruct { + fn method(&self) -> &'static str { + function_name!() + } + + fn associated_fn() -> &'static str { + function_name!() + } + } + + impl MyTrait for MyStruct { + fn trait_fn_1(&self) -> &'static str { + function_name!() + } + } + + #[test] + fn full_names() { + let h = MyStruct; + assert_eq!( + "::trait_fn_1", + h.trait_fn_1() + ); + assert_eq!( + "rustics_dbg::tests_function_name_full_path::MyTrait::trait_fn_2", + h.trait_fn_2() + ); + assert_eq!( + "rustics_dbg::tests_function_name_full_path::MyStruct::method", + h.method() + ); + assert_eq!( + "rustics_dbg::tests_function_name_full_path::MyStruct::associated_fn", + MyStruct::associated_fn() + ); + } +} + +#[cfg(test)] +mod tests_function_name_short { + + use super::*; + + trait MyTrait { + fn trait_fn_1(&self) -> &'static str { + function_name_short!() + } + + fn trait_fn_2(&self) -> &'static str { + function_name_short!() + } + } + + struct MyStruct; + + impl MyStruct { + fn method(&self) -> &'static str { + function_name_short!() + } + + fn associated_fn() -> &'static str { + function_name_short!() + } + } + + impl MyTrait for MyStruct { + fn trait_fn_1(&self) -> &'static str { + function_name_short!() + } + } + + #[test] + fn full_names() { + let h = MyStruct; + assert_eq!("trait_fn_1", h.trait_fn_1()); + assert_eq!("trait_fn_2", h.trait_fn_2()); + assert_eq!("method", h.method()); + assert_eq!("associated_fn", MyStruct::associated_fn()); + } +}