use lol_io::NativeBitFlags; use lol_macros::{const_uuid, const_uuid_wide}; use std::ffi::OsString; use std::os::windows::prelude::*; #[test] fn lol_test_macro_const_uuid() { static FOO: &'static str = const_uuid!(); let uuid = uuid::Uuid::try_parse(FOO); assert!(uuid.is_ok()); } #[test] fn lol_test_macro_const_uuid_prefix() { static FOO: &'static str = const_uuid!(prefix = r#"\\.\pipe\"#); let uuid = uuid::Uuid::try_parse(&FOO[9..]); assert!(uuid.is_ok()); assert_eq!(r#"\\.\pipe\"#, &FOO[..9]); } #[test] fn lol_test_macro_const_uuid_postfix() { static FOO: &'static str = const_uuid!(postfix = r#"foo"#); let uuid = uuid::Uuid::try_parse(&FOO[..FOO.len() - 3]); assert!(uuid.is_ok()); assert_eq!("foo", &FOO[FOO.len() - 3..]); } #[test] fn lol_test_macro_const_uuid_prefix_postfix() { static FOO: &'static str = const_uuid!(prefix = r#"\\.\pipe\"#, postfix = "foo"); let uuid = uuid::Uuid::try_parse(&FOO[9..FOO.len() - 3]); assert!(uuid.is_ok()); assert_eq!(r#"\\.\pipe\"#, &FOO[..9]); assert_eq!("foo", &FOO[FOO.len() - 3..]); } #[test] fn lol_test_macro_const_uuid_wide() { const WIDE: *const u16 = const_uuid_wide!(prefix = r#"\\.\pipe\"#, postfix = "foo"); let s = u16_ptr_to_string(WIDE); let uuid = uuid::Uuid::try_parse(&s[9..s.len() - 3]); assert!(uuid.is_ok()); assert_eq!(r#"\\.\pipe\"#, &s[..9]); assert_eq!("foo", &s[s.len() - 3..]); /// https://stackoverflow.com/questions/48586816/converting-raw-pointer-to-16-bit-unicode-character-to-file-path-in-rust fn u16_ptr_to_string(ptr: *const u16) -> String { unsafe { let len = (0..).take_while(|&i| *ptr.offset(i) != 0).count(); let slice = std::slice::from_raw_parts(ptr, len); OsString::from_wide(slice).to_str().unwrap().to_owned() } } } #[test] fn lol_test_macro_consts() { lol_io::consts! { /// This is a foo pub struct FOO: u32 { const THING = 1; const HAHAH = 2; } } let test = FOO::HAHAH; assert_eq!(&1, FOO::THING.as_ref()); assert_eq!(&2, test.as_ref()); assert_eq!(2, test.raw()); } #[test] fn lol_test_macro_const_flags() { struct Other { flags: u8, } lol_io::consts! { #[derive(NativeBitFlags)] #[lol(from = "Other", field = "flags")] #[lol(crate = lol_io)] pub struct Flags: u8 { const OVERLAPPED = 0b0000_0010 } } let other = Other { flags: 2 }; let flags = Flags::parse(&other); assert_eq!(Flags::OVERLAPPED.raw(), flags.raw()); }