コード例 #1
0
        internal async Task TestWithLineFeedOnlyBlankLinesAtEndOfFileAsync(OptionSetting?newlineAtEndOfFile, string result)
        {
            var testCode  = BaseCode + "\n\n";
            var fixedCode = BaseCode + result;

            var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);

            await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
        }
コード例 #2
0
        internal async Task TestCodeFixProviderOnlyStripsTrailingBlankLinesAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            var testCode  = "#if true\r\n" + BaseCode + "\r\n#endif\r\n   \r\n";
            var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;

            var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);

            await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
        }
コード例 #3
0
        internal async Task TestFileEndingWithCommentAndSpuriousWhitespaceAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            var testCode  = BaseCode + "\r\n// Test comment\r\n   \r\n";
            var fixedCode = BaseCode + "\r\n// Test comment" + expectedText;

            var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(9, 16);

            await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
        }
コード例 #4
0
        internal async Task TestFileEndingWithEndIfWithSpuriousWhitespaceAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            var testCode  = "#if true\r\n" + BaseCode + "\r\n#endif\r\n   \r\n";
            var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;

            var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);

            await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
        }
コード例 #5
0
        internal async Task TestFileEndsWithSpacesAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            var testCode  = BaseCode + "\r\n          ";
            var fixedCode = BaseCode + expectedText;

            var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);

            await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
        }
コード例 #6
0
        internal async Task TestWithSingleCarriageReturnLineFeedAtEndOfFileAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            var testCode  = BaseCode + "\r\n";
            var fixedCode = BaseCode + expectedText;

            if (expectedText == null)
            {
                await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
                await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
            }
        }
コード例 #7
0
        private static Task VerifyCSharpDiagnosticAsync(OptionSetting?newlineAtEndOfFile, string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
        {
            var test = new StyleCopCodeFixVerifier <SA1518UseLineEndingsCorrectlyAtEndOfFile, SA1518CodeFixProvider> .CSharpTest
            {
                TestCode = source,
            };

            if (newlineAtEndOfFile != null)
            {
                test.Settings = GetSettings(newlineAtEndOfFile.Value);
            }

            test.ExpectedDiagnostics.AddRange(expected);
            return(test.RunAsync(cancellationToken));
        }
コード例 #8
0
        internal async Task TestFileEndingWithEndIfAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            var testCode  = "#if true\r\n" + BaseCode + "\r\n#endif\r\n";
            var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;

            if (expectedText == null)
            {
                await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);
                await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
            }
        }
コード例 #9
0
        internal async Task TestWithBlankLinesAtEndOfFileAsync(OptionSetting?newlineAtEndOfFile, string result)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode  = BaseCode + "\r\n\r\n";
            var fixedCode = BaseCode + result;

            var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);

            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
        }
コード例 #10
0
        internal async Task TestCodeFixProviderStripsTrailingLinefeedOnlyBlankLinesIncludingWhitespaceAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode  = BaseCode + "\n   \n   \n";
            var fixedCode = BaseCode + expectedText;

            var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);

            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
        }
コード例 #11
0
        private DiagnosticDescriptor GetDescriptor(OptionSetting?endOfFileHandling)
        {
            switch (endOfFileHandling)
            {
            case OptionSetting.Require:
                return(SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorRequire);

            case OptionSetting.Omit:
                return(SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorOmit);

            case OptionSetting.Allow:
            case null:
            default:
                return(SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorAllow);
            }
        }
コード例 #12
0
        public async Task TestOmitWithSystemFirstAsync(string namespaceName)
        {
            this.systemUsingDirectivesFirst   = true;
            this.blankLinesBetweenUsingGroups = OptionSetting.Omit;

            var testCode = @"
using System;

using TestNamespace;

using static System.Math;

using Factory = System.Activator;

namespace TestNamespace
{
}
";

            var fixedTestCode = @"
using System;
using TestNamespace;
using static System.Math;
using Factory = System.Activator;

namespace TestNamespace
{
}
";

            testCode      = WrapWithNamespace(testCode, namespaceName);
            fixedTestCode = WrapWithNamespace(fixedTestCode, namespaceName);
            var offset = GetLineOffset(namespaceName);

            DiagnosticResult[] expected =
            {
                this.CSharpDiagnostic(SA1516ElementsMustBeSeparatedByBlankLine.DescriptorOmit).WithLocation(4 + offset, 1),
                this.CSharpDiagnostic(SA1516ElementsMustBeSeparatedByBlankLine.DescriptorOmit).WithLocation(6 + offset, 1),
                this.CSharpDiagnostic(SA1516ElementsMustBeSeparatedByBlankLine.DescriptorOmit).WithLocation(8 + offset, 1),
            };

            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
        }
コード例 #13
0
        public async Task TestAllowForCompilationUnitAsync(string namespaceName)
        {
            this.blankLinesBetweenUsingGroups = OptionSetting.Allow;

            var testCode = @"
using System;

using TestNamespace;
using static System.Math;

using Factory = System.Activator;
";

            testCode = WrapWithNamespace(testCode, namespaceName);

            await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
コード例 #14
0
        protected internal OrderingSettings(JsonObject orderingSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            ImmutableArray <OrderingTrait> .Builder elementOrder = null;
            bool?systemUsingDirectivesFirst = null;
            UsingDirectivesPlacement?usingDirectivesPlacement     = null;
            OptionSetting?           blankLinesBetweenUsingGroups = null;

            foreach (var kvp in orderingSettingsObject)
            {
                switch (kvp.Key)
                {
                case "elementOrder":
                    kvp.AssertIsArray();
                    elementOrder = ImmutableArray.CreateBuilder <OrderingTrait>();
                    foreach (var value in kvp.Value.AsJsonArray)
                    {
                        elementOrder.Add(value.ToEnumValue <OrderingTrait>(kvp.Key));
                    }

                    break;

                case "systemUsingDirectivesFirst":
                    systemUsingDirectivesFirst = kvp.ToBooleanValue();
                    break;

                case "usingDirectivesPlacement":
                    usingDirectivesPlacement = kvp.ToEnumValue <UsingDirectivesPlacement>();
                    break;

                case "blankLinesBetweenUsingGroups":
                    blankLinesBetweenUsingGroups = kvp.ToEnumValue <OptionSetting>();
                    break;

                default:
                    break;
                }
            }

            systemUsingDirectivesFirst ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "dotnet_sort_system_directives_first");
            usingDirectivesPlacement ??= AnalyzerConfigHelper.TryGetStringValueAndNotification(analyzerConfigOptions, "csharp_using_directive_placement") switch
            {
                ("inside_namespace", _) => UsingDirectivesPlacement.InsideNamespace,
                ("outside_namespace", _) => UsingDirectivesPlacement.OutsideNamespace,
                _ => null,
            };
コード例 #15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LayoutSettings"/> class.
        /// </summary>
        /// <param name="layoutSettingsObject">The JSON object containing the settings.</param>
        /// <param name="analyzerConfigOptions">The <strong>.editorconfig</strong> options to use if
        /// <strong>stylecop.json</strong> does not provide values.</param>
        protected internal LayoutSettings(JsonObject layoutSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            OptionSetting?newlineAtEndOfFile         = null;
            bool?         allowConsecutiveUsings     = null;
            bool?         allowDoWhileOnClosingBrace = null;

            foreach (var kvp in layoutSettingsObject)
            {
                switch (kvp.Key)
                {
                case "newlineAtEndOfFile":
                    newlineAtEndOfFile = kvp.ToEnumValue <OptionSetting>();
                    break;

                case "allowConsecutiveUsings":
                    allowConsecutiveUsings = kvp.ToBooleanValue();
                    break;

                case "allowDoWhileOnClosingBrace":
                    allowDoWhileOnClosingBrace = kvp.ToBooleanValue();
                    break;

                default:
                    break;
                }
            }

            newlineAtEndOfFile ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "insert_final_newline") switch
            {
                true => OptionSetting.Require,
                false => OptionSetting.Omit,
                _ => null,
            };

            allowConsecutiveUsings ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.layout.allowConsecutiveUsings");
            allowDoWhileOnClosingBrace ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.layout.allowDoWhileOnClosingBrace");

            this.newlineAtEndOfFile         = newlineAtEndOfFile.GetValueOrDefault(OptionSetting.Allow);
            this.allowConsecutiveUsings     = allowConsecutiveUsings.GetValueOrDefault(true);
            this.allowDoWhileOnClosingBrace = allowDoWhileOnClosingBrace.GetValueOrDefault(false);
        }
コード例 #16
0
        internal async Task TestFileEndingWithCommentAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode  = BaseCode + "\r\n// Test comment";
            var fixedCode = BaseCode + "\r\n// Test comment" + expectedText;

            if (expectedText == null)
            {
                await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(9, 16);
                await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

                await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

                await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
            }
        }
コード例 #17
0
        public async Task TestOmitWithSeparingCommentsAsync(string namespaceName)
        {
            this.systemUsingDirectivesFirst   = true;
            this.blankLinesBetweenUsingGroups = OptionSetting.Omit;

            var testCode = @"
using System;
// regular group
using TestNamespace;
// static group
using static System.Math;
/* alias group */
using Factory = System.Activator;

namespace TestNamespace
{
}
";

            testCode = WrapWithNamespace(testCode, namespaceName);

            await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
コード例 #18
0
 private static Task VerifyCSharpFixAsync(OptionSetting?newlineAtEndOfFile, string source, DiagnosticResult expected, string fixedSource, CancellationToken cancellationToken)
 => VerifyCSharpFixAsync(newlineAtEndOfFile, source, new[] { expected }, fixedSource, cancellationToken);
コード例 #19
0
        internal async Task TestCodeFixProviderStripsTrailingBlankLinesIncludingWhitespaceAsync(OptionSetting?newlineAtEndOfFile, string expectedText)
        {
            var testCode  = BaseCode + "\r\n   \r\n   \r\n";
            var fixedCode = BaseCode + expectedText;

            var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);

            await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
        }