From b7d86f1c157934af7005a8f83cf12539ceea0599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Sat, 18 Mar 2023 10:02:20 +0100 Subject: [PATCH] Update so that c# 7 tests will be run with the expected language version in test projects for c# 8 and above #3493 --- .../LayoutRules/SA1500CSharp7UnitTests.cs | 7 ++--- .../LayoutRules/SA1505CSharp7UnitTests.cs | 5 ++-- .../LayoutRules/SA1508CSharp7UnitTests.cs | 5 ++-- .../LayoutRules/SA1509CSharp7UnitTests.cs | 5 ++-- .../LayoutRules/SA1513CSharp7UnitTests.cs | 5 ++-- .../SA1413CSharp7UnitTests.cs | 5 ++-- .../NamingRules/SA1316CSharp7UnitTests.cs | 19 ++++++------- .../SA1137CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1000CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1001CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1002CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1010CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1011CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1012CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1013CSharp7UnitTests.cs | 5 ++-- .../SpacingRules/SA1026CSharp7UnitTests.cs | 3 ++- .../Helpers/LanguageVersionTestExtensions.cs | 27 +++++++++++++++++++ .../Verifiers/StyleCopCodeFixVerifier`2.cs | 6 +++++ 18 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/LanguageVersionTestExtensions.cs diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1500CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1500CSharp7UnitTests.cs index c39fb36b4..13aa70fb0 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1500CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1500CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.LayoutRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1500BracesForMultiLineStatementsMustNotShareLine, StyleCop.Analyzers.LayoutRules.SA1500CodeFixProvider>; @@ -199,7 +200,7 @@ public unsafe void TestMethod() }; } }"; - await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_3, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } /// @@ -277,7 +278,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(23, 22), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } /// @@ -335,7 +336,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(15, 22), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1505CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1505CSharp7UnitTests.cs index c36eb150f..3e0022567 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1505CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1505CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.LayoutRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1505OpeningBracesMustNotBeFollowedByBlankLine, StyleCop.Analyzers.LayoutRules.SA1505CodeFixProvider>; @@ -129,7 +130,7 @@ public unsafe void TestMethod() "; var expectedDiagnostic = Diagnostic().WithLocation(8, 13); - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -171,7 +172,7 @@ public unsafe void TestMethod() "; var expectedDiagnostic = Diagnostic().WithLocation(8, 13); - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1508CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1508CSharp7UnitTests.cs index 526a8175e..49a23ccab 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1508CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1508CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.LayoutRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1508ClosingBracesMustNotBePrecededByBlankLine, StyleCop.Analyzers.LayoutRules.SA1508CodeFixProvider>; @@ -129,7 +130,7 @@ public unsafe void TestMethod() "; var expectedDiagnostic = Diagnostic().WithLocation(13, 13); - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -171,7 +172,7 @@ public unsafe void TestMethod() "; var expectedDiagnostic = Diagnostic().WithLocation(13, 13); - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1509CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1509CSharp7UnitTests.cs index b93557b57..d45118e8f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1509CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1509CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.LayoutRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1509OpeningBracesMustNotBePrecededByBlankLine, StyleCop.Analyzers.LayoutRules.SA1509CodeFixProvider>; @@ -116,7 +117,7 @@ public unsafe void TestMethod() "; var expectedDiagnostic = Diagnostic().WithLocation(9, 13); - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -158,7 +159,7 @@ public unsafe void TestMethod() "; var expectedDiagnostic = Diagnostic().WithLocation(9, 13); - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1513CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1513CSharp7UnitTests.cs index 655d826a7..2a54043ad 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1513CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1513CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.LayoutRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.LayoutRules.SA1513ClosingBraceMustBeFollowedByBlankLine, StyleCop.Analyzers.LayoutRules.SA1513CodeFixProvider>; @@ -171,7 +172,7 @@ public unsafe void TestMethod() } "; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -200,7 +201,7 @@ public unsafe void TestMethod() } "; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1413CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1413CSharp7UnitTests.cs index ecfe6b132..532e19b90 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1413CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1413CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.MaintainabilityRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.MaintainabilityRules.SA1413UseTrailingCommasInMultiLineInitializers, StyleCop.Analyzers.MaintainabilityRules.SA1413CodeFixProvider>; @@ -61,7 +62,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(12, 17), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -108,7 +109,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(12, 17), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/NamingRules/SA1316CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/NamingRules/SA1316CSharp7UnitTests.cs index 7adc891d1..5e784d66a 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/NamingRules/SA1316CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/NamingRules/SA1316CSharp7UnitTests.cs @@ -7,10 +7,11 @@ namespace StyleCop.Analyzers.Test.CSharp7.NamingRules { using System.Threading; using System.Threading.Tasks; + using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Testing; - using StyleCop.Analyzers.Lightup; using StyleCop.Analyzers.NamingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.NamingRules.SA1316TupleElementNamesShouldUseCorrectCasing, StyleCop.Analyzers.NamingRules.SA1316CodeFixProvider>; @@ -116,7 +117,7 @@ public class TestClass }} "; - await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7, testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } /// @@ -136,7 +137,7 @@ public class TestClass } "; - await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7, testCode, DefaultTestSettings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(testCode, DefaultTestSettings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } /// @@ -168,7 +169,7 @@ public void TestMethod() }} "; - await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7_1, testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_1.OrLaterDefault(), testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } /// @@ -204,7 +205,7 @@ public void TestMethod2() }} "; - await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7_1, testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_1.OrLaterDefault(), testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } /// @@ -257,7 +258,7 @@ public class TestClass // diagnostics are specified inline }; - await VerifyCSharpFixAsync(LanguageVersionEx.CSharp7, testCode, settings, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(testCode, settings, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false); } /// @@ -287,7 +288,7 @@ public void TestMethod() }} "; - await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7_1, testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_1.OrLaterDefault(), testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } /// @@ -327,7 +328,7 @@ public void TestMethod2() // diagnostics are specified inline }; - await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7_1, testCode, settings, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_1.OrLaterDefault(), testCode, settings, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -398,7 +399,7 @@ public void MethodName((string Name, string Value) obj) } "; - await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7, testCode, DefaultTestSettings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpDiagnosticAsync(testCode, DefaultTestSettings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1137CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1137CSharp7UnitTests.cs index 77104c7cc..911f36837 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1137CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1137CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.ReadabilityRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.ReadabilityRules.SA1137ElementsShouldHaveTheSameIndentation, StyleCop.Analyzers.ReadabilityRules.IndentationCodeFixProvider>; @@ -160,7 +161,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(17, 1), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -220,7 +221,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(17, 1), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs index b2d288ab0..8b8dbb921 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -246,10 +247,10 @@ public async Task TestStackAllocImplicitArrayStatementAsync() string statementWithSpace = @"int* x = stackalloc [] { 3 };"; - await this.TestKeywordStatementAsync(statementWithoutSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithoutSpace, languageVersion: LanguageVersion.CSharp7_3).ConfigureAwait(false); + await this.TestKeywordStatementAsync(statementWithoutSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithoutSpace, languageVersion: LanguageVersion.CSharp7_3.OrLaterDefault()).ConfigureAwait(false); // this case is handled by SA1026, so it shouldn't be reported here - await this.TestKeywordStatementAsync(statementWithSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithSpace, languageVersion: LanguageVersion.CSharp7_3).ConfigureAwait(false); + await this.TestKeywordStatementAsync(statementWithSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithSpace, languageVersion: LanguageVersion.CSharp7_3.OrLaterDefault()).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1001CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1001CSharp7UnitTests.cs index d8e638061..d0e666c72 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1001CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1001CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1001CommasMustBeSpacedCorrectly, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -158,7 +159,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(8, 47).WithArguments(string.Empty, "followed"), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -197,7 +198,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(8, 43).WithArguments(string.Empty, "followed"), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1002CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1002CSharp7UnitTests.cs index 83b2d0cdd..2ae6b4ea1 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1002CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1002CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1002SemicolonsMustBeSpacedCorrectly, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -49,7 +50,7 @@ public unsafe void TestMethod() Diagnostic().WithArguments(" not", "preceded").WithLocation(7, 51), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -84,7 +85,7 @@ public unsafe void TestMethod() Diagnostic().WithArguments(" not", "preceded").WithLocation(7, 47), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1010CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1010CSharp7UnitTests.cs index 7214054da..9515ba866 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1010CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1010CSharp7UnitTests.cs @@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using StyleCop.Analyzers.Test.SpacingRules; using Xunit; using static StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1010OpeningSquareBracketsMustBeSpacedCorrectly, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -60,7 +61,7 @@ public unsafe void TestMethod() Diagnostic(DescriptorNotPreceded).WithLocation(8, 41), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -103,7 +104,7 @@ public unsafe void TestMethod() Diagnostic(DescriptorNotFollowed).WithLocation(7, 37), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1011CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1011CSharp7UnitTests.cs index bab4e349b..4255db74e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1011CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1011CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1011ClosingSquareBracketsMustBeSpacedCorrectly, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -87,7 +88,7 @@ public unsafe void TestMethod() Diagnostic().WithArguments(string.Empty, "followed").WithLocation(10, 41), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -135,7 +136,7 @@ public unsafe void TestMethod() Diagnostic().WithArguments(string.Empty, "followed").WithLocation(10, 37), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1012CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1012CSharp7UnitTests.cs index 1d68810e6..3fea85791 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1012CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1012CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -73,7 +74,7 @@ public unsafe void TestMethod() Diagnostic().WithArguments(string.Empty, "followed").WithLocation(14, 13), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -132,7 +133,7 @@ public unsafe void TestMethod() Diagnostic().WithArguments(string.Empty, "followed").WithLocation(14, 13), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1013CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1013CSharp7UnitTests.cs index aaa9e8b13..f25407a97 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1013CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1013CSharp7UnitTests.cs @@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1013ClosingBracesMustBeSpacedCorrectly, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -106,7 +107,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(2).WithArguments(string.Empty, "preceded"), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } [Fact] @@ -163,7 +164,7 @@ public unsafe void TestMethod() Diagnostic().WithLocation(2).WithArguments(string.Empty, "preceded"), }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1026CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1026CSharp7UnitTests.cs index 12034ed50..47d8c5f63 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1026CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1026CSharp7UnitTests.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.SpacingRules; using Xunit; + using static StyleCop.Analyzers.Test.Helpers.LanguageVersionTestExtensions; using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< StyleCop.Analyzers.SpacingRules.SA1026CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArrayAllocation, StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; @@ -32,7 +33,7 @@ public async Task TestImplicitStackAllocArrayCreationExpressionAsync(string spac const string expectedCode = "public class Foo { public unsafe Foo() { int* ints = stackalloc[] { 1, 2, 3 }; } }"; DiagnosticResult[] expected = { Diagnostic().WithArguments("stackalloc").WithLocation(1, 54) }; - await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, expectedCode, CancellationToken.None).ConfigureAwait(false); + await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3.OrLaterDefault(), testCode, expected, expectedCode, CancellationToken.None).ConfigureAwait(false); } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/LanguageVersionTestExtensions.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/LanguageVersionTestExtensions.cs new file mode 100644 index 000000000..4a146ba7d --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Helpers/LanguageVersionTestExtensions.cs @@ -0,0 +1,27 @@ +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +namespace StyleCop.Analyzers.Test.Helpers +{ + using System; + using Microsoft.CodeAnalysis.CSharp; + using StyleCop.Analyzers.Lightup; + + internal static class LanguageVersionTestExtensions + { + public static LanguageVersion? OrLaterDefault(this LanguageVersion input) + { + // Use the default version instead, if that would be a later version than the one specified + switch (input) + { + case LanguageVersionEx.CSharp7_1: + case LanguageVersionEx.CSharp7_2: + case LanguageVersionEx.CSharp7_3: + return LightupHelpers.SupportsCSharp8 ? null : input; + + default: + throw new ArgumentException($"Unexpected value {input}", nameof(input)); + } + } + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier`2.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier`2.cs index 383bf2691..5f9be5385 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier`2.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier`2.cs @@ -53,6 +53,9 @@ internal static Task VerifyCSharpDiagnosticAsync(LanguageVersion? languageVersio internal static Task VerifyCSharpDiagnosticAsync(LanguageVersion? languageVersion, string source, DiagnosticResult[] expected, CancellationToken cancellationToken) => StyleCopDiagnosticVerifier.VerifyCSharpDiagnosticAsync(languageVersion, source, settings: null, expected, cancellationToken); + internal static Task VerifyCSharpDiagnosticAsync(string source, string settings, DiagnosticResult[] expected, CancellationToken cancellationToken) + => StyleCopDiagnosticVerifier.VerifyCSharpDiagnosticAsync(languageVersion: null, source, settings, expected, cancellationToken); + internal static Task VerifyCSharpDiagnosticAsync(LanguageVersion? languageVersion, string source, string settings, DiagnosticResult[] expected, CancellationToken cancellationToken) => StyleCopDiagnosticVerifier.VerifyCSharpDiagnosticAsync(languageVersion, source, settings, expected, cancellationToken); @@ -77,6 +80,9 @@ internal static Task VerifyCSharpFixAsync(LanguageVersion? languageVersion, stri internal static Task VerifyCSharpFixAsync(LanguageVersion? languageVersion, string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken) => VerifyCSharpFixAsync(languageVersion, source, settings: null, expected, fixedSource, cancellationToken); + internal static Task VerifyCSharpFixAsync(string source, string settings, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken) + => VerifyCSharpFixAsync(languageVersion: null, source, settings, expected, fixedSource, cancellationToken); + internal static Task VerifyCSharpFixAsync(LanguageVersion? languageVersion, string source, string settings, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken) { var test = new CSharpTest(languageVersion)