Skip to content

Commit

Permalink
More test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev committed Feb 16, 2022
1 parent c39bc4f commit cf05fe2
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 169 deletions.
28 changes: 16 additions & 12 deletions crates/libs/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syn::spanned::Spanned;
/// A COM interface definition
///
/// # Example
/// ```rust
/// ```rust,ignore
/// #[windows_interface::interface("8CEEB155-2849-4ce5-9448-91FF70E1E4D9")]
/// unsafe trait IUIAnimationVariable: IUnknown {
/// fn GetValue(&self, value: *mut f64) -> HRESULT;
Expand Down Expand Up @@ -46,10 +46,12 @@ macro_rules! expected_token {

/// Parsed interface
///
/// ```rust
/// #[windows_interface::interface("0000010c-0000-0000-C000-000000000046")]
/// unsafe trait IFoo {}
/// ```rust,ignore
/// #[windows_interface::interface("8CEEB155-2849-4ce5-9448-91FF70E1E4D9")]
/// unsafe trait IUIAnimationVariable: IUnknown {
/// //^ parses this
/// fn GetValue(&self, value: *mut f64) -> HRESULT;
/// }
/// ```
struct Interface {
pub visibility: syn::Visibility,
Expand Down Expand Up @@ -310,10 +312,12 @@ impl Parse for Interface {

/// Parsed interface guid attribute
///
/// ```rust
/// #[windows_interface::interface("0000010c-0000-0000-C000-000000000046")]
/// //^ parses this
/// unsafe trait IFoo {}
/// ```rust,ignore
/// #[windows_interface::interface("8CEEB155-2849-4ce5-9448-91FF70E1E4D9")]
/// //^ parses this
/// unsafe trait IUIAnimationVariable: IUnknown {
/// fn GetValue(&self, value: *mut f64) -> HRESULT;
/// }
/// ```
struct Guid(syn::LitStr);

Expand Down Expand Up @@ -388,11 +392,11 @@ impl Parse for Guid {

/// A parsed interface method
///
/// ```rust
/// #[windows_interface::interface("0000010c-0000-0000-C000-000000000046")]
/// unsafe trait IFoo {
/// ```rust,ignore
/// #[windows_interface::interface("8CEEB155-2849-4ce5-9448-91FF70E1E4D9")]
/// unsafe trait IUIAnimationVariable: IUnknown {
/// fn GetValue(&self, value: *mut f64) -> HRESULT;
/// //^ parses this
/// //^ parses this
/// }
/// ```
struct InterfaceMethod {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "test_interface"
name = "test_implement_interface"
version = "0.0.0"
authors = ["Microsoft"]
edition = "2018"
Expand Down
File renamed without changes.
81 changes: 81 additions & 0 deletions crates/tests/implement_interface/tests/com.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#![allow(non_snake_case)]
#![feature(const_fn_trait_bound)]
#![feature(const_fn_fn_ptr_basics)]

use windows::{core::*, Win32::Foundation::*, Win32::System::Com::*};

/// A custom declaration of implementation of `IUri`
#[interface("a39ee748-6a27-4817-a6f2-13914bef5890")]
pub unsafe trait ICustomUri: IUnknown {
unsafe fn GetPropertyBSTR(&self) -> HRESULT;
unsafe fn GetPropertyLength(&self) -> HRESULT;
unsafe fn GetPropertyDWORD(&self) -> HRESULT;
unsafe fn HasProperty(&self) -> HRESULT;
unsafe fn GetAbsoluteUri(&self) -> HRESULT;
unsafe fn GetAuthority(&self) -> HRESULT;
unsafe fn GetDisplayUri(&self) -> HRESULT;
unsafe fn GetDomain(&self, value: *mut BSTR) -> HRESULT;
// etc
}

#[implement(ICustomUri)]
struct CustomUri;

impl ICustomUri_Impl for CustomUri {
unsafe fn GetPropertyBSTR(&self) -> HRESULT {
todo!()
}
unsafe fn GetPropertyLength(&self) -> HRESULT {
todo!()
}
unsafe fn GetPropertyDWORD(&self) -> HRESULT {
todo!()
}
unsafe fn HasProperty(&self) -> HRESULT {
todo!()
}
unsafe fn GetAbsoluteUri(&self) -> HRESULT {
todo!()
}
unsafe fn GetAuthority(&self) -> HRESULT {
todo!()
}
unsafe fn GetDisplayUri(&self) -> HRESULT {
todo!()
}
unsafe fn GetDomain(&self, value: *mut BSTR) -> HRESULT {
*value = "kennykerr.ca".into();
S_OK
}
}

#[test]
fn test_custom_interface() -> windows::core::Result<()> {
unsafe {
// Use the OS implementation through the OS interface
let a: IUri = CreateUri("http://kennykerr.ca", Default::default(), 0)?;
let domain = a.GetDomain()?;
assert_eq!(domain, "kennykerr.ca");

// Call the OS implementation through the custom interface
let b: ICustomUri = a.cast()?;
let mut domain = BSTR::new();
b.GetDomain(&mut domain).ok()?;
assert_eq!(domain, "kennykerr.ca");

// Use the custom implementation through the OS interface
let c: ICustomUri = CustomUri.into();
// This works because `ICustomUri` and `IUri` share the same guid
let c: IUri = c.cast()?;
let domain = c.GetDomain()?;
assert_eq!(domain, "kennykerr.ca");

// Call the custom implementation through the custom interface
let d: ICustomUri = c.cast()?;
let mut domain = BSTR::new();
d.GetDomain(&mut domain).ok()?;
assert_eq!(domain, "kennykerr.ca");

Ok(())
}
}
156 changes: 0 additions & 156 deletions crates/tests/interface/tests/com.rs

This file was deleted.

0 comments on commit cf05fe2

Please sign in to comment.