Example #1
0
 internal DiagnosticTestData(DiagnosticTestData other)
     : this(
         descriptor : other.Descriptor,
         source : other.Source,
         spans : other.Spans,
         additionalSpans : other.AdditionalSpans,
         additionalFiles : other.AdditionalFiles,
         diagnosticMessage : other.DiagnosticMessage,
         formatProvider : other.FormatProvider,
         equivalenceKey : other.EquivalenceKey,
         alwaysVerifyAdditionalLocations : other.AlwaysVerifyAdditionalLocations)
 {
 }
Example #2
0
        /// <summary>
        /// Verifies that specified source will produce specified diagnostic(s).
        /// </summary>
        /// <param name="data"></param>
        /// <param name="options"></param>
        /// <param name="cancellationToken"></param>
        public async Task VerifyDiagnosticAsync(
            DiagnosticTestData data,
            TestOptions options = null,
            CancellationToken cancellationToken = default)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            cancellationToken.ThrowIfCancellationRequested();

            options ??= Options;

            TAnalyzer analyzer = Activator.CreateInstance <TAnalyzer>();
            ImmutableArray <DiagnosticDescriptor> supportedDiagnostics = analyzer.SupportedDiagnostics;

            using (Workspace workspace = new AdhocWorkspace())
            {
                (Document document, ImmutableArray <ExpectedDocument> expectedDocuments) = CreateDocument(workspace.CurrentSolution, data.Source, data.AdditionalFiles, options, data.Descriptor);

                SyntaxTree tree = await document.GetSyntaxTreeAsync();

                ImmutableArray <Diagnostic> expectedDiagnostics = data.GetDiagnostics(tree);

                VerifySupportedDiagnostics(analyzer, expectedDiagnostics);

                Compilation compilation = await document.Project.GetCompilationAsync(cancellationToken);

                ImmutableArray <Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(cancellationToken);

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                ImmutableArray <Diagnostic> diagnostics = await GetAnalyzerDiagnosticsAsync(compilation, analyzer, DiagnosticComparer.SpanStart, cancellationToken);

                if (diagnostics.Length > 0 &&
                    supportedDiagnostics.Length > 1)
                {
                    VerifyDiagnostics(data, analyzer, expectedDiagnostics, FilterDiagnostics(diagnostics, expectedDiagnostics), cancellationToken);
                }
                else
                {
                    VerifyDiagnostics(data, analyzer, expectedDiagnostics, diagnostics, cancellationToken);
                }

                if (expectedDocuments.Any())
                {
                    await VerifyAdditionalDocumentsAsync(document.Project, expectedDocuments, cancellationToken);
                }
            }