Exemple #1
0
        public ICollection <string> RemoveMarkupFrom([NotNull] FixProviderTestContext context)
        {
            Guard.NotNull(context, nameof(context));

            return(context.Expected
                   .Select(text => RemoveMarkupFrom(context.AnalyzerTestContext.WithMarkupCode(text), context.ReformatExpected))
                   .ToList());
        }
Exemple #2
0
        private void RunCodeFixes([NotNull] FixProviderTestContext context, [NotNull] Diagnostic diagnostic,
                                  [NotNull] CodeFixProvider fixProvider)
        {
            for (int index = 0; index < context.Expected.Count; index++)
            {
                Document document = DocumentFactory.GetDocumentWithSpansFromMarkup(context.AnalyzerTestContext).Document;

                ImmutableArray <CodeAction> codeFixes = GetCodeFixesForDiagnostic(diagnostic, document, fixProvider);
                codeFixes.Should().HaveSameCount(context.Expected);

                VerifyCodeAction(codeFixes[index], document, context.Expected[index]);
            }
        }
        protected void VerifyNullabilityFix([NotNull] ParsedSourceCode source)
        {
            Guard.NotNull(source, nameof(source));

            string fixNotNull = source.GetExpectedTextForAttribute(NotNullAttributeName);
            string fixCanBeNull = source.GetExpectedTextForAttribute(CanBeNullAttributeName);

            AnalyzerTestContext analyzeTextContext = new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp)
                .WithReferences(source.References)
                .WithFileName(source.Filename);
            var fixTestContext = new FixProviderTestContext(analyzeTextContext, new[] { fixNotNull, fixCanBeNull },
                source.ReIndentExpected);

            AssertDiagnosticsWithCodeFixes(fixTestContext);
        }
        protected void VerifyDiagnosticWithFix([NotNull] ParsedSourceCode source)
        {
            Guard.NotNull(source, nameof(source));

            AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings);

            AnalyzerTestContext analyzeContext = new AnalyzerTestContext(source.GetText(), LanguageNames.CSharp, options)
                .WithReferences(source.References)
                .WithFileName(source.Filename);

            var fixTestContext = new FixProviderTestContext(analyzeContext, new[] { string.Empty },
                source.ReIndentExpected);

            AssertDiagnosticsWithCodeFixes(fixTestContext);
        }
Exemple #5
0
        private void RunCodeFixes([NotNull] FixProviderTestContext context, [NotNull] Diagnostic diagnostic,
                                  [NotNull] CodeFixProvider fixProvider)
        {
            for (int index = 0; index < context.Expected.Count; index++)
            {
                Document document =
                    TestHelpers.GetDocumentAndSpansFromMarkup(context.AnalyzerTestContext.MarkupCode,
                                                              context.AnalyzerTestContext.LanguageName, context.AnalyzerTestContext.References,
                                                              context.AnalyzerTestContext.FileName).Document;

                ImmutableArray <CodeAction> codeFixes = GetCodeFixesForDiagnostic(diagnostic, document, fixProvider);
                codeFixes.Should().HaveCount(context.Expected.Count);

                Verify.CodeAction(codeFixes[index], document, context.Expected[index]);
            }
        }
Exemple #6
0
        protected void AssertDiagnosticsWithCodeFixes([NotNull] FixProviderTestContext context,
                                                      [NotNull][ItemNotNull] params string[] messages)
        {
            Guard.NotNull(context, nameof(context));
            Guard.NotNull(messages, nameof(messages));

            AnalysisResult analysisResult = RunDiagnostics(context.AnalyzerTestContext, messages);

            ICollection <string> expectedCode = DocumentFactory.RemoveMarkupFrom(context);

            context = context.WithExpected(expectedCode);

            CodeFixProvider fixProvider = CreateFixProvider();

            foreach (Diagnostic diagnostic in analysisResult.Diagnostics)
            {
                RunCodeFixes(context, diagnostic, fixProvider);
            }
        }
Exemple #7
0
        protected void AssertDiagnosticsWithCodeFixes([NotNull] FixProviderTestContext context)
        {
            Guard.NotNull(context, nameof(context));

            ImmutableArray <Diagnostic> diagnostics = RunDiagnostics(context.AnalyzerTestContext);

            IList <string> expectedCode = TestHelpers.RemoveMarkupFrom(context.Expected,
                                                                       context.AnalyzerTestContext.LanguageName, context.ReformatExpected,
                                                                       context.AnalyzerTestContext.References, context.AnalyzerTestContext.FileName);

            context = context.WithExpected(expectedCode);

            CodeFixProvider fixProvider = CreateFixProvider();

            foreach (Diagnostic diagnostic in diagnostics)
            {
                RunCodeFixes(context, diagnostic, fixProvider);
            }
        }
        protected virtual void VerifyNullabilityFix([NotNull] ParsedSourceCode source)
        {
            Guard.NotNull(source, nameof(source));

            externalAnnotationsResolver = new SimpleExternalAnnotationsResolver(source.ExternalAnnotationsMap);

            string fixNotNull = source.GetExpectedTextForAttribute(NotNullAttributeName);
            string fixCanBeNull = source.GetExpectedTextForAttribute(CanBeNullAttributeName);

            string text = source.GetText();
            AnalyzerOptions options = AnalyzerSettingsBuilder.ToOptions(source.Settings);

            AnalyzerTestContext analyzerContext = new AnalyzerTestContext(text, LanguageNames.CSharp, options)
                .WithReferences(source.References)
                .WithFileName(source.Filename);

            var fixContext = new FixProviderTestContext(analyzerContext, new[] { fixNotNull, fixCanBeNull },
                source.ReIndentExpected);

            AssertDiagnosticsWithCodeFixes(fixContext);
        }