Skip to content

Commit f8e0b22

Browse files
authored
Add test for cross-winmd IInspectable derivation and fix a tiny bug (#1547)
1 parent 12cfe7d commit f8e0b22

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/Microsoft.Windows.CsWin32/Generator.Com.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private static StatementSyntax ThrowOnHRFailure(ExpressionSyntax hrExpression) =
134134
foreach (QualifiedTypeDefinitionHandle baseType in baseTypes)
135135
{
136136
TypeDefinition baseTypeDef = baseType.Generator.Reader.GetTypeDefinition(baseType.DefinitionHandle);
137-
if (this.FindGuidAttribute(baseTypeDef.GetCustomAttributes()) is null)
137+
if (baseType.Generator.FindGuidAttribute(baseTypeDef.GetCustomAttributes()) is null)
138138
{
139139
canDeclareAsInterface = false;
140140
break;

test/CsWin32Generator.Tests/CsWin32GeneratorTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,21 @@ public async Task VerifyOverloadPriorityAttributeInNet8(LanguageVersion langVers
542542
Assert.Empty(methodsWithAttribute);
543543
}
544544
}
545+
546+
[Theory, CombinatorialData]
547+
public async Task CrossWinMD_IInspectable(
548+
[CombinatorialValues([false, true])] bool allowMarshaling,
549+
[CombinatorialValues([null, "TestPInvoke"])] string pinvokeClassName,
550+
[CombinatorialValues(["net8.0", "net9.0"])] string tfm)
551+
{
552+
this.compilation = this.starterCompilations[tfm];
553+
this.win32winmdPaths = [.. this.win32winmdPaths!, CustomIInspectableMetadataPath];
554+
this.nativeMethodsJsonOptions = new NativeMethodsJsonOptions
555+
{
556+
AllowMarshaling = allowMarshaling,
557+
ClassName = pinvokeClassName,
558+
};
559+
this.nativeMethods.Add("ITestDerivedFromInspectable");
560+
await this.InvokeGeneratorAndCompile($"{nameof(this.CrossWinMD_IInspectable)}_{tfm}_{allowMarshaling}_{pinvokeClassName ?? "null"}");
561+
}
545562
}

test/CsWin32Generator.Tests/CsWin32GeneratorTestsBase.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public enum TestOptions
2121
DoNotFailOnDiagnostics = 2,
2222
}
2323

24+
public record NativeMethodsJsonOptions(
25+
bool? AllowMarshaling = null,
26+
bool? EmitSingleFile = null,
27+
bool? Public = null,
28+
string? ClassName = null);
29+
2430
public partial class CsWin32GeneratorTestsBase : GeneratorTestBase
2531
{
2632
protected bool fullGeneration;
@@ -35,6 +41,7 @@ public partial class CsWin32GeneratorTestsBase : GeneratorTestBase
3541
protected int expectedExitCode = 0;
3642
protected string? tfm;
3743
protected string[]? win32winmdPaths;
44+
protected NativeMethodsJsonOptions? nativeMethodsJsonOptions;
3845

3946
public CsWin32GeneratorTestsBase(ITestOutputHelper logger)
4047
: base(logger)
@@ -99,6 +106,16 @@ protected async Task InvokeGenerator(string outputPath, string testCase, TestOpt
99106
File.WriteAllLines(nativeMethodsTxtPath, this.nativeMethods);
100107
}
101108

109+
if (this.nativeMethodsJsonOptions is NativeMethodsJsonOptions generatorOptions)
110+
{
111+
string nativeMethodsJsonPath = Path.Combine(this.GetTestCaseOutputDirectory(testCase), "NativeMethods.json");
112+
var jsonOptions = new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
113+
jsonOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault;
114+
string jsonContent = System.Text.Json.JsonSerializer.Serialize(generatorOptions, jsonOptions);
115+
File.WriteAllText(nativeMethodsJsonPath, jsonContent);
116+
this.nativeMethodsJson = Path.GetFileName(nativeMethodsJsonPath);
117+
}
118+
102119
Directory.CreateDirectory(outputPath);
103120

104121
this.Logger.WriteLine($"OutputPath: {outputPath}");

0 commit comments

Comments
 (0)