Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve core string type testing #2074

Merged
merged 2 commits into from Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/libs/windows/src/core/bindings.rs
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
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
@@ -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
@@ -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
@@ -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
@@ -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(())
}