Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit baffb3c

Browse files
committed
Merge pull request #41 from sharwell/test-coverage
Test coverage
2 parents 7fee02b + b4c1bb1 commit baffb3c

File tree

12 files changed

+189
-474
lines changed

12 files changed

+189
-474
lines changed

AsyncUsageAnalyzers/AsyncUsageAnalyzers.CodeFixes/Helpers/RenameHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static async Task<Solution> RenameSymbolAsync(Document document, SyntaxNo
2121
var annotatedToken = annotatedRoot.FindToken(declarationToken.SpanStart);
2222

2323
var semanticModel = await annotatedDocument.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
24-
var symbol = semanticModel?.GetDeclaredSymbol(annotatedToken.Parent, cancellationToken);
24+
var symbol = semanticModel.GetDeclaredSymbol(annotatedToken.Parent, cancellationToken);
2525

2626
var newSolution = await Renamer.RenameSymbolAsync(annotatedSolution, symbol, newName, null, cancellationToken).ConfigureAwait(false);
2727

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 AsyncUsageAnalyzers.Test
5+
{
6+
using Xunit;
7+
8+
public class AnalyzerConstantsUnitTests
9+
{
10+
[Fact]
11+
public void TestEnabledByDefault()
12+
{
13+
Assert.True(AnalyzerConstants.EnabledByDefault);
14+
}
15+
16+
[Fact]
17+
public void TestDisabledByDefault()
18+
{
19+
Assert.False(AnalyzerConstants.DisabledByDefault);
20+
}
21+
22+
[Fact]
23+
public void TestDisabledAlternative()
24+
{
25+
Assert.False(AnalyzerConstants.DisabledAlternative);
26+
}
27+
28+
[Fact]
29+
public void TestDisabledNoTests()
30+
{
31+
#if DEBUG
32+
Assert.True(AnalyzerConstants.DisabledNoTests);
33+
#else
34+
Assert.False(AnalyzerConstants.DisabledNoTests);
35+
#endif
36+
}
37+
}
38+
}

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/AsyncUsageAnalyzers.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@
117117
</Reference>
118118
</ItemGroup>
119119
<ItemGroup>
120+
<Compile Include="AnalyzerConstantsUnitTests.cs" />
121+
<Compile Include="AttributesUnitTests.cs" />
120122
<Compile Include="Helpers\DiagnosticResultLocation.cs" />
121123
<Compile Include="Helpers\MetadataReferences.cs" />
124+
<Compile Include="Helpers\SpecializedTasksUnitTests.cs" />
122125
<Compile Include="Helpers\TestDiagnosticProvider.cs" />
123126
<Compile Include="Naming\AvoidAsyncSuffixUnitTests.cs" />
124127
<Compile Include="Naming\UseAsyncSuffixUnitTests.cs" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 AsyncUsageAnalyzers.Test
5+
{
6+
using Xunit;
7+
8+
public class AttributesUnitTests
9+
{
10+
[Fact]
11+
public void TestNoCodeFixAttribute()
12+
{
13+
var attribute = new NoCodeFixAttribute("Message");
14+
Assert.Equal("Message", attribute.Reason);
15+
}
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 AsyncUsageAnalyzers.Test.Helpers
5+
{
6+
using System.Threading.Tasks;
7+
using AsyncUsageAnalyzers.Helpers;
8+
using Xunit;
9+
10+
public class SpecializedTasksUnitTests
11+
{
12+
[Fact]
13+
public void TestCompletedTask()
14+
{
15+
Assert.NotNull(SpecializedTasks.CompletedTask);
16+
Assert.Equal(TaskStatus.RanToCompletion, SpecializedTasks.CompletedTask.Status);
17+
Assert.Same(SpecializedTasks.CompletedTask, SpecializedTasks.CompletedTask);
18+
}
19+
}
20+
}

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/Reliability/AvoidAsyncVoidUnitTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ namespace AsyncUsageAnalyzers.Test.Reliability
77
using System.Threading;
88
using System.Threading.Tasks;
99
using AsyncUsageAnalyzers.Reliability;
10+
using Microsoft.CodeAnalysis;
1011
using Microsoft.CodeAnalysis.Diagnostics;
1112
using TestHelper;
1213
using Xunit;
1314

1415
public class AvoidAsyncVoidUnitTests : DiagnosticVerifier
1516
{
17+
private static readonly DiagnosticDescriptor CS1660 =
18+
new DiagnosticDescriptor("CS1660", "Error", "Cannot convert lambda expression to type '{0}' because it is not a delegate type", "Compiler", DiagnosticSeverity.Error, true);
19+
20+
private static readonly DiagnosticDescriptor CS1989 =
21+
new DiagnosticDescriptor("CS1989", "Error", "Async lambda expressions cannot be converted to expression trees", "Compiler", DiagnosticSeverity.Error, true);
22+
1623
[Fact]
1724
public async Task TestAsyncReturnVoidAsync()
1825
{
@@ -100,6 +107,82 @@ class ClassName
100107
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
101108
}
102109

110+
[Fact]
111+
public async Task TestNonAsyncLambdaReturnTaskAsync()
112+
{
113+
string testCode = @"
114+
using System;
115+
using System.Threading.Tasks;
116+
class ClassName
117+
{
118+
static Func<Task> ZeroArgumentFunction;
119+
static Func<object, Task> SingleArgumentFunction;
120+
121+
ClassName()
122+
{
123+
ZeroArgumentFunction = () => Task.Delay(42);
124+
SingleArgumentFunction = arg => Task.Delay(42);
125+
SingleArgumentFunction = (object arg) => Task.Delay(42);
126+
SingleArgumentFunction = delegate (object arg) { return Task.Delay(42); };
127+
}
128+
}
129+
";
130+
131+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
132+
}
133+
134+
[Fact]
135+
public async Task TestAsyncExpressionLambdaReturnTaskAsync()
136+
{
137+
string testCode = @"
138+
using System;
139+
using System.Linq.Expressions;
140+
using System.Threading.Tasks;
141+
class ClassName
142+
{
143+
static Expression<Func<Task>> ZeroArgumentFunction;
144+
static Expression<Func<object, Task>> SingleArgumentFunction;
145+
146+
ClassName()
147+
{
148+
ZeroArgumentFunction = async () => await Task.Delay(42);
149+
SingleArgumentFunction = async arg => await Task.Delay(42);
150+
SingleArgumentFunction = async (object arg) => await Task.Delay(42);
151+
}
152+
}
153+
";
154+
155+
DiagnosticResult[] expected =
156+
{
157+
this.CSharpDiagnostic(CS1989).WithLocation(12, 32),
158+
this.CSharpDiagnostic(CS1989).WithLocation(13, 34),
159+
this.CSharpDiagnostic(CS1989).WithLocation(14, 34),
160+
};
161+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
162+
}
163+
164+
[Fact]
165+
public async Task TestAsyncDynamicLambdaReturnTaskAsync()
166+
{
167+
string testCode = @"
168+
using System;
169+
using System.Linq.Expressions;
170+
using System.Threading.Tasks;
171+
class ClassName
172+
{
173+
static dynamic ZeroArgumentFunction;
174+
175+
ClassName()
176+
{
177+
ZeroArgumentFunction = async () => await Task.Delay(42);
178+
}
179+
}
180+
";
181+
182+
DiagnosticResult expected = this.CSharpDiagnostic(CS1660).WithArguments("dynamic").WithLocation(11, 32);
183+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
184+
}
185+
103186
[Fact]
104187
public async Task TestAsyncReturnTaskAsync()
105188
{

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/Usage/UseConfigureAwaitUnitTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ async Task MethodNameAsync()
4444
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
4545
}
4646

47+
[Fact]
48+
public async Task TestDynamicExpressionAsync()
49+
{
50+
string testCode = @"
51+
using System.Threading.Tasks;
52+
class ClassName
53+
{
54+
async Task MethodNameAsync()
55+
{
56+
await (dynamic)Task.Delay(1000);
57+
}
58+
}
59+
";
60+
61+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
62+
}
63+
4764
[Fact]
4865
public async Task TestNestedExpressionsAsync()
4966
{

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/Verifiers/DiagnosticVerifier.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public async Task TestEmptySourceAsync()
3030
{
3131
var testCode = string.Empty;
3232
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
33+
34+
testCode =
35+
" \r\n"
36+
+ "\r\n";
37+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3338
}
3439

3540
/// <summary>

AsyncUsageAnalyzers/AsyncUsageAnalyzers/AnalyzerConstants.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ static AnalyzerConstants()
2828
/// <see cref="DiagnosticDescriptor(string, string, string, string, DiagnosticSeverity, bool, string, string, string[])"/>
2929
/// to disable a diagnostic which is currently untested.
3030
/// </value>
31-
[ExcludeFromCodeCoverage]
3231
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1623:Property summary documentation must match accessors.", Justification = "This property behaves more like an opaque value than a Boolean.")]
3332
internal static bool DisabledNoTests { get; }
3433

AsyncUsageAnalyzers/AsyncUsageAnalyzers/GeneratedCodeAnalysisExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private static bool IsEmpty(SyntaxTree tree, CancellationToken cancellationToken
175175
var firstToken = root.GetFirstToken(includeZeroWidth: true);
176176

177177
return firstToken.IsKind(SyntaxKind.EndOfFileToken)
178-
&& TriviaHelper.IndexOfFirstNonWhitespaceTrivia(firstToken.LeadingTrivia) == -1;
178+
&& TriviaHelper.IndexOfFirstNonWhitespaceTrivia(firstToken.LeadingTrivia, true) == -1;
179179
}
180180
}
181181
}

0 commit comments

Comments
 (0)