protected void AssertDiagnostics([NotNull] AnalyzerTestContext context, [NotNull][ItemNotNull] params string[] messages)
        {
            Guard.NotNull(context, nameof(context));
            Guard.NotNull(messages, nameof(messages));

            RunDiagnostics(context, messages);
        }
        private AnalysisResult GetAnalysisResult([NotNull] AnalyzerTestContext context, [NotNull][ItemNotNull] string[] messages)
        {
            DocumentWithSpans documentWithSpans = DocumentFactory.GetDocumentWithSpansFromMarkup(context);

            IList <Diagnostic>        diagnostics = GetSortedAnalyzerDiagnostics(context, documentWithSpans);
            ImmutableArray <TextSpan> spans       = documentWithSpans.TextSpans.OrderBy(s => s).ToImmutableArray();

            return(new AnalysisResult(diagnostics, spans, messages));
        }
示例#3
0
        public FixProviderTestContext([NotNull] AnalyzerTestContext analyzerTestContext,
                                      [NotNull][ItemNotNull] IEnumerable <string> expected, bool reformatExpected = true)
        {
            Guard.NotNull(analyzerTestContext, nameof(analyzerTestContext));
            Guard.NotNull(expected, nameof(expected));

            AnalyzerTestContext = analyzerTestContext;
            Expected            = ImmutableList.CreateRange(expected);
            ReformatExpected    = reformatExpected;
        }
        private AnalysisResult RunDiagnostics([NotNull] AnalyzerTestContext context,
                                              [NotNull][ItemNotNull] params string[] messages)
        {
            AnalysisResult result = GetAnalysisResult(context, messages);

            VerifyDiagnosticCount(result, context.DiagnosticsCaptureMode);
            VerifyDiagnostics(result, context);

            return(result);
        }
示例#5
0
        private string RemoveMarkupFrom([NotNull] AnalyzerTestContext context, bool reformat)
        {
            Document   document   = GetDocumentWithSpansFromMarkup(context).Document;
            SyntaxNode syntaxRoot = document.GetSyntaxRootAsync().Result;

            if (reformat)
            {
                SyntaxNode formattedSyntaxRoot = Formatter.Format(syntaxRoot, document.Project.Solution.Workspace);
                return(formattedSyntaxRoot.ToFullString());
            }

            return(syntaxRoot.ToFullString());
        }
        private IList <Diagnostic> GetSortedAnalyzerDiagnostics([NotNull] AnalyzerTestContext context,
                                                                [NotNull] DocumentWithSpans documentWithSpans)
        {
            IEnumerable <Diagnostic> diagnostics = EnumerateDiagnosticsForDocument(documentWithSpans.Document,
                                                                                   context.ValidationMode, context.DiagnosticsCaptureMode, context.Options).Where(d => d.Id == DiagnosticId);

            if (context.DiagnosticsCaptureMode == DiagnosticsCaptureMode.RequireInSourceTree)
            {
                diagnostics = diagnostics.OrderBy(d => d.Location.SourceSpan);
            }

            return(diagnostics.ToImmutableArray());
        }
        private static void VerifyDiagnostics([NotNull] AnalysisResult result, [NotNull] AnalyzerTestContext context)
        {
            for (int index = 0; index < result.Diagnostics.Count; index++)
            {
                Diagnostic diagnostic = result.Diagnostics[index];

                if (context.DiagnosticsCaptureMode == DiagnosticsCaptureMode.RequireInSourceTree)
                {
                    VerifyDiagnosticLocation(diagnostic, result.Spans[index]);
                }

                diagnostic.GetMessage().Should().Be(result.Messages[index]);
            }
        }
示例#8
0
        public DocumentWithSpans GetDocumentWithSpansFromMarkup([NotNull] AnalyzerTestContext context)
        {
            Guard.NotNull(context, nameof(context));

            var           parser        = new MarkupParser(context.MarkupCode);
            CodeWithSpans codeWithSpans = parser.Parse();

            ParseOptions parseOptions = GetParseOptions(context.DocumentationMode, context.LanguageName,
                                                        context.OperationFeature);
            CompilationOptions compilationOptions = GetCompilationOptions(context.CompilerWarningLevel, context.LanguageName);

            Document document = new AdhocWorkspace()
                                .AddProject(context.AssemblyName, context.LanguageName)
                                .WithParseOptions(parseOptions)
                                .WithCompilationOptions(compilationOptions)
                                .AddMetadataReferences(context.References)
                                .AddDocument(context.FileName, codeWithSpans.Code);

            return(new DocumentWithSpans(document, codeWithSpans.Spans));
        }