Пример #1
0
        public static void VerifyNoFix(
            string source,
            DiagnosticAnalyzer analyzer,
            CodeFixProvider fixProvider,
            string language)
        {
            Document document = WorkspaceFactory.CreateDocument(source, language);

            DiagnosticVerifier.VerifyNoCompilerError(document);

            foreach (Diagnostic diagnostic in DiagnosticUtility.GetSortedDiagnostics(document, analyzer))
            {
                List <CodeAction> actions = null;

                var context = new CodeFixContext(
                    document,
                    diagnostic,
                    (a, _) => (actions ?? (actions = new List <CodeAction>())).Add(a),
                    CancellationToken.None);

                fixProvider.RegisterCodeFixesAsync(context).Wait();

                Assert.True(actions == null, $"Expected no code fix, actual: {actions.Count}.");
            }
        }
Пример #2
0
        public static void VerifyNoRefactoring(
            string source,
            TextSpan span,
            CodeRefactoringProvider refactoringProvider,
            string language,
            string equivalenceKey = null)
        {
            Document document = WorkspaceFactory.CreateDocument(source, language);

            DiagnosticVerifier.VerifyNoCompilerError(document);

            List <CodeAction> actions = null;

            var context = new CodeRefactoringContext(
                document,
                span,
                codeAction =>
            {
                if (equivalenceKey == null ||
                    string.Equals(codeAction.EquivalenceKey, equivalenceKey, StringComparison.Ordinal))
                {
                    (actions ?? (actions = new List <CodeAction>())).Add(codeAction);
                }
            },
                CancellationToken.None);

            refactoringProvider.ComputeRefactoringsAsync(context).Wait();

            Assert.True(actions == null, $"Expected no code refactoring, actual: {actions?.Count ?? 0}");
        }
Пример #3
0
        private static Document VerifyRefactoring(
            Document document,
            TextSpan span,
            CodeRefactoringProvider refactoringProvider,
            string equivalenceKey,
            bool allowNewCompilerDiagnostics)
        {
            ImmutableArray <Diagnostic> compilerDiagnostics = document.GetCompilerDiagnostics();

            DiagnosticVerifier.VerifyNoCompilerError(compilerDiagnostics);

            List <CodeAction> actions = null;

            var context = new CodeRefactoringContext(
                document,
                span,
                a =>
            {
                if (equivalenceKey == null ||
                    string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal))
                {
                    (actions ?? (actions = new List <CodeAction>())).Add(a);
                }
            },
                CancellationToken.None);

            refactoringProvider.ComputeRefactoringsAsync(context).Wait();

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

            document = document.ApplyCodeAction(actions[0]);

            if (!allowNewCompilerDiagnostics)
            {
                DiagnosticVerifier.VerifyNoNewCompilerDiagnostics(document, compilerDiagnostics);
            }

            return(document);
        }
Пример #4
0
        public static void VerifyFix(
            string source,
            string newSource,
            DiagnosticAnalyzer analyzer,
            CodeFixProvider fixProvider,
            string language,
            bool allowNewCompilerDiagnostics = false)
        {
            Assert.True(fixProvider.CanFixAny(analyzer.SupportedDiagnostics), $"Code fix provider '{fixProvider.GetType().Name}' cannot fix any diagnostic supported by analyzer '{analyzer}'.");

            Document document = WorkspaceFactory.CreateDocument(source, language);

            ImmutableArray <Diagnostic> compilerDiagnostics = document.GetCompilerDiagnostics();

            DiagnosticVerifier.VerifyNoCompilerError(compilerDiagnostics);

            Diagnostic[] analyzerDiagnostics = DiagnosticUtility.GetSortedDiagnostics(document, analyzer);

            while (analyzerDiagnostics.Length > 0)
            {
                Diagnostic diagnostic = null;

                foreach (Diagnostic analyzerDiagnostic in analyzerDiagnostics)
                {
                    if (fixProvider.FixableDiagnosticIds.Contains(analyzerDiagnostic.Id))
                    {
                        diagnostic = analyzerDiagnostic;
                        break;
                    }
                }

                if (diagnostic == null)
                {
                    break;
                }

                List <CodeAction> actions = null;

                var context = new CodeFixContext(
                    document,
                    diagnostic,
                    (a, _) => (actions ?? (actions = new List <CodeAction>())).Add(a),
                    CancellationToken.None);

                fixProvider.RegisterCodeFixesAsync(context).Wait();

                if (actions == null)
                {
                    break;
                }

                document = document.ApplyCodeAction(actions[0]);

                if (!allowNewCompilerDiagnostics)
                {
                    DiagnosticVerifier.VerifyNoNewCompilerDiagnostics(document, compilerDiagnostics);
                }

                analyzerDiagnostics = DiagnosticUtility.GetSortedDiagnostics(document, analyzer);
            }

            string actual = document.ToSimplifiedAndFormattedFullString();

            Assert.Equal(newSource, actual);
        }