diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200UnitTests.cs new file mode 100644 index 000000000..c7dbf8e36 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200UnitTests.cs @@ -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; + + /// + /// Unit tests for the + /// + 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();"; + + /// + /// Verify that the analyzer accepts an empty source. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task TestEmptySourceAsync() + { + var testCode = string.Empty; + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + + /// + /// Verifies that valid using statements in a namespace does not produce any diagnostics. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task TestValidUsingStatementsInNamespaceAsync() + { + var testCode = @"namespace TestNamespace +{ + using System; + using System.Threading; +} +"; + + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); + } + + /// + /// Verifies that having using statements in the compilation unit will not produce any diagnostics when there are type definition present. + /// + /// The type definition to test. + /// A representing the asynchronous unit test. + [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); + } + + /// + /// Verifies that having using statements in the compilation unit will not produce any diagnostics when there are attributes present. + /// + /// A representing the asynchronous unit test. + [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); + } + + /// + /// Verifies that having using statements in the compilation unit will produce the expected diagnostics. + /// + /// A representing the asynchronous unit test. + [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 GetCSharpDiagnosticAnalyzers() + { + yield return new SA1200UsingDirectivesMustBePlacedWithinNamespace(); + } + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj b/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj index 381c508ad..4b5312a5b 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj @@ -235,6 +235,7 @@ + diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1200UsingDirectivesMustBePlacedWithinNamespace.cs b/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1200UsingDirectivesMustBePlacedWithinNamespace.cs index 4970ebb4a..43dac6908 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1200UsingDirectivesMustBePlacedWithinNamespace.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1200UsingDirectivesMustBePlacedWithinNamespace.cs @@ -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 SupportedDiagnosticsValue = ImmutableArray.Create(Descriptor);