-
Notifications
You must be signed in to change notification settings - Fork 508
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 #903 from vweijsters/SA1200-tests
- Loading branch information
Showing
3 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200UnitTests.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,134 @@ | ||
namespace StyleCop.Analyzers.Test.OrderingRules | ||
{ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using StyleCop.Analyzers.OrderingRules; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Unit tests for the <see cref="SA1200UsingDirectivesMustBePlacedWithinNamespace"/> | ||
/// </summary> | ||
public class SA1200UnitTests : DiagnosticVerifier | ||
{ | ||
private const string ClassDefinition = @"public class TestClass | ||
{ | ||
}"; | ||
|
||
private const string StructDefinition = @"public struct TestStruct | ||
{ | ||
}"; | ||
|
||
private const string InterfaceDefinition = @"public interface TestInterface | ||
{ | ||
}"; | ||
|
||
private const string EnumDefinition = @"public enum TestEnum | ||
{ | ||
TestValue | ||
}"; | ||
|
||
private const string DelegateDefinition = @"public delegate void TestDelegate();"; | ||
|
||
/// <summary> | ||
/// Verify that the analyzer accepts 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 using statements in a namespace does not produce any diagnostics. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestValidUsingStatementsInNamespaceAsync() | ||
{ | ||
var testCode = @"namespace TestNamespace | ||
{ | ||
using System; | ||
using System.Threading; | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that having using statements in the compilation unit will not produce any diagnostics when there are type definition present. | ||
/// </summary> | ||
/// <param name="typeDefinition">The type definition to test.</param> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Theory] | ||
[InlineData(ClassDefinition)] | ||
[InlineData(StructDefinition)] | ||
[InlineData(InterfaceDefinition)] | ||
[InlineData(EnumDefinition)] | ||
[InlineData(DelegateDefinition)] | ||
public async Task TestValidUsingStatementsInCompilationUnitWithTypeDefinitionAsync(string typeDefinition) | ||
{ | ||
var testCode = $@"using System; | ||
{typeDefinition} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that having using statements in the compilation unit will not produce any diagnostics when there are attributes present. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestValidUsingStatementsInCompilationUnitWithAttributesAsync() | ||
{ | ||
var testCode = @"using System.Reflection; | ||
[assembly: AssemblyVersion(""1.0.0.0"")] | ||
namespace TestNamespace | ||
{ | ||
using System; | ||
using System.Threading; | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that having using statements in the compilation unit will produce the expected diagnostics. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestInvalidUsingStatementsInCompilationUnitAsync() | ||
{ | ||
var testCode = @"using System; | ||
using System.Threading; | ||
namespace TestNamespace | ||
{ | ||
} | ||
"; | ||
|
||
DiagnosticResult[] expectedResults = | ||
{ | ||
this.CSharpDiagnostic().WithLocation(1, 1), | ||
this.CSharpDiagnostic().WithLocation(2, 1) | ||
}; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() | ||
{ | ||
yield return new SA1200UsingDirectivesMustBePlacedWithinNamespace(); | ||
} | ||
} | ||
} |
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