[tests] tests for function_name! and function_name_short! macros

This commit is contained in:
ddidderr 2024-02-26 11:43:18 +01:00
parent e63189514f
commit 11a8868252
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -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!(
"<rustics_dbg::tests_function_name_full_path::MyStruct as rustics_dbg::tests_function_name_full_path::MyTrait>::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());
}
}