diff --git a/tests/debug.rs b/tests/debug.rs index 0c4b180a..8944bd42 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -102,7 +102,7 @@ mod structs { assert_eq!(format!("{:03?}", UpperHex), "00B"); assert_eq!(format!("{:07?}", LowerExp), "03.15e0"); assert_eq!(format!("{:07?}", UpperExp), "03.15E0"); - assert_eq!(format!("{:018?}", Pointer).len(), 18); + assert_eq!(format!("{:018?}", Pointer), format!("{POINTER:018p}")); } mod omitted { @@ -246,6 +246,35 @@ mod structs { "Struct {\n field: 0.0,\n}", ); } + + mod pointer { + #[cfg(not(feature = "std"))] + use alloc::format; + + use derive_more::Debug; + + #[derive(Debug)] + struct Tuple<'a>(#[debug("{_0:p}.{:p}", self.0)] &'a i32); + + #[derive(Debug)] + struct Struct<'a> { + #[debug("{field:p}.{:p}", self.field)] + field: &'a i32, + } + + #[test] + fn assert() { + let a = 42; + assert_eq!( + format!("{:?}", Tuple(&a)), + format!("Tuple({0:p}.{0:p})", &a), + ); + assert_eq!( + format!("{:?}", Struct { field: &a }), + format!("Struct {{ field: {0:p}.{0:p} }}", &a), + ); + } + } } mod ignore { @@ -527,6 +556,37 @@ mod structs { assert_eq!(format!("{:?}", Tuple(10, true)), "10 * true"); assert_eq!(format!("{:?}", Struct { a: 10, b: true }), "10 * true"); } + + mod pointer { + #[cfg(not(feature = "std"))] + use alloc::format; + + use derive_more::Debug; + + #[derive(Debug)] + #[debug("{_0:p} * {_1:p}", _0 = self.0)] + struct Tuple<'a, 'b>(&'a u8, &'b bool); + + #[derive(Debug)] + #[debug("{a:p} * {b:p}", a = self.a)] + struct Struct<'a, 'b> { + a: &'a u8, + b: &'b bool, + } + + #[test] + fn assert() { + let (a, b) = (10, true); + assert_eq!( + format!("{:?}", Tuple(&a, &b)), + format!("{:p} * {:p}", &a, &b), + ); + assert_eq!( + format!("{:?}", Struct { a: &a, b: &b }), + format!("{:p} * {:p}", &a, &b), + ); + } + } } mod ignore { @@ -677,7 +737,10 @@ mod enums { assert_eq!(format!("{:03?}", Unit::UpperHex), "00B"); assert_eq!(format!("{:07?}", Unit::LowerExp), "03.15e0"); assert_eq!(format!("{:07?}", Unit::UpperExp), "03.15E0"); - assert_eq!(format!("{:018?}", Unit::Pointer).len(), 18); + assert_eq!( + format!("{:018?}", Unit::Pointer), + format!("{POINTER:018p}"), + ); } mod omitted { diff --git a/tests/display.rs b/tests/display.rs index 60de8182..0626fabc 100644 --- a/tests/display.rs +++ b/tests/display.rs @@ -139,7 +139,7 @@ mod structs { assert_eq!(format!("{:03}", UpperHex), "00B"); assert_eq!(format!("{:07}", LowerExp), "03.15e0"); assert_eq!(format!("{:07}", UpperExp), "03.15E0"); - assert_eq!(format!("{:018}", Pointer).len(), 18); + assert_eq!(format!("{:018}", Pointer), format!("{POINTER:018p}")); } } @@ -322,9 +322,10 @@ mod structs { format!("{:07E}", StructUpperExp { field: 42.0 }), "004.2E1", ); + let a = 42; assert_eq!( - format!("{:018p}", StructPointer { field: &42 }).len(), - 18, + format!("{:018p}", StructPointer { field: &a }), + format!("{:018p}", &a), ); } } @@ -394,9 +395,10 @@ mod structs { format!("{:07}", StructUpperExp { field: 42.0 }), "004.2E1", ); + let a = 42; assert_eq!( - format!("{:018}", StructPointer { field: &42 }).len(), - 18, + format!("{:018}", StructPointer { field: &a }), + format!("{:018p}", &a), ); } } @@ -466,9 +468,10 @@ mod structs { format!("{:07}", StructUpperExp { field: 42.0 }), "4.2E1", ); - assert_ne!( - format!("{:018}", StructPointer { field: &42 }).len(), - 18, + let a = 42; + assert_eq!( + format!("{:018}", StructPointer { field: &a }), + format!("{:p}", &a), ); } } @@ -671,9 +674,10 @@ mod structs { format!("{:07}", StructUpperExp { a: 41.0, b: 42.0 }), "004.2E1", ); + let (a, b) = (42, 43); assert_eq!( - format!("{:018}", StructPointer { a: &42, b: &43 }).len(), - 18, + format!("{:018}", StructPointer { a: &a, b: &b }), + format!("{:018p}", &b), ); } } @@ -764,7 +768,10 @@ mod enums { assert_eq!(format!("{:03}", Unit::UpperHex), "00B"); assert_eq!(format!("{:07}", Unit::LowerExp), "03.15e0"); assert_eq!(format!("{:07}", Unit::UpperExp), "03.15E0"); - assert_eq!(format!("{:018}", Unit::Pointer).len(), 18); + assert_eq!( + format!("{:018}", Unit::Pointer), + format!("{POINTER:018p}"), + ); } } @@ -922,8 +929,15 @@ mod enums { format!("{:07E}", UpperExp::B { field: 43.0 }), "004.3E1", ); - assert_eq!(format!("{:018p}", Pointer::A(&7)).len(), 18); - assert_eq!(format!("{:018p}", Pointer::B { field: &42 }).len(), 18); + let (a, b) = (7, 42); + assert_eq!( + format!("{:018p}", Pointer::A(&a)), + format!("{:018p}", &a), + ); + assert_eq!( + format!("{:018p}", Pointer::B { field: &b }), + format!("{:018p}", &b), + ); } } @@ -1026,8 +1040,15 @@ mod enums { format!("{:07}", UpperExp::B { field: 43.0 }), "004.3E1", ); - assert_eq!(format!("{:018}", Pointer::A(&7)).len(), 18); - assert_eq!(format!("{:018}", Pointer::B { field: &42 }).len(), 18); + let (a, b) = (7, 42); + assert_eq!( + format!("{:018}", Pointer::A(&a)), + format!("{:018p}", &a), + ); + assert_eq!( + format!("{:018}", Pointer::B { field: &b }), + format!("{:018p}", &b), + ); } } @@ -1124,8 +1145,12 @@ mod enums { assert_eq!(format!("{:07}", LowerExp::B { field: 43.0 }), "4.3e1"); assert_eq!(format!("{:07}", UpperExp::A(42.0)), "4.2E1"); assert_eq!(format!("{:07}", UpperExp::B { field: 43.0 }), "4.3E1"); - assert_ne!(format!("{:018}", Pointer::A(&7)).len(), 18); - assert_ne!(format!("{:018}", Pointer::B { field: &42 }).len(), 18); + let (a, b) = (7, 42); + assert_eq!(format!("{:018}", Pointer::A(&a)), format!("{:0p}", &a)); + assert_eq!( + format!("{:018}", Pointer::B { field: &b }), + format!("{:p}", &b), + ); } } } @@ -1276,10 +1301,14 @@ mod enums { format!("{:07}", UpperExp::B { a: 43.0, b: 52.0 }), "004.3E1", ); - assert_eq!(format!("{:018}", Pointer::A(&7.0, &8.3)).len(), 18); + let (a, b) = (8.3, 42.1); + assert_eq!( + format!("{:018}", Pointer::A(&7.0, &a)), + format!("{:018p}", &a), + ); assert_eq!( - format!("{:018}", Pointer::B { a: &42.1, b: &43.3 }).len(), - 18, + format!("{:018}", Pointer::B { a: &b, b: &43.3 }), + format!("{:018p}", &b), ); } } @@ -2024,12 +2053,19 @@ mod generic { format!("{:07E}", Enum::::B { field: 43.0 }), "004.3E1", ); - assert_eq!(format!("{:018p}", Tuple(&42)).len(), 18); - assert_eq!(format!("{:018p}", Struct { field: &42 }).len(), 18); - assert_eq!(format!("{:018p}", Enum::<_, &i8>::A(&7)).len(), 18); + let (a, b) = (42, 7); + assert_eq!(format!("{:018p}", Tuple(&a)), format!("{:018p}", &a)); assert_eq!( - format!("{:018p}", Enum::<&i8, _>::B { field: &42 }).len(), - 18, + format!("{:018p}", Struct { field: &a }), + format!("{:018p}", &a), + ); + assert_eq!( + format!("{:018p}", Enum::<_, &i8>::A(&b)), + format!("{:018p}", &b), + ); + assert_eq!( + format!("{:018p}", Enum::<&i8, _>::B { field: &a }), + format!("{:018p}", &a), ); } } @@ -2245,12 +2281,19 @@ mod generic { format!("{:07}", EnumUpperExp::::B { field: 43.0 }), "004.3E1", ); - assert_eq!(format!("{:018}", TuplePointer(&42)).len(), 18); - assert_eq!(format!("{:018}", StructPointer { field: &42 }).len(), 18); - assert_eq!(format!("{:018}", EnumPointer::<_, &i8>::A(&7)).len(), 18); + let (a, b) = (42, 7); + assert_eq!(format!("{:018}", TuplePointer(&a)), format!("{:018p}", &a)); + assert_eq!( + format!("{:018}", StructPointer { field: &a }), + format!("{:018p}", &a), + ); + assert_eq!( + format!("{:018}", EnumPointer::<_, &i8>::A(&b)), + format!("{:018p}", &b), + ); assert_eq!( - format!("{:018}", EnumPointer::<&i8, _>::B { field: &42 }).len(), - 18, + format!("{:018}", EnumPointer::<&i8, _>::B { field: &a }), + format!("{:018p}", &a), ); } }