Skip to content

Commit 0e19ee7

Browse files
Sergio0694Copilot
andcommitted
Add scalar IReference<TypeName>/IReference<HResult> projection tests
Add members to TestComponentCSharp exercising the scalar IReference<T> projection for the two WinRT value types that map to .NET reference types: IReference<Windows.UI.Xaml.Interop.TypeName> -> System.Type and IReference<Windows.Foundation.HResult> -> System.Exception. Each adds a native-boxed property (BoxedTypeName / BoxedHResult) and a round-trip method (RoundtripTypeName / RoundtripHResult), with matching UnitTest coverage including the null case. These tests fail to build until the projection writer stops emitting the invalid Nullable<Type> / Nullable<Exception> for these instantiations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5bc0525 commit 0e19ee7

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/Tests/TestComponentCSharp/Class.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,26 @@ namespace winrt::TestComponentCSharp::implementation
18911891
return type.Name;
18921892
}
18931893

1894+
IReference<Windows::UI::Xaml::Interop::TypeName> Class::BoxedTypeName()
1895+
{
1896+
return winrt::box_value(winrt::xaml_typename<winrt::TestComponentCSharp::Class>()).as<IReference<Windows::UI::Xaml::Interop::TypeName>>();
1897+
}
1898+
1899+
IReference<Windows::UI::Xaml::Interop::TypeName> Class::RoundtripTypeName(IReference<Windows::UI::Xaml::Interop::TypeName> const& value)
1900+
{
1901+
return value;
1902+
}
1903+
1904+
IReference<winrt::hresult> Class::BoxedHResult()
1905+
{
1906+
return winrt::box_value(winrt::error_invalid_argument).as<IReference<winrt::hresult>>();
1907+
}
1908+
1909+
IReference<winrt::hresult> Class::RoundtripHResult(IReference<winrt::hresult> const& value)
1910+
{
1911+
return value;
1912+
}
1913+
18941914
WF::IInspectable Class::EmptyString()
18951915
{
18961916
return winrt::box_value(hstring{});

src/Tests/TestComponentCSharp/Class.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ namespace winrt::TestComponentCSharp::implementation
441441
static bool VerifyTypeIsThisClassType(Windows::UI::Xaml::Interop::TypeName const& type_name);
442442
static hstring GetTypeNameForType(Windows::UI::Xaml::Interop::TypeName const& type);
443443

444+
static Windows::Foundation::IReference<Windows::UI::Xaml::Interop::TypeName> BoxedTypeName();
445+
static Windows::Foundation::IReference<Windows::UI::Xaml::Interop::TypeName> RoundtripTypeName(Windows::Foundation::IReference<Windows::UI::Xaml::Interop::TypeName> const& value);
446+
447+
static Windows::Foundation::IReference<winrt::hresult> BoxedHResult();
448+
static Windows::Foundation::IReference<winrt::hresult> RoundtripHResult(Windows::Foundation::IReference<winrt::hresult> const& value);
449+
444450
static Windows::Foundation::IInspectable EmptyString();
445451
static Windows::Foundation::IInspectable BoxedDelegate();
446452
static Windows::Foundation::IInspectable BoxedEnum();

src/Tests/TestComponentCSharp/TestComponentCSharp.idl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,16 @@ namespace TestComponentCSharp
520520

521521
static String GetTypeNameForType(Windows.UI.Xaml.Interop.TypeName type);
522522

523+
// IReference<WUX.Interop.TypeName> projects to System.Type (TypeName is a WinRT value type but
524+
// projects to the reference type System.Type, so it must project as 'Type', not 'Nullable<Type>').
525+
static Windows.Foundation.IReference<Windows.UI.Xaml.Interop.TypeName> BoxedTypeName{ get; };
526+
static Windows.Foundation.IReference<Windows.UI.Xaml.Interop.TypeName> RoundtripTypeName(Windows.Foundation.IReference<Windows.UI.Xaml.Interop.TypeName> value);
527+
528+
// IReference<HResult> projects to System.Exception (HResult is a WinRT value type but projects to
529+
// the reference type System.Exception, so it must project as 'Exception', not 'Nullable<Exception>').
530+
static Windows.Foundation.IReference<Windows.Foundation.HResult> BoxedHResult{ get; };
531+
static Windows.Foundation.IReference<Windows.Foundation.HResult> RoundtripHResult(Windows.Foundation.IReference<Windows.Foundation.HResult> value);
532+
523533
// Other
524534
static Object EmptyString { get; };
525535

src/Tests/UnitTest/TestComponentCSharp_Tests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,6 +3758,34 @@ public void ProjectedTypeInfo()
37583758
Assert.IsTrue(Class.VerifyTypeIsReferenceInt32Type(typeof(int?)));
37593759
}
37603760

3761+
[TestMethod]
3762+
public void ReferenceTypeNameProjectsAsType()
3763+
{
3764+
// 'IReference<WUX.Interop.TypeName>' projects to 'System.Type', not the invalid 'Nullable<Type>'
3765+
// (TypeName is a WinRT value type, but it projects to the reference type 'System.Type'). The boxed
3766+
// value round-trips through the native boundary as a 'Type', including the null case.
3767+
Assert.AreEqual(typeof(Class), Class.BoxedTypeName);
3768+
3769+
Assert.AreEqual(typeof(int), Class.RoundtripTypeName(typeof(int)));
3770+
Assert.AreEqual(typeof(Class), Class.RoundtripTypeName(typeof(Class)));
3771+
Assert.IsNull(Class.RoundtripTypeName(null));
3772+
}
3773+
3774+
[TestMethod]
3775+
public void ReferenceHResultProjectsAsException()
3776+
{
3777+
// 'IReference<HResult>' projects to 'System.Exception', not the invalid 'Nullable<Exception>'
3778+
// (HResult is a WinRT value type, but it projects to the reference type 'System.Exception'). The
3779+
// boxed value round-trips through the native boundary as an 'Exception', including the null case.
3780+
Exception boxed = Class.BoxedHResult;
3781+
3782+
Assert.IsNotNull(boxed);
3783+
Assert.AreEqual(unchecked((int)0x80070057), boxed.HResult); // E_INVALIDARG
3784+
3785+
Assert.IsInstanceOfType<ArgumentOutOfRangeException>(Class.RoundtripHResult(new ArgumentOutOfRangeException()));
3786+
Assert.IsNull(Class.RoundtripHResult(null));
3787+
}
3788+
37613789
[TestMethod]
37623790
public void TypeInfoGenerics()
37633791
{

0 commit comments

Comments
 (0)