From 1006baf08915bc74d7ea90beee8721bb674027b9 Mon Sep 17 00:00:00 2001 From: tyranron Date: Sun, 19 Jan 2025 19:24:59 +0200 Subject: [PATCH] More tests to the God of Tests! --- tests/display.rs | 149 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/tests/display.rs b/tests/display.rs index 74364527..478e1d94 100644 --- a/tests/display.rs +++ b/tests/display.rs @@ -530,6 +530,80 @@ mod structs { } } + mod r#unsized { + use core::ptr; + + use super::*; + + #[derive(Display)] + struct Tuple(str); + + #[derive(Display)] + struct Struct { + tail: str, + } + + #[test] + fn assert() { + let dat = "14"; + + let t = + unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple) }; + assert_eq!(t.to_string(), "14"); + let s = + unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct) }; + assert_eq!(s.to_string(), "14"); + } + + mod interpolated { + use core::ptr; + + use super::*; + + #[derive(Display)] + #[display("{}.", _0)] + struct Tuple1(str); + + #[derive(Display)] + #[display("{_0}.")] + struct Tuple2(str); + + #[derive(Display)] + #[display("{}.", tail)] + struct Struct1 { + tail: str, + } + + #[derive(Display)] + #[display("{tail}.")] + struct Struct2 { + tail: str, + } + + #[test] + fn assert() { + let dat = "14"; + + let t1 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple1) + }; + assert_eq!(t1.to_string(), "14."); + let t2 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple2) + }; + assert_eq!(t2.to_string(), "14."); + let s1 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct1) + }; + assert_eq!(s1.to_string(), "14."); + let s2 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct2) + }; + assert_eq!(s2.to_string(), "14."); + } + } + } + #[cfg(nightly)] mod never { use super::*; @@ -3023,6 +3097,81 @@ mod generic { } } } + + mod r#unsized { + use core::ptr; + + use super::*; + + #[derive(Display)] + struct Tuple(T); + + #[derive(Display)] + struct Struct { + tail: T, + } + + #[test] + fn assert() { + let dat = "14"; + + let t = + unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple) }; + assert_eq!(t.to_string(), "14"); + let s = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct) + }; + assert_eq!(s.to_string(), "14"); + } + + mod interpolated { + use core::ptr; + + use super::*; + + #[derive(Display)] + #[display("{}.", _0)] + struct Tuple1(T); + + #[derive(Display)] + #[display("{_0}.")] + struct Tuple2(T); + + #[derive(Display)] + #[display("{}.", tail)] + struct Struct1 { + tail: T, + } + + #[derive(Display)] + #[display("{tail}.")] + struct Struct2 { + tail: T, + } + + #[test] + fn assert() { + let dat = "14"; + + let t1 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple1) + }; + assert_eq!(t1.to_string(), "14."); + let t2 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple2) + }; + assert_eq!(t2.to_string(), "14."); + let s1 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct1) + }; + assert_eq!(s1.to_string(), "14."); + let s2 = unsafe { + &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct2) + }; + assert_eq!(s2.to_string(), "14."); + } + } + } } // See: https://github.com/JelteF/derive_more/issues/363