private void RunDiagnostics([NotNull] AnalyzerTestContext context, [NotNull][ItemNotNull] params string[] messages)
        {
            AnalysisResult result = GetAnalysisResult(context, messages);

            VerifyDiagnosticCount(result, context.DiagnosticsCaptureMode);
            VerifyDiagnostics(result, context);
        }
        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));
        }
        private IList <Diagnostic> GetSortedAnalyzerDiagnostics([NotNull] AnalyzerTestContext context,
                                                                [NotNull] DocumentWithSpans documentWithSpans)
        {
            IEnumerable <Diagnostic> diagnostics =
                EnumerateDiagnosticsForDocument(documentWithSpans.Document, context.ValidationMode,
                                                context.DiagnosticsCaptureMode).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]);
            }
        }
Beispiel #6
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);
            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));
        }