Example #1
0
        internal async Task VerifyRefactoringAsync(
            string source,
            string expected,
            TextSpan span,
            IEnumerable <string> additionalSources = null,
            string title                        = null,
            string equivalenceKey               = null,
            CodeVerificationOptions options     = null,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            options ??= Options;

            using (Workspace workspace = new AdhocWorkspace())
            {
                Document document = WorkspaceFactory.CreateDocument(workspace.CurrentSolution, source, additionalSources, options);

                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken);

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

                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);

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

                document = await VerifyAndApplyCodeActionAsync(document, action, title);

                semanticModel = await document.GetSemanticModelAsync(cancellationToken);

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

                VerifyCompilerDiagnostics(newCompilerDiagnostics, options);

                VerifyNoNewCompilerDiagnostics(compilerDiagnostics, newCompilerDiagnostics, options);

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

                Assert.Equal(expected, actual);
            }
        }
Example #2
0
        internal async Task VerifyDiagnosticAsync(
            string source,
            IEnumerable <Diagnostic> expectedDiagnostics,
            IEnumerable <string> additionalSources = null,
            CodeVerificationOptions options        = null,
            CancellationToken cancellationToken    = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            options ??= Options;

            using (Workspace workspace = new AdhocWorkspace())
            {
                Document document = WorkspaceFactory.CreateDocument(workspace.CurrentSolution, source, additionalSources, options);

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

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

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                compilation = UpdateCompilation(compilation);

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

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

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

                    if (success)
                    {
                        yield return(diagnostic);
                    }
                }
            }
        }
Example #3
0
        internal async Task VerifyNoRefactoringAsync(
            string source,
            IEnumerable <TextSpan> spans,
            string equivalenceKey               = null,
            CodeVerificationOptions options     = null,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            options ??= Options;

            using (Workspace workspace = new AdhocWorkspace())
            {
                Document document = WorkspaceFactory.CreateDocument(workspace.CurrentSolution, source, options);

                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken);

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

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                using (IEnumerator <TextSpan> en = spans.GetEnumerator())
                {
                    if (!en.MoveNext())
                    {
                        Assert.True(false, "Span on which a refactoring should be invoked was not found.");
                    }

                    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);
                    } while (en.MoveNext());
                }
            }
        }
Example #4
0
        /// <summary>
        /// Verifies that specified source will not produce diagnostic described with see <see cref="Descriptor"/>
        /// </summary>
        /// <param name="source">A source code that should be tested. Tokens [| and |] represents start and end of selection respectively.</param>
        /// <param name="additionalSources"></param>
        /// <param name="options"></param>
        /// <param name="cancellationToken"></param>
        public async Task VerifyNoDiagnosticAsync(
            string source,
            IEnumerable <string> additionalSources = null,
            CodeVerificationOptions options        = null,
            CancellationToken cancellationToken    = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            options ??= Options;

            if (SupportedDiagnostics.IndexOf(Descriptor, DiagnosticDescriptorComparer.Id) == -1)
            {
                Assert.True(false, $"Diagnostic \"{Descriptor.Id}\" is not supported by analyzer(s) {string.Join(", ", Analyzers.Select(f => f.GetType().Name))}.");
            }

            using (Workspace workspace = new AdhocWorkspace())
            {
                Document document = WorkspaceFactory.CreateDocument(workspace.CurrentSolution, source, additionalSources, options);

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

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

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                compilation = UpdateCompilation(compilation);

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

                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()}");
                    }
                }
            }
        }