Skip to content

Commit f467479

Browse files
authored
Merge pull request #4056 from sharwell/lambda-discard
Update tests for lambda discard parameters
2 parents 14dea4d + 55477d9 commit f467479

File tree

5 files changed

+110
-27
lines changed

5 files changed

+110
-27
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/DocumentationRules/SA1611CSharp9UnitTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,24 @@ public async Task TestPrimaryRecordConstructorIncludeMissingParametersAsync(stri
3737

3838
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3939
}
40+
41+
[Fact]
42+
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
43+
public async Task TestLambdaDiscardParametersAsync()
44+
{
45+
var testCode = @"
46+
/// <summary>Test class.</summary>
47+
public class TestClass
48+
{
49+
/// <summary>Test method.</summary>
50+
public void TestMethod()
51+
{
52+
System.Func<int, int, int> handler = (_, _) => 0;
53+
}
54+
}
55+
";
56+
57+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
58+
}
4059
}
4160
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/DocumentationRules/SA1612CSharp9UnitTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.DocumentationRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.DocumentationRules;
10+
using Xunit;
711

812
public partial class SA1612CSharp9UnitTests : SA1612CSharp8UnitTests
913
{
14+
[Fact]
15+
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
16+
public async Task TestLambdaDiscardParametersAsync()
17+
{
18+
var testCode = @"
19+
/// <summary>Test class.</summary>
20+
public class TestClass
21+
{
22+
/// <summary>Test method.</summary>
23+
public void TestMethod()
24+
{
25+
System.Func<int, int, int> handler = (_, _) => 0;
26+
}
27+
}
28+
";
29+
30+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
31+
}
1032
}
1133
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/NamingRules/SA1312CSharp9UnitTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,34 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.NamingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.NamingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.NamingRules.SA1312VariableNamesMustBeginWithLowerCaseLetter,
13+
StyleCop.Analyzers.NamingRules.RenameToLowerCaseCodeFixProvider>;
714

815
public partial class SA1312CSharp9UnitTests : SA1312CSharp8UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
19+
public async Task TestLambdaDiscardParametersDoNotReportAsync()
20+
{
21+
var testCode = @"
22+
using System;
23+
24+
public class TestClass
25+
{
26+
public void Test()
27+
{
28+
Func<int, int, int> handler = (_, _) => 0;
29+
}
30+
}
31+
";
32+
33+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
34+
}
1035
}
1136
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/NamingRules/SA1313CSharp9UnitTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ namespace StyleCop.Analyzers.Test.CSharp9.NamingRules
1414

1515
public partial class SA1313CSharp9UnitTests : SA1313CSharp8UnitTests
1616
{
17+
[Fact]
18+
[WorkItem(3977, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3977")]
19+
public async Task TestLambdaDiscardParametersAsync()
20+
{
21+
var testCode = @"
22+
using System;
23+
24+
public class TestClass
25+
{
26+
public void Test()
27+
{
28+
Action<int, int> handler = (_, _) => { };
29+
}
30+
}
31+
";
32+
33+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
34+
}
35+
1736
[Fact]
1837
[WorkItem(3168, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3168")]
1938
[WorkItem(3181, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3181")]

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1612UnitTests.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.DocumentationRules
75
{
86
using System.Collections.Generic;
@@ -562,35 +560,13 @@ public class ClassName
562560
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
563561
}
564562

565-
private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult normallyExpected, string declaration)
566-
{
567-
return GetExpectedDiagnostics(new[] { normallyExpected }, declaration);
568-
}
569-
570-
// Syntax node actions for type declarations with a primary constructor were called twice
571-
// before support for c# 11 was added.
572-
private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult[] normallyExpected, string declaration)
573-
{
574-
var isPrimaryConstructor = declaration.Contains("record") || declaration.Contains("class") || declaration.Contains("struct");
575-
576-
if (isPrimaryConstructor && !LightupHelpers.SupportsCSharp11)
577-
{
578-
// Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/53136 and https://github.com/dotnet/roslyn/issues/70488
579-
return normallyExpected.Concat(normallyExpected).ToArray();
580-
}
581-
else
582-
{
583-
return normallyExpected;
584-
}
585-
}
586-
587-
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
563+
protected static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
588564
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, ignoreCompilerDiagnostics: false, cancellationToken);
589565

590-
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
566+
protected static Task VerifyCSharpDiagnosticAsync(string source, string? testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
591567
=> VerifyCSharpDiagnosticAsync(source, testSettings, expected, ignoreCompilerDiagnostics: false, cancellationToken);
592568

593-
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, bool ignoreCompilerDiagnostics, CancellationToken cancellationToken)
569+
protected static Task VerifyCSharpDiagnosticAsync(string source, string? testSettings, DiagnosticResult[] expected, bool ignoreCompilerDiagnostics, CancellationToken cancellationToken)
594570
{
595571
string contentWithoutParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
596572
<ClassName>
@@ -696,5 +672,27 @@ private static Task VerifyCSharpDiagnosticAsync(string source, string testSettin
696672
test.ExpectedDiagnostics.AddRange(expected);
697673
return test.RunAsync(cancellationToken);
698674
}
675+
676+
private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult normallyExpected, string declaration)
677+
{
678+
return GetExpectedDiagnostics(new[] { normallyExpected }, declaration);
679+
}
680+
681+
// Syntax node actions for type declarations with a primary constructor were called twice
682+
// before support for c# 11 was added.
683+
private static DiagnosticResult[] GetExpectedDiagnostics(DiagnosticResult[] normallyExpected, string declaration)
684+
{
685+
var isPrimaryConstructor = declaration.Contains("record") || declaration.Contains("class") || declaration.Contains("struct");
686+
687+
if (isPrimaryConstructor && !LightupHelpers.SupportsCSharp11)
688+
{
689+
// Diagnostic issued twice because of https://github.com/dotnet/roslyn/issues/53136 and https://github.com/dotnet/roslyn/issues/70488
690+
return normallyExpected.Concat(normallyExpected).ToArray();
691+
}
692+
else
693+
{
694+
return normallyExpected;
695+
}
696+
}
699697
}
700698
}

0 commit comments

Comments
 (0)