From e36afc8c491d676e93e99360ed6c1379aabae18c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Nov 2024 23:13:23 -0800 Subject: [PATCH] Add 'DiagnosticSuppressorTests' --- .../DiagnosticSuppressorTests.cs | 35 +++++++++++++++++++ .../SourceGeneratorTest.csproj | 11 ++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/Tests/SourceGeneratorTest/DiagnosticSuppressorTests.cs diff --git a/src/Tests/SourceGeneratorTest/DiagnosticSuppressorTests.cs b/src/Tests/SourceGeneratorTest/DiagnosticSuppressorTests.cs new file mode 100644 index 000000000..050bd94e2 --- /dev/null +++ b/src/Tests/SourceGeneratorTest/DiagnosticSuppressorTests.cs @@ -0,0 +1,35 @@ +using System.Threading.Tasks; +using Generator; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SourceGeneratorTest.Helpers; + +namespace SourceGeneratorTest; + +[TestClass] +public sealed class DiagnosticSuppressorTests +{ + public static readonly DiagnosticResult IDE0300 = DiagnosticResult.CompilerWarning("IDE0300"); + + [TestMethod] + public async Task CollectionExpression_IDE0300_ArrayInitializer_TargetingArray_NotSuppressed() + { + await new CSharpSuppressorTest( + """ + class TestClass + { + private int[] f = {|IDE0300:{|} 1, 2, 3 }; + + void TestMethod() + { + int[] a = {|IDE0300:{|} 1, 2, 3 }; + } + + public int[] P { get; } = {|IDE0300:{|} 1, 2, 3 }; + } + """) + .WithAnalyzer("Microsoft.CodeAnalysis.CSharp.UseCollectionExpression.CSharpUseCollectionExpressionForArrayDiagnosticAnalyzer, Microsoft.CodeAnalysis.CSharp.CodeStyle") + .WithSpecificDiagnostics(IDE0300) + .RunAsync(); + } +} \ No newline at end of file diff --git a/src/Tests/SourceGeneratorTest/SourceGeneratorTest.csproj b/src/Tests/SourceGeneratorTest/SourceGeneratorTest.csproj index 4156d5705..931947ccf 100644 --- a/src/Tests/SourceGeneratorTest/SourceGeneratorTest.csproj +++ b/src/Tests/SourceGeneratorTest/SourceGeneratorTest.csproj @@ -9,11 +9,18 @@ - + + - + + + + + + +