|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Test.ReadabilityRules |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Analyzers.ReadabilityRules; |
| 10 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | + using TestHelper; |
| 12 | + using Xunit; |
| 13 | + |
| 14 | + public class SA1130UnitTests : DiagnosticVerifier |
| 15 | + { |
| 16 | + [Fact] |
| 17 | + public async Task TestSimpleDelegateUseAsync() |
| 18 | + { |
| 19 | + var testCode = @" |
| 20 | +using System; |
| 21 | +public class TypeName |
| 22 | +{ |
| 23 | + public void Test() |
| 24 | + { |
| 25 | + Action action1 = delegate { }; |
| 26 | + Action action2 = delegate() { }; |
| 27 | + Action<int> action3 = delegate(int i) { }; |
| 28 | + } |
| 29 | +}"; |
| 30 | + var expected = new[] |
| 31 | + { |
| 32 | + this.CSharpDiagnostic().WithLocation(7, 26), |
| 33 | + this.CSharpDiagnostic().WithLocation(8, 26), |
| 34 | + this.CSharpDiagnostic().WithLocation(9, 31) |
| 35 | + }; |
| 36 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task TestDelegateUseAsMethodArgumentsAsync() |
| 41 | + { |
| 42 | + var testCode = @" |
| 43 | +using System; |
| 44 | +public class TypeName |
| 45 | +{ |
| 46 | + public void Test(Action argument) |
| 47 | + { |
| 48 | +
|
| 49 | + } |
| 50 | +
|
| 51 | + public void Test() |
| 52 | + { |
| 53 | + Test(delegate { }); |
| 54 | + } |
| 55 | +}"; |
| 56 | + var expected = this.CSharpDiagnostic().WithLocation(12, 14); |
| 57 | + |
| 58 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public async Task TestDelegateUseAsMethodArgumentsWithConflictingExpressionOverloadAsync() |
| 63 | + { |
| 64 | + var testCode = @" |
| 65 | +using System; |
| 66 | +using System.Linq.Expressions; |
| 67 | +public class TypeName |
| 68 | +{ |
| 69 | + public void Test(Action argument) |
| 70 | + { |
| 71 | +
|
| 72 | + } |
| 73 | +
|
| 74 | + public void Test(Expression<Action> argument) |
| 75 | + { |
| 76 | +
|
| 77 | + } |
| 78 | +
|
| 79 | + public void Test() |
| 80 | + { |
| 81 | + Test(delegate { }); |
| 82 | + } |
| 83 | +}"; |
| 84 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 85 | + } |
| 86 | + |
| 87 | + /// <inheritdoc/> |
| 88 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 89 | + { |
| 90 | + yield return new SA1130UseLambdaSyntax(); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments