|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis; |
| 9 | + using Microsoft.CodeAnalysis.CSharp; |
6 | 10 | using StyleCop.Analyzers.Test.ReadabilityRules; |
| 11 | + using TestHelper; |
| 12 | + using Xunit; |
7 | 13 |
|
8 | 14 | public class SA1131CSharp7UnitTests : SA1131UnitTests |
9 | 15 | { |
| 16 | + [Theory] |
| 17 | + [InlineData("==", "==")] |
| 18 | + [InlineData("!=", "!=")] |
| 19 | + [WorkItem(2675, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2675")] |
| 20 | + public async Task TestDefaultLiteralStructComparismOutsideIfAsync(string oldOperator, string newOperator) |
| 21 | + { |
| 22 | + var testCode = $@" |
| 23 | +using System; |
| 24 | +public class TypeName |
| 25 | +{{ |
| 26 | + public void Test() |
| 27 | + {{ |
| 28 | + TestStruct i = default; |
| 29 | + bool b = default {oldOperator} i; |
| 30 | + }} |
| 31 | +}} |
| 32 | +
|
| 33 | +struct TestStruct |
| 34 | +{{ |
| 35 | + public static bool operator == (TestStruct a, TestStruct b) {{ return true; }} |
| 36 | + public static bool operator != (TestStruct a, TestStruct b) {{ return false; }} |
| 37 | +}} |
| 38 | +"; |
| 39 | + var fixedCode = $@" |
| 40 | +using System; |
| 41 | +public class TypeName |
| 42 | +{{ |
| 43 | + public void Test() |
| 44 | + {{ |
| 45 | + TestStruct i = default; |
| 46 | + bool b = i {newOperator} default; |
| 47 | + }} |
| 48 | +}} |
| 49 | +
|
| 50 | +struct TestStruct |
| 51 | +{{ |
| 52 | + public static bool operator == (TestStruct a, TestStruct b) {{ return true; }} |
| 53 | + public static bool operator != (TestStruct a, TestStruct b) {{ return false; }} |
| 54 | +}} |
| 55 | +"; |
| 56 | + DiagnosticResult[] expected = |
| 57 | + { |
| 58 | + this.CSharpDiagnostic().WithLocation(8, 18), |
| 59 | + }; |
| 60 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 61 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 62 | + await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 63 | + } |
| 64 | + |
| 65 | + protected override Project ApplyCompilationOptions(Project project) |
| 66 | + { |
| 67 | + var newProject = base.ApplyCompilationOptions(project); |
| 68 | + |
| 69 | + var parseOptions = (CSharpParseOptions)newProject.ParseOptions; |
| 70 | + |
| 71 | + return newProject.WithParseOptions(parseOptions.WithLanguageVersion(LanguageVersion.Latest)); |
| 72 | + } |
10 | 73 | } |
11 | 74 | } |
0 commit comments