Skip to content

Commit

Permalink
Improve core string type testing (#2074)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 28, 2022
1 parent e094bad commit 1071bdb
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/libs/windows/src/core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const FORMAT_MESSAGE_FROM_SYSTEM: u32 = 4096;
pub const FORMAT_MESSAGE_IGNORE_INSERTS: u32 = 512;
pub const AGILEREFERENCE_DEFAULT: i32 = 0;

pub const ERROR_NO_UNICODE_TRANSLATION: u32 = 1113u32;
pub const CLASS_E_CLASSNOTAVAILABLE: HRESULT = HRESULT(-2147221231i32);
pub const CO_E_NOTINITIALIZED: HRESULT = HRESULT(-2147221008i32);
pub const E_NOINTERFACE: HRESULT = HRESULT(-2147467262i32);
Expand Down
12 changes: 12 additions & 0 deletions crates/libs/windows/src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ impl std::convert::From<Error> for std::io::Error {
}
}

impl std::convert::From<std::string::FromUtf16Error> for Error {
fn from(_: std::string::FromUtf16Error) -> Self {
Self { code: HRESULT::from_win32(ERROR_NO_UNICODE_TRANSLATION), info: None }
}
}

impl std::convert::From<std::string::FromUtf8Error> for Error {
fn from(_: std::string::FromUtf8Error) -> Self {
Self { code: HRESULT::from_win32(ERROR_NO_UNICODE_TRANSLATION), info: None }
}
}

// Unfortunately this is needed to make types line up. The Rust type system does
// not know the `Infallible` can never be constructed. This code needs to be here
// to satesify the type checker but it will never be run. Once `!` is stabilizied
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions crates/tests/core/tests/pcstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use windows::{core::*, Win32::Foundation::*};

#[test]
fn test() -> Result<()> {
let p: PCSTR = s!("hello");
let s: String = unsafe { p.to_string()? };
assert_eq!("hello", s);
assert_eq!("hello", format!("{}", unsafe { p.display() }));

let invalid = &[0xc0, 0x80];
let p = PCSTR::from_raw(invalid.as_ptr());
let e: Error = unsafe { p.to_string().unwrap_err().into() };
assert_eq!(e.code(), ERROR_NO_UNICODE_TRANSLATION.into());
assert_eq!(e.message(), "No mapping for the Unicode character exists in the target multi-byte code page.");

Ok(())
}
18 changes: 18 additions & 0 deletions crates/tests/core/tests/pcwstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use windows::{core::*, Win32::Foundation::*};

#[test]
fn test() -> Result<()> {
// `w!` returns a `&HSTRING` so `into()` is needed to convert it to a `PCWSTR`
let p: PCWSTR = w!("hello").into();
let s: String = unsafe { p.to_string()? };
assert_eq!("hello", s);
assert_eq!("hello", format!("{}", unsafe { p.display() }));

let invalid = &[0xD834, 0xDD1E, 0x006d, 0x0075, 0xD800, 0x0069, 0x0063];
let p = PCWSTR::from_raw(invalid.as_ptr());
let e: Error = unsafe { p.to_string().unwrap_err().into() };
assert_eq!(e.code(), ERROR_NO_UNICODE_TRANSLATION.into());
assert_eq!(e.message(), "No mapping for the Unicode character exists in the target multi-byte code page.");

Ok(())
}
17 changes: 17 additions & 0 deletions crates/tests/core/tests/pstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use windows::{core::*, Win32::Foundation::*};

#[test]
fn test() -> Result<()> {
let p = PSTR::from_raw(s!("hello").as_ptr() as *mut _);
let s: String = unsafe { p.to_string()? };
assert_eq!("hello", s);
assert_eq!("hello", format!("{}", unsafe { p.display() }));

let invalid = &mut [0xc0, 0x80];
let p = PSTR::from_raw(invalid.as_mut_ptr());
let e: Error = unsafe { p.to_string().unwrap_err().into() };
assert_eq!(e.code(), ERROR_NO_UNICODE_TRANSLATION.into());
assert_eq!(e.message(), "No mapping for the Unicode character exists in the target multi-byte code page.");

Ok(())
}
17 changes: 17 additions & 0 deletions crates/tests/core/tests/pwstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use windows::{core::*, Win32::Foundation::*};

#[test]
fn test() -> Result<()> {
let p = PWSTR::from_raw(w!("hello").as_ptr() as *mut _);
let s: String = unsafe { p.to_string()? };
assert_eq!("hello", s);
assert_eq!("hello", format!("{}", unsafe { p.display() }));

let invalid = &mut [0xD834, 0xDD1E, 0x006d, 0x0075, 0xD800, 0x0069, 0x0063];
let p = PWSTR::from_raw(invalid.as_mut_ptr());
let e: Error = unsafe { p.to_string().unwrap_err().into() };
assert_eq!(e.code(), ERROR_NO_UNICODE_TRANSLATION.into());
assert_eq!(e.message(), "No mapping for the Unicode character exists in the target multi-byte code page.");

Ok(())
}

0 comments on commit 1071bdb

Please sign in to comment.