|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Diagnostics; |
| 7 | +using System.Linq; |
7 | 8 | using System.Runtime.CompilerServices; |
8 | 9 | using System.Threading.Tasks; |
| 10 | +using Microsoft.CodeAnalysis; |
| 11 | +using Microsoft.CodeAnalysis.CSharp; |
| 12 | +using Microsoft.CodeAnalysis.Testing; |
9 | 13 | using Microsoft.Interop.UnitTests; |
10 | 14 | using Xunit; |
11 | 15 | using VerifyComInterfaceGenerator = Microsoft.Interop.UnitTests.Verifiers.CSharpSourceGeneratorVerifier<Microsoft.Interop.ComInterfaceGenerator>; |
@@ -369,5 +373,80 @@ public async Task ValidateComInterfaceSnippets(string id, string source) |
369 | 373 |
|
370 | 374 | await VerifyComInterfaceGenerator.VerifySourceGeneratorAsync(source); |
371 | 375 | } |
| 376 | + |
| 377 | + [Fact] |
| 378 | + public async Task DocumentedComInterfaceDoesNotProduceCS1591Warnings() |
| 379 | + { |
| 380 | + string source = """ |
| 381 | + using System.Runtime.InteropServices; |
| 382 | + using System.Runtime.InteropServices.Marshalling; |
| 383 | +
|
| 384 | + namespace Test |
| 385 | + { |
| 386 | + /// <summary> |
| 387 | + /// This is my interface. |
| 388 | + /// </summary> |
| 389 | + [GeneratedComInterface, Guid("27dd3a3d-4c16-485a-a123-7cd8f39c6ef2")] |
| 390 | + public partial interface IMyInterface |
| 391 | + { |
| 392 | + /// <summary> |
| 393 | + /// This does something. |
| 394 | + /// </summary> |
| 395 | + void DoSomething(); |
| 396 | + } |
| 397 | +
|
| 398 | + /// <summary> |
| 399 | + /// This is my other interface. |
| 400 | + /// </summary> |
| 401 | + [GeneratedComInterface, Guid("1b681178-368a-4d13-8893-66b4673d2ff9")] |
| 402 | + public partial interface MyOtherInterface : IMyInterface |
| 403 | + { |
| 404 | + /// <summary> |
| 405 | + /// This does something else. |
| 406 | + /// </summary> |
| 407 | + void DoSomethingElse(); |
| 408 | + } |
| 409 | + } |
| 410 | + """; |
| 411 | + |
| 412 | + var test = new VerifyCompilationTest<Microsoft.Interop.ComInterfaceGenerator>(false) |
| 413 | + { |
| 414 | + TestCode = source, |
| 415 | + TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck | TestBehaviors.SkipGeneratedCodeCheck, |
| 416 | + CompilationVerifier = compilation => |
| 417 | + { |
| 418 | + // Verify that no CS1591 warnings are produced in the generated code |
| 419 | + var diagnostics = compilation.GetDiagnostics(); |
| 420 | + var cs1591Diagnostics = diagnostics.Where(d => d.Id == "CS1591").ToList(); |
| 421 | + |
| 422 | + Assert.Empty(cs1591Diagnostics); |
| 423 | + } |
| 424 | + }; |
| 425 | + |
| 426 | + // Enable XML documentation warnings to ensure CS1591 would be raised if not suppressed |
| 427 | + test.SolutionTransforms.Add((solution, projectId) => |
| 428 | + { |
| 429 | + var project = solution.GetProject(projectId); |
| 430 | + if (project is null) return solution; |
| 431 | + |
| 432 | + // Set parse options to enable documentation mode which is required for CS1591 validation |
| 433 | + var parseOptions = (CSharpParseOptions?)project.ParseOptions; |
| 434 | + if (parseOptions is not null) |
| 435 | + { |
| 436 | + parseOptions = parseOptions.WithDocumentationMode(DocumentationMode.Diagnose); |
| 437 | + solution = solution.WithProjectParseOptions(projectId, parseOptions); |
| 438 | + project = solution.GetProject(projectId)!; |
| 439 | + } |
| 440 | + |
| 441 | + var compilationOptions = project.CompilationOptions! |
| 442 | + .WithSpecificDiagnosticOptions(new Dictionary<string, ReportDiagnostic> |
| 443 | + { |
| 444 | + ["CS1591"] = ReportDiagnostic.Warn |
| 445 | + }); |
| 446 | + return solution.WithProjectCompilationOptions(projectId, compilationOptions); |
| 447 | + }); |
| 448 | + |
| 449 | + await test.RunAsync(); |
| 450 | + } |
372 | 451 | } |
373 | 452 | } |
0 commit comments