Skip to content

Commit

Permalink
Merge pull request #903 from vweijsters/SA1200-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed May 31, 2015
2 parents 5aef39c + 8042bca commit 96a902b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
<Compile Include="NamingRules\SA1311UnitTests.cs" />
<Compile Include="NamingRules\SX1309SUnitTests.cs" />
<Compile Include="NamingRules\SX1309UnitTests.cs" />
<Compile Include="OrderingRules\SA1200UnitTests.cs" />
<Compile Include="OrderingRules\SA1201UnitTests.cs" />
<Compile Include="OrderingRules\SA1205UnitTests.cs" />
<Compile Include="OrderingRules\SA1208UnitTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public class SA1200UsingDirectivesMustBePlacedWithinNamespace : DiagnosticAnalyz
private const string HelpLink = "http://www.stylecop.com/docs/SA1200.html";

private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, AnalyzerConstants.DisabledNoTests, Description, HelpLink);
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

private static readonly ImmutableArray<DiagnosticDescriptor> SupportedDiagnosticsValue =
ImmutableArray.Create(Descriptor);
Expand Down

0 comments on commit 96a902b

Please sign in to comment.