-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #884 from vweijsters/SA1510
- Loading branch information
Showing
10 changed files
with
446 additions
and
121 deletions.
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1510UnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
namespace StyleCop.Analyzers.Test.LayoutRules | ||
{ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using StyleCop.Analyzers.LayoutRules; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Unit tests for <see cref="SA1510ChainedStatementBlocksMustNotBePrecededByBlankLine"/> | ||
/// </summary> | ||
public class SA1510UnitTests : CodeFixVerifier | ||
{ | ||
/// <summary> | ||
/// Verifies that the analyzer will properly handle an empty source. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestEmptySourceAsync() | ||
{ | ||
var testCode = string.Empty; | ||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that valid chained statements will not produce any diagnostics. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestValidChainedStatementsAsync() | ||
{ | ||
var testCode = @"using System; | ||
namespace Foo | ||
{ | ||
public class Bar | ||
{ | ||
public int TestElseStatement(int x) | ||
{ | ||
if (x > 0) | ||
{ | ||
return -x; | ||
} | ||
else | ||
{ | ||
return x * x; | ||
} | ||
} | ||
public void TestCatchFinallyStatements() | ||
{ | ||
var x = 0; | ||
try | ||
{ | ||
x = x + 1; | ||
} | ||
catch (Exception) | ||
{ | ||
x = 2; | ||
} | ||
finally | ||
{ | ||
x = 3; | ||
} | ||
} | ||
} | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that an else statement preceded by a blank line will report the expected diagnostic. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestInvalidElseStatementAsync() | ||
{ | ||
var testCode = @"using System; | ||
namespace Foo | ||
{ | ||
public class Bar | ||
{ | ||
public int TestElseStatement(int x) | ||
{ | ||
if (x > 0) | ||
{ | ||
return -x; | ||
} | ||
else | ||
{ | ||
return x * x; | ||
} | ||
} | ||
} | ||
} | ||
"; | ||
|
||
var fixedTestCode = @"using System; | ||
namespace Foo | ||
{ | ||
public class Bar | ||
{ | ||
public int TestElseStatement(int x) | ||
{ | ||
if (x > 0) | ||
{ | ||
return -x; | ||
} | ||
else | ||
{ | ||
return x * x; | ||
} | ||
} | ||
} | ||
} | ||
"; | ||
|
||
DiagnosticResult[] expectedDiagnostics = | ||
{ | ||
this.CSharpDiagnostic().WithLocation(14, 13).WithArguments("else") | ||
}; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that an else statement preceded by a blank line will report the expected diagnostic. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestInvalidCatchFinallyStatementsAsync() | ||
{ | ||
var testCode = @"using System; | ||
namespace Foo | ||
{ | ||
public class Bar | ||
{ | ||
public void TestCatchFinallyStatements() | ||
{ | ||
var x = 0; | ||
try | ||
{ | ||
x = x + 1; | ||
} | ||
catch (Exception) | ||
{ | ||
x = 2; | ||
} | ||
finally | ||
{ | ||
x = 3; | ||
} | ||
} | ||
} | ||
} | ||
"; | ||
|
||
var fixedTestCode = @"using System; | ||
namespace Foo | ||
{ | ||
public class Bar | ||
{ | ||
public void TestCatchFinallyStatements() | ||
{ | ||
var x = 0; | ||
try | ||
{ | ||
x = x + 1; | ||
} | ||
catch (Exception) | ||
{ | ||
x = 2; | ||
} | ||
finally | ||
{ | ||
x = 3; | ||
} | ||
} | ||
} | ||
} | ||
"; | ||
|
||
DiagnosticResult[] expectedDiagnostics = | ||
{ | ||
this.CSharpDiagnostic().WithLocation(16, 13).WithArguments("catch"), | ||
this.CSharpDiagnostic().WithLocation(21, 13).WithArguments("finally") | ||
}; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); | ||
} | ||
|
||
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() | ||
{ | ||
yield return new SA1510ChainedStatementBlocksMustNotBePrecededByBlankLine(); | ||
} | ||
|
||
protected override CodeFixProvider GetCSharpCodeFixProvider() | ||
{ | ||
return new SA1510CodeFixProvider(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/LayoutResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.