Example #1
0
        public async Task VerifyCodefixForElsePartOfDirectiveTriviaAsync()
        {
            var testCode = @"namespace NamespaceName
{
#if false
    using System.Runtime.CompilerServices;
#else
    using System.Collections.Generic;
    using System.Collections.Concurrent;
#endif
}
";

            var fixedTestCode = @"namespace NamespaceName
{
#if false
    using System.Runtime.CompilerServices;
#else
    using System.Collections.Concurrent;
    using System.Collections.Generic;
#endif
}
";
            var expected      = StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(6, 5);

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #2
0
        public async Task VerifyUsingReorderingWithoutMovingWithFileHeaderAsync()
        {
            var testCode = @"// This is a file header.

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// This is a file header.

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            var expected = StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(4, 1).WithArguments("System", "Microsoft.CodeAnalysis");

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
        public async Task VerifyRegressionFor2027Async()
        {
            this.disableSA1200 = true;

            var testCode = @"using System;
using System.Reflection;
using System.Net;

namespace MyNamespace
{
    using System.Threading.Tasks;
}
";

            var fixedTestCode = @"using System;
using System.Net;
using System.Reflection;

namespace MyNamespace
{
    using System.Threading.Tasks;
}
";

            var expected = StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(2, 1);

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
        }
Example #4
0
        public async Task VerifyUsingReorderingAsync()
        {
            var testCode = @"using Microsoft.CodeAnalysis;
using SystemAction = System.Action;
using static System.Math;
using System;

using static System.String;
using MyFunc = System.Func<int,bool>;

using System.Collections.Generic;
using System.Collections;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"namespace Foo
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Microsoft.CodeAnalysis;
    using static System.Math;
    using static System.String;
    using MyFunc = System.Func<int,bool>;
    using SystemAction = System.Action;

    public class Bar
    {
    }
}
";

            DiagnosticResult[] expected =
            {
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(1,                                                                                  1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(2,                                                                                  1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(3,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1216UsingStaticDirectivesMustBePlacedAtTheCorrectLocation> .Diagnostic().WithLocation(3,                                                   1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(4,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(4,                      1).WithArguments("System","System.Math"),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(6,                                                                                  1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(7,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1209UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives> .Diagnostic().WithLocation(7,                                               1),
                StyleCopDiagnosticVerifier <SA1211UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName> .Diagnostic().WithLocation(7,                       1).WithArguments("MyFunc","SystemAction"),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(9,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(9,  1).WithArguments("System.Collections.Generic","System.Math"),
                StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(9,                                                   1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(10,                                                                                 1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(10,         1).WithArguments("System.Collections","System.Math"),
            };
            await this.VerifyCSharpFixAsync(testCode, expected, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #5
0
        public async Task VerifyUsingReorderingWithGlobalAttributesAsync()
        {
            var testCode = @"using Microsoft.CodeAnalysis;
using SystemAction = System.Action;
using static System.Math;
using System.Reflection;

using static System.String;
using MyFunc = System.Func<int,bool>;

using System.Collections.Generic;
using System.Collections;

[assembly: AssemblyVersion(""1.0.0.0"")]

namespace NamespaceName
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"using Microsoft.CodeAnalysis;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using static System.Math;
using static System.String;
using MyFunc = System.Func<int,bool>;
using SystemAction = System.Action;

[assembly: AssemblyVersion(""1.0.0.0"")]

namespace NamespaceName
{
    public class Bar
    {
    }
}
";

            DiagnosticResult[] expected =
            {
                StyleCopDiagnosticVerifier <SA1216UsingStaticDirectivesMustBePlacedAtTheCorrectLocation> .Diagnostic().WithLocation(3,                             1),
                StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(4,                             1),
                StyleCopDiagnosticVerifier <SA1209UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives> .Diagnostic().WithLocation(7,                         1),
                StyleCopDiagnosticVerifier <SA1211UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName> .Diagnostic().WithLocation(7, 1).WithArguments("MyFunc","SystemAction"),
                StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(9,                             1),
            };
            await this.VerifyCSharpFixAsync(testCode, expected, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #6
0
        public async Task VerifyUsingReorderingWithoutMovingWithMultiLineFileHeaderAsync()
        {
            var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            var expected = StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(6, 1).WithArguments("System", "Microsoft.CodeAnalysis");

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #7
0
        public async Task VerifyUsingReorderingWithoutMovingWithMultiLineCommentAsync()
        {
            var testCode = @"/*
 * Copyright by FooCorp Inc.
 */

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"/*
 * Copyright by FooCorp Inc.
 */

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            var expected = StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(5, 1);

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #8
0
        public async Task VerifyUsingReorderingWithFileHeaderAsync()
        {
            var testCode = @"// This is a file header.

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// This is a file header.

namespace Foo
{
    using System;
    using Microsoft.CodeAnalysis;

    public class Bar
    {
    }
}
";

            DiagnosticResult[] expected =
            {
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(3,                                                             1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(4,                                                             1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(4, 1).WithArguments("System","Microsoft.CodeAnalysis"),
            };
            await this.VerifyCSharpFixAsync(testCode, expected, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #9
0
        public async Task VerifyCodefixForDirectiveTriviaOutsideOfNamespacesAsync()
        {
            var testCode = @"// <copyright file=""Program.cs"" company=""PlaceholderCompany"" >
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

#if DEBUG
using Fish;
#else
using Fish.Face;
#endif
using System.Text;
using System;

namespace StyleCopBugRepro
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 q;
            Haddock h;
            StringBuilder sb;
        }
    }
}

namespace Fish
{
    public class Haddock { }

    namespace Face
    {
        public class Haddock { }
    }
}
";

            var fixedTestCode = @"// <copyright file=""Program.cs"" company=""PlaceholderCompany"" >
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

#if DEBUG
using Fish;
#else
using Fish.Face;
#endif
using System;
using System.Text;

namespace StyleCopBugRepro
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 q;
            Haddock h;
            StringBuilder sb;
        }
    }
}

namespace Fish
{
    public class Haddock { }

    namespace Face
    {
        public class Haddock { }
    }
}
";

            DiagnosticResult[] expected =
            {
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(8,                                 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(10,                                1),
                StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(10, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(11,                                1),
            };

            DiagnosticResult[] fixedExpected =
            {
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(8,  1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(10, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(11, 1),
            };

            await this.VerifyCSharpFixAsync(testCode, expected, fixedTestCode, fixedExpected, cancellationToken : CancellationToken.None).ConfigureAwait(false);
        }
Example #10
0
        public async Task VerifyUsingReorderingWithMultipleNamespacesAsync()
        {
            var testCode = @"using Microsoft.CodeAnalysis;
using SystemAction = System.Action;
using static System.Math;
using System;

using static System.String;
using MyFunc = System.Func<int,bool>;

using System.Collections.Generic;
using System.Collections;

namespace TestNamespace1
{
    public class TestClass1
    {
    }
}

namespace TestNamespace2
{
}
";

            var fixedTestCode = @"using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using static System.Math;
using static System.String;
using MyFunc = System.Func<int,bool>;
using SystemAction = System.Action;

namespace TestNamespace1
{
    public class TestClass1
    {
    }
}

namespace TestNamespace2
{
}
";

            DiagnosticResult[] expected =
            {
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(1,                                                                                  1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(2,                                                                                  1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(3,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1216UsingStaticDirectivesMustBePlacedAtTheCorrectLocation> .Diagnostic().WithLocation(3,                                                   1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(4,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(4,                      1).WithArguments("System","System.Math"),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(6,                                                                                  1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(7,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1209UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives> .Diagnostic().WithLocation(7,                                               1),
                StyleCopDiagnosticVerifier <SA1211UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName> .Diagnostic().WithLocation(7,                       1).WithArguments("MyFunc","SystemAction"),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(9,                                                                                  1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(9,  1).WithArguments("System.Collections.Generic","System.Math"),
                StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(9,                                                   1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(10,                                                                                 1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(10,         1).WithArguments("System.Collections","System.Math"),
            };

            // The code fix is not able to correct all violations due to the use of multiple namespaces in a single file
            DiagnosticResult[] remainingDiagnostics =
            {
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(1, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(2, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(3, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(4, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(5, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(6, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(7, 1),
                Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorInside).WithLocation(8, 1),
            };

            await this.VerifyCSharpFixAsync(testCode, expected, fixedTestCode, remainingDiagnostics, cancellationToken : CancellationToken.None).ConfigureAwait(false);
        }
Example #11
0
 protected static DiagnosticResult Diagnostic()
 => StyleCopDiagnosticVerifier <SA1402FileMayOnlyContainASingleType> .Diagnostic();
        public async Task VerifyRegressionFor2026Async()
        {
            var testCode = @"// Copyright (c) 2015 ACME, Inc. All rights reserved worldwide.

#if !VS2012

using System;
using System.Runtime.InteropServices;
using System.ComponentModel;

#if VS2008
using System.Collections.ObjectModel;
#elif VS2010
using System.Collections.Concurrent;
#endif

using Math = System.Math;
using Queue = System.Collections.Queue;

namespace Microsoft.VisualStudio.Shell
{
#pragma warning disable SA1200 // Using directives should be placed correctly
    // This is required to work around accessibility issues in documentation comments.
    using NativeMethods = System;
#pragma warning restore SA1200 // Using directives should be placed correctly
}

#endif
";

            var fixedTestCode = @"// Copyright (c) 2015 ACME, Inc. All rights reserved worldwide.

#if !VS2012

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

#if VS2008
using System.Collections.ObjectModel;
#elif VS2010
using System.Collections.Concurrent;
#endif

using Math = System.Math;
using Queue = System.Collections.Queue;

namespace Microsoft.VisualStudio.Shell
{
#pragma warning disable SA1200 // Using directives should be placed correctly
    // This is required to work around accessibility issues in documentation comments.
    using NativeMethods = System;
#pragma warning restore SA1200 // Using directives should be placed correctly
}

#endif
";

            var expected = StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(6, 1);

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
        }