public static RoslynTestKitException DiagnosticNotFound(string diagnosticId, IDiagnosticLocator locator, Diagnostic[] reportedDiagnostics)
        {
            var reportedDiagnosticInfo = reportedDiagnostics.MergeWithComma(x => x.Id, title: "Reported issues: ");
            var message = $"There is no issue reported for {diagnosticId} at {locator.Description()}.{reportedDiagnosticInfo}";

            return(new RoslynTestKitException(message));
        }
        private void NoCodeFix(Document document, Diagnostic diagnostic, IDiagnosticLocator locator)
        {
            var codeFixes = GetCodeFixes(document, diagnostic);

            if (codeFixes.Length != 0)
            {
                throw RoslynTestKitException.UnexpectedCodeFixFound(codeFixes, locator);
            }
        }
        private void TestNoCodeRefactoring(Document document, IDiagnosticLocator locator)
        {
            var codeRefactorings = GetCodeRefactorings(document, locator).ToImmutableArray();

            if (codeRefactorings.Length != 0)
            {
                throw RoslynTestKitException.UnexpectedCodeRefactorings(codeRefactorings);
            }
        }
        private ImmutableArray <CodeAction> GetCodeRefactorings(Document document, IDiagnosticLocator locator)
        {
            var builder  = ImmutableArray.CreateBuilder <CodeAction>();
            var context  = new CodeRefactoringContext(document, locator.GetSpan(), a => builder.Add(a), CancellationToken.None);
            var provider = CreateProvider();

            provider.ComputeRefactoringsAsync(context).Wait();
            return(builder.ToImmutable());
        }
        private void HasDiagnostic(Document document, string diagnosticId, IDiagnosticLocator locator)
        {
            var reporteddiagnostics = GetDiagnostics(document).Where(d => locator.Match(d.Location)).ToArray();
            var matchedDiagnostics  = reporteddiagnostics.Count(d => d.Id == diagnosticId);

            if (matchedDiagnostics == 0)
            {
                throw RoslynTestKitException.DiagnosticNotFound(diagnosticId, locator, reporteddiagnostics);
            }
        }
        private void TestCodeRefactoring(Document document, string expected, IDiagnosticLocator locator, int refactoringIndex = 0)
        {
            var codeRefactorings = GetCodeRefactorings(document, locator);

            if (codeRefactorings.Length < refactoringIndex + 1)
            {
                throw RoslynTestKitException.CodeRefactoringNotFound(refactoringIndex, codeRefactorings, locator);
            }
            Verify.CodeAction(codeRefactorings[refactoringIndex], document, expected);
        }
Beispiel #7
0
        private void VerifyExpectations(Document document, IDiagnosticLocator locator, CompletionTrigger?trigger, Action <ImmutableArray <CompletionItem> > assertion)
        {
            var selectedTrigger = trigger ?? CompletionTrigger.Default;
            var provider        = CreateProvider();
            var span            = locator.GetSpan();
            var options         = document.GetOptionsAsync(CancellationToken.None).GetAwaiter().GetResult();
            var service         = new TestCompletionService(document.Project.Solution.Workspace, LanguageName, provider);
            var result          = service.GetCompletionsAsync(document, span.Start, selectedTrigger, ImmutableHashSet <string> .Empty, options, CancellationToken.None).GetAwaiter().GetResult();

            assertion(result.Items);
        }
        private Diagnostic GetDiagnostic(Document document, string diagnosticId, IDiagnosticLocator locator)
        {
            var reportedDiagnostics = GetReportedDiagnostics(document, locator).ToArray();
            var diagnostic          = reportedDiagnostics.FirstOrDefault(x => x.Id == diagnosticId);

            if (diagnostic == null)
            {
                throw RoslynTestKitException.DiagnosticNotFound(diagnosticId, locator, reportedDiagnostics);
            }

            return(diagnostic);
        }
        private ImmutableArray <CodeAction> GetCodeFixes(Document document, IDiagnosticLocator locator, DiagnosticDescriptor descriptor)
        {
            var builder    = ImmutableArray.CreateBuilder <CodeAction>();
            var diagnostic = FindOrCreateDiagnosticForDescriptor(document, descriptor, locator);
            var context    = new CodeFixContext(document, diagnostic, (a, _) => builder.Add(a), CancellationToken.None);

            var provider = CreateProvider();

            provider.RegisterCodeFixesAsync(context).Wait();

            return(builder.ToImmutable());
        }
        protected void NoDiagnostic(Document document, string diagnosticId, IDiagnosticLocator locator = null)
        {
            var diagnostics = GetDiagnostics(document);

            if (locator != null)
            {
                diagnostics = diagnostics.Where(x => locator.Match(x.Location)).ToImmutableArray();
            }
            var unexpectedDiagnostics = diagnostics.Where(d => d.Id == diagnosticId).ToList();

            if (unexpectedDiagnostics.Count > 0)
            {
                throw RoslynTestKitException.UnexpectedDiagnostic(unexpectedDiagnostics);
            }
        }
        private IEnumerable <Diagnostic> GetReportedDiagnostics(Document document, IDiagnosticLocator locator)
        {
            var allReportedDiagnostics = GetAllReportedDiagnostics(document);

            foreach (var diagnostic in allReportedDiagnostics)
            {
                if (locator.Match(diagnostic.Location))
                {
                    yield return(diagnostic);
                }
                else if (ThrowsWhenInputDocumentContainsError && diagnostic.Severity == DiagnosticSeverity.Error)
                {
                    throw new InvalidOperationException($"Input document contains unexpected error: {diagnostic.GetMessage()}");
                }
            }
        }
Beispiel #12
0
        private void TestCodeRefactoring(Document document, string expected, IDiagnosticLocator locator, int refactoringIndex = 0)
        {
            var codeRefactorings = GetCodeRefactorings(document, locator);

            if (codeRefactorings.Length < refactoringIndex + 1)
            {
                throw RoslynTestKitException.CodeRefactoringNotFound(refactoringIndex, codeRefactorings, locator);
            }

            if (FailWhenInputContainsErrors)
            {
                var errors = document.GetErrors();
                if (errors.Count > 0)
                {
                    throw RoslynTestKitException.UnexpectedErrorDiagnostic(errors);
                }
            }

            Verify.CodeAction(codeRefactorings[refactoringIndex], document, expected);
        }
        private void TestCodeRefactoring(Document document, string expected, IDiagnosticLocator locator, ICodeActionSelector codeActionSelector)
        {
            if (FailWhenInputContainsErrors)
            {
                var errors = document.GetErrors();
                if (errors.Count > 0)
                {
                    throw RoslynTestKitException.UnexpectedErrorDiagnostic(errors);
                }
            }

            var codeRefactorings    = GetCodeRefactorings(document, locator);
            var selectedRefactoring = codeActionSelector.Find(codeRefactorings);

            if (selectedRefactoring is null)
            {
                throw RoslynTestKitException.CodeRefactoringNotFound(codeActionSelector, codeRefactorings, locator);
            }

            Verify.CodeAction(selectedRefactoring, document, expected);
        }
Beispiel #14
0
        private static Action <ImmutableArray <CompletionItem> > CreateAssertionBasedOnExpectedSet(string[] expectedCompletions, IDiagnosticLocator locator)
        {
            return((items) =>
            {
                var allFoundCompletionText = items.Select(x => x.DisplayText);
                var missingSuggestions = expectedCompletions.Except(allFoundCompletionText).ToList();

                if (missingSuggestions.Count > 0)
                {
                    throw RoslynTestKitException.CannotFindSuggestion(missingSuggestions, items, locator);
                }
            });
        }
 private static IEnumerable <Diagnostic> GetReportedDiagnostics(Document document, IDiagnosticLocator locator)
 {
     return(document.GetSemanticModelAsync().Result.GetDiagnostics().Where(d => locator.Match(d.Location)));
 }
        public static RoslynTestKitException UnexpectedDiagnostic(string diagnosticId, IDiagnosticLocator locator = null)
        {
            var description = locator != null ? $"at {locator.Description()}" : string.Empty;

            return(new RoslynTestKitException($"Found reported diagnostic '{diagnosticId}' in spite of the expectations {description}"));
        }
        public static RoslynTestKitException CodeFixNotFound(int expectedCodeFixIndex, ImmutableArray <CodeAction> codeFixes, IDiagnosticLocator locator)
        {
            var codeFixDescription = GetActionsDescription(codeFixes, " Found only {codeFixes.Length} CodeFixes: ");
            var message            = $"Cannot find CodeFix with index {expectedCodeFixIndex} at {locator.Description()}.{codeFixDescription}";

            return(new RoslynTestKitException(message));
        }
        private void TestCodeFix(Document document, string expected, string diagnosticId, IDiagnosticLocator locator, ICodeActionSelector codeActionSelector)
        {
            var diagnostic = GetDiagnostic(document, diagnosticId, locator);

            TestCodeFix(document, expected, diagnostic, locator, codeActionSelector);
        }
        private Diagnostic FindOrCreateDiagnosticForDescriptor(Document document, DiagnosticDescriptor descriptor, IDiagnosticLocator locator)
        {
            var reportedDiagnostics = GetReportedDiagnostics(document, locator).ToList();
            var diagnostic          = reportedDiagnostics.FirstOrDefault(x => x.Id == descriptor.Id);

            if (diagnostic != null)
            {
                return(diagnostic);
            }

            var tree = document.GetSyntaxTreeAsync(CancellationToken.None).Result;

            return(Diagnostic.Create(descriptor, Location.Create(tree, locator.GetSpan())));
        }
 private IEnumerable <Diagnostic> GetReportedDiagnostics(Document document, IDiagnosticLocator locator)
 {
     return(GetAllReportedDiagnostics(document).Where(d => locator.Match(d.Location)));
 }
        private void TestCodeFix(Document document, string expected, Diagnostic diagnostic, IDiagnosticLocator locator, int codeFixIndex = 0)
        {
            var codeFixes = GetCodeFixes(document, diagnostic);

            if (codeFixes.Length < codeFixIndex + 1)
            {
                throw RoslynTestKitException.CodeFixNotFound(codeFixIndex, codeFixes, locator);
            }
            Verify.CodeAction(codeFixes[codeFixIndex], document, expected);
        }
        private void TestCodeFix(Document document, string expected, Diagnostic diagnostic, IDiagnosticLocator locator, ICodeActionSelector codeActionSelector)
        {
            var codeFixes  = GetCodeFixes(document, diagnostic);
            var codeAction = codeActionSelector.Find(codeFixes);

            if (codeAction is null)
            {
                throw RoslynTestKitException.CodeFixNotFound(codeActionSelector, codeFixes, locator);
            }
            Verify.CodeAction(codeAction, document, expected);
        }
        public static Exception CannotFindSuggestion(IReadOnlyList <string> missingCompletion, ImmutableArray <CompletionItem> resultItems, IDiagnosticLocator locator)
        {
            var foundSuggestionDescription = resultItems.MergeAsBulletList(x => x.DisplayText, title: "\r\nFound suggestions:\r\n");

            return(new RoslynTestKitException($"Cannot get suggestions:\r\n{missingCompletion.MergeAsBulletList()}\r\nat{locator.Description()}{foundSuggestionDescription}"));
        }
        private void NoCodeFix(Document document, string diagnosticId, IDiagnosticLocator locator)
        {
            var diagnostic = GetDiagnostic(document, diagnosticId, locator);

            NoCodeFix(document, diagnostic, locator);
        }
        private void TestCodeFix(Document document, string expected, string diagnosticId, IDiagnosticLocator locator, int codeFixIndex)
        {
            var diagnostic = GetDiagnostic(document, diagnosticId, locator);

            TestCodeFix(document, expected, diagnostic, locator, codeFixIndex);
        }
        public static RoslynTestKitException UnexpectedCodeFixFound(ImmutableArray <CodeAction> codeFixes, IDiagnosticLocator locator)
        {
            var codeFixDescription = GetActionsDescription(codeFixes, "Reported fixes: ");
            var message            = $"Found unexpected CodeFixes at {locator.Description()}.{codeFixDescription}";

            return(new RoslynTestKitException(message));
        }
        private void TestCodeFix(Document document, string expected, string diagnosticId, IDiagnosticLocator locator, int codeFixIndex)
        {
            var reportedDiagnostics = GetReportedDiagnostics(document, locator).ToArray();
            var diagnostic          = reportedDiagnostics.FirstOrDefault(x => x.Id == diagnosticId);

            if (diagnostic == null)
            {
                throw RoslynTestKitException.DiagnosticNotFound(diagnosticId, locator, reportedDiagnostics);
            }
            TestCodeFix(document, expected, diagnostic.Descriptor, locator, codeFixIndex);
        }
        public static RoslynTestKitException CodeRefactoringNotFound(ICodeActionSelector codeActionSelector, ImmutableArray <CodeAction> codeRefactorings, IDiagnosticLocator locator)
        {
            var refactoringDescriptions = GetActionsDescription(codeRefactorings, $" Found only {codeRefactorings.Length} CodeRefactorings: ");
            var message = $"Cannot find CodeRefactoring {codeActionSelector}  at {locator.Description()}.{refactoringDescriptions}";

            return(new RoslynTestKitException(message));
        }