public async Task VerifyNoRefactoringAsync(
            string source,
            IEnumerable <TextSpan> spans,
            string equivalenceKey               = null,
            CodeVerificationOptions options     = null,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (Workspace workspace = new AdhocWorkspace())
            {
                Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options);

                Document document = WorkspaceFactory.AddDocument(project, source);

                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

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

                if (options == null)
                {
                    options = Options;
                }

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                using (IEnumerator <TextSpan> en = spans.GetEnumerator())
                {
                    if (!en.MoveNext())
                    {
                        throw new InvalidOperationException($"'{nameof(spans)}' contains no elements.");
                    }

                    do
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        var context = new CodeRefactoringContext(
                            document,
                            en.Current,
                            a =>
                        {
                            if (equivalenceKey == null ||
                                string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal))
                            {
                                Assert.True(false, "No code refactoring expected.");
                            }
                        },
                            CancellationToken.None);

                        await RefactoringProvider.ComputeRefactoringsAsync(context).ConfigureAwait(false);
                    } while (en.MoveNext());
                }
            }
        }
示例#2
0
        public async Task VerifyNoDiagnosticAsync(
            string source,
            string[] additionalSources          = null,
            CodeVerificationOptions options     = null,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!Analyzer.Supports(Descriptor))
            {
                Assert.True(false, $"Diagnostic \"{Descriptor.Id}\" is not supported by analyzer \"{Analyzer.GetType().Name}\".");
            }

            using (Workspace workspace = new AdhocWorkspace())
            {
                Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options);

                Document document = WorkspaceFactory.AddDocument(project, source, additionalSources ?? Array.Empty <string>());

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

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

                if (options == null)
                {
                    options = Options;
                }

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                if (options.EnableDiagnosticsDisabledByDefault)
                {
                    compilation = compilation.EnableDiagnosticsDisabledByDefault(Analyzer);
                }

                ImmutableArray <Diagnostic> analyzerDiagnostics = await compilation.GetAnalyzerDiagnosticsAsync(Analyzer, DiagnosticComparer.SpanStart, cancellationToken).ConfigureAwait(false);

                foreach (Diagnostic diagnostic in analyzerDiagnostics)
                {
                    if (string.Equals(diagnostic.Id, Descriptor.Id, StringComparison.Ordinal))
                    {
                        Assert.True(false, $"No diagnostic expected{analyzerDiagnostics.Where(f => string.Equals(f.Id, Descriptor.Id, StringComparison.Ordinal)).ToDebugString()}");
                    }
                }
            }
        }
示例#3
0
        public async Task VerifyDiagnosticAsync(
            string source,
            IEnumerable <Diagnostic> expectedDiagnostics,
            string[] additionalSources          = null,
            CodeVerificationOptions options     = null,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (Workspace workspace = new AdhocWorkspace())
            {
                Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options);

                Document document = WorkspaceFactory.AddDocument(project, source, additionalSources ?? Array.Empty <string>());

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

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

                if (options == null)
                {
                    options = Options;
                }

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                if (options.EnableDiagnosticsDisabledByDefault)
                {
                    compilation = compilation.EnableDiagnosticsDisabledByDefault(Analyzer);
                }

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

                if (diagnostics.Length > 0 &&
                    Analyzer.SupportedDiagnostics.Length > 1)
                {
                    VerifyDiagnostics(FilterDiagnostics(diagnostics), expectedDiagnostics, cancellationToken);
                }
                else
                {
                    VerifyDiagnostics(diagnostics, expectedDiagnostics, cancellationToken);
                }
            }

            IEnumerable <Diagnostic> FilterDiagnostics(ImmutableArray <Diagnostic> diagnostics)
            {
                foreach (Diagnostic diagnostic in diagnostics)
                {
                    bool success = false;
                    foreach (Diagnostic expectedDiagnostic in expectedDiagnostics)
                    {
                        if (DiagnosticComparer.Id.Equals(diagnostic, expectedDiagnostic))
                        {
                            success = true;
                            break;
                        }
                    }

                    if (success)
                    {
                        yield return(diagnostic);
                    }
                }
            }
        }
        public async Task VerifyRefactoringAsync(
            string source,
            string expected,
            TextSpan span,
            string[] additionalSources          = null,
            string equivalenceKey               = null,
            CodeVerificationOptions options     = null,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (Workspace workspace = new AdhocWorkspace())
            {
                Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options);

                Document document = WorkspaceFactory.AddDocument(project, source, additionalSources ?? Array.Empty <string>());

                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

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

                if (options == null)
                {
                    options = Options;
                }

                VerifyCompilerDiagnostics(compilerDiagnostics, options);
                CodeAction action = null;

                var context = new CodeRefactoringContext(
                    document,
                    span,
                    a =>
                {
                    if (equivalenceKey == null ||
                        string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal))
                    {
                        if (action == null)
                        {
                            action = a;
                        }
                    }
                },
                    CancellationToken.None);

                await RefactoringProvider.ComputeRefactoringsAsync(context).ConfigureAwait(false);

                Assert.True(action != null, "No code refactoring has been registered.");

                document = await document.ApplyCodeActionAsync(action).ConfigureAwait(false);

                semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                ImmutableArray <Diagnostic> newCompilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken);

                VerifyCompilerDiagnostics(newCompilerDiagnostics, options);

                if (!options.AllowNewCompilerDiagnostics)
                {
                    VerifyNoNewCompilerDiagnostics(compilerDiagnostics, newCompilerDiagnostics, options);
                }

                string actual = await document.ToFullStringAsync(simplify : true, format : true).ConfigureAwait(false);

                Assert.Equal(expected, actual);
            }
        }