Exemple #1
0
 internal DiagnosticTestState(DiagnosticTestState 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)
 {
 }
        /// <summary>
        /// Verifies that specified source will produce specified diagnostic(s).
        /// </summary>
        /// <param name="state"></param>
        /// <param name="options"></param>
        /// <param name="cancellationToken"></param>
        public async Task VerifyDiagnosticAsync(
            DiagnosticTestState state,
            TestOptions options = null,
            CancellationToken cancellationToken = default)
        {
            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, state.Source, state.AdditionalFiles, options);

                SyntaxTree tree = await document.GetSyntaxTreeAsync();

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

                VerifySupportedDiagnostics(analyzer, expectedDiagnostics);

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

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

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                compilation = UpdateCompilation(compilation, expectedDiagnostics);

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

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