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 interop testing #3253

Merged
merged 6 commits into from
Sep 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
/target
*.lock
*.winmd
**/src/winrt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ fn main() {
println!("cargo:rerun-if-changed=src/client.cpp");
println!("cargo:rustc-link-lib=windows.0.52.0");

let include = std::env::var("OUT_DIR").unwrap();

cppwinrt::cppwinrt([
"-in",
"../json_validator_winrt/sample.winmd",
&format!("{}\\System32\\WinMetadata", env!("windir")),
"-out",
"src",
&include,
])
.unwrap();

Expand All @@ -20,5 +22,6 @@ fn main() {
.std("c++20")
.flag("/EHsc")
.file("src/client.cpp")
.include(include)
.compile("client");
}
5 changes: 4 additions & 1 deletion crates/tests/misc/noexcept/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ fn main() {
panic!("{error}");
}

let include = std::env::var("OUT_DIR").unwrap();

cppwinrt::cppwinrt([
"-in",
"test.winmd",
&format!("{}\\System32\\WinMetadata", env!("windir")),
"-out",
"src",
&include,
])
.unwrap();

Expand All @@ -55,5 +57,6 @@ fn main() {
.std("c++20")
.flag("/EHsc")
.file("src/interop.cpp")
.include(include)
.compile("interop");
}
6 changes: 6 additions & 0 deletions crates/tests/winrt/constructors_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ features = [
# Cargo doesn't understand cdylib targets. https://github.com/rust-lang/cargo/issues/7825
[dependencies.test_constructors]
path = "../constructors"

[build-dependencies]
cc = "1.0"

[build-dependencies.cppwinrt]
workspace = true
25 changes: 25 additions & 0 deletions crates/tests/winrt/constructors_client/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
fn main() {
if !cfg!(target_env = "msvc") {
return;
}

println!("cargo:rerun-if-changed=src/interop.cpp");

windows_bindgen::bindgen([
"--in",
"../constructors/metadata.winmd",
Expand All @@ -10,4 +16,23 @@ fn main() {
"no-bindgen-comment",
])
.unwrap();

let include = std::env::var("OUT_DIR").unwrap();

cppwinrt::cppwinrt([
"-in",
"../constructors/metadata.winmd",
&format!("{}\\System32\\WinMetadata", env!("windir")),
"-out",
&include,
])
.unwrap();

cc::Build::new()
.cpp(true)
.std("c++20")
.flag("/EHsc")
.file("src/interop.cpp")
.include(include)
.compile("interop");
}
35 changes: 35 additions & 0 deletions crates/tests/winrt/constructors_client/src/interop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <windows.h>
#include <assert.h>
#include "winrt/test_constructors.h"

using namespace winrt;
using namespace test_constructors;

void test()
{
Activatable activatable;
assert(activatable.Property() == 0);

Activatable activatable2(123);
assert(activatable2.Property() == 123);

Composable composable;
assert(composable.Property() == 0);

Composable composable2(456);
assert(composable2.Property() == 456);
}

extern "C"
{
HRESULT __stdcall interop() noexcept
try
{
test();
return S_OK;
}
catch (...)
{
return to_hresult();
}
}
7 changes: 7 additions & 0 deletions crates/tests/winrt/constructors_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#![cfg(target_env = "msvc")]
#![cfg(test)]

mod bindings;
use bindings::*;
use windows::core::*;

extern "system" {
fn interop() -> HRESULT;
}

#[test]
fn test() -> Result<()> {
unsafe { interop().ok()? };

let activatable = Activatable::new()?;
assert_eq!(activatable.Property()?, 0);

Expand Down