use std::fmt::{self, Debug, Display, Formatter};
struct DisplayOnly;
impl Display for DisplayOnly {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("display-only")
}
}
struct DebugOnly;
impl Debug for DebugOnly {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("debug-only")
}
}
struct DisplayAndDebug;
impl Display for DisplayAndDebug {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("display-wins")
}
}
impl Debug for DisplayAndDebug {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("debug-loses")
}
}
struct Plain;
#[test]
fn to_string_returns_cloned_string_for_borrowed_and_owned_values() {
let value = "hello".to_string();
assert_eq!(phlow::to_string!(value.clone()), "hello".to_string());
assert_eq!(phlow::to_string!(&value), "hello".to_string());
}
#[test]
fn to_string_uses_display_when_available_for_borrowed_and_owned_values() {
assert_eq!(phlow::to_string!(DisplayOnly), "display-only".to_string());
assert_eq!(phlow::to_string!(&DisplayOnly), "display-only".to_string());
}
#[test]
fn to_string_uses_debug_when_display_is_unavailable_for_borrowed_and_owned_values() {
assert_eq!(phlow::to_string!(DebugOnly), "debug-only".to_string());
assert_eq!(phlow::to_string!(&DebugOnly), "debug-only".to_string());
}
#[test]
fn to_string_prefers_display_over_debug_for_borrowed_and_owned_values() {
assert_eq!(
phlow::to_string!(DisplayAndDebug),
"display-wins".to_string()
);
assert_eq!(
phlow::to_string!(&DisplayAndDebug),
"display-wins".to_string()
);
}
#[test]
fn to_string_falls_back_to_type_name_in_both_forms() {
assert_eq!(
phlow::to_string!(Plain),
std::any::type_name::<Plain>().to_string()
);
assert_eq!(
phlow::to_string!(&Plain),
std::any::type_name::<Plain>().to_string()
);
}