Exemplo n.º 1
0
        public async Task ErrorTagGeneratedForWarningAsError()
        {
            var workspaceXml =
                @"<Workspace>
    <Project Language=""C#"" CommonReferences=""true"">
        <CompilationOptions ReportDiagnostic = ""Error"" />
            <Document FilePath = ""Test.cs"" >
                class Program
                {
                    void Test()
                    {
                        int a = 5;
                    }
                }
        </Document>
    </Project>
</Workspace>";

            using var workspace = TestWorkspace.Create(workspaceXml);
            var spans =
                (
                    await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .GetDiagnosticsAndErrorSpans(
                        workspace
                        )
                ).Item2;

            Assert.Equal(1, spans.Count());
            Assert.Equal(PredefinedErrorTypeNames.SyntaxError, spans.First().Tag.ErrorType);
        }
Exemplo n.º 2
0
        public async Task LiveErrorZeroLengthSpan()
        {
            var workspaceXml =
                @"<Workspace>
    <Project Language=""C#"" CommonReferences=""true"">
        <Document FilePath = ""Test.cs"" >
            class Test
{
}
        </Document>
    </Project>
</Workspace>";

            using var workspace = TestWorkspace.Create(workspaceXml);
            var document = workspace.Documents.First();

            var updateArgs = DiagnosticsUpdatedArgs.DiagnosticsCreated(
                new LiveId(), workspace, workspace.CurrentSolution, document.Project.Id, document.Id,
                ImmutableArray.Create(
                    TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .CreateDiagnosticData(document, new TextSpan(0, 0)),
                    TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .CreateDiagnosticData(document, new TextSpan(0, 1))));

            var spans = await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .GetErrorsFromUpdateSource(workspace, updateArgs);

            Assert.Equal(2, spans.Count());
            var first  = spans.First();
            var second = spans.Last();

            Assert.Equal(1, first.Span.Span.Length);
            Assert.Equal(1, second.Span.Span.Length);
        }
Exemplo n.º 3
0
        private static async Task <(ImmutableArray <ITagSpan <IErrorTag> > spans, TextSpan selection)> GetTagSpansAndSelectionAsync(string content)
        {
            using var workspace = TestWorkspace.CreateCSharp(content);
            var analyzerMap = new Dictionary <string, ImmutableArray <DiagnosticAnalyzer> >()
            {
                { LanguageNames.CSharp, ImmutableArray.Create <DiagnosticAnalyzer>(new CSharpUseObjectInitializerDiagnosticAnalyzer()) }
            };

            var spans = (await TestDiagnosticTagProducer <DiagnosticsSuggestionTaggerProvider> .GetDiagnosticsAndErrorSpans(workspace, analyzerMap)).Item2;

            return(spans, workspace.Documents.Single().SelectedSpans.Single());
        }
Exemplo n.º 4
0
 private static async Task <ImmutableArray <ITagSpan <IErrorTag> > > GetTagSpansAsync(
     string content
     )
 {
     using var workspace = TestWorkspace.CreateCSharp(
               content,
               composition: SquiggleUtilities.CompositionWithSolutionCrawler
               );
     return((
                await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .GetDiagnosticsAndErrorSpans(
                    workspace
                    )
                ).Item2);
 }
Exemplo n.º 5
0
        public async Task SemanticErrorReported()
        {
            using var workspace = TestWorkspace.CreateCSharp(
                      "class C : Bar { }",
                      composition: SquiggleUtilities.CompositionWithSolutionCrawler
                      );

            var spans =
                await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .GetDiagnosticsAndErrorSpans(
                    workspace
                    );

            Assert.Equal(1, spans.Item2.Count());

            var firstDiagnostic = spans.Item1.First();
            var firstSpan       = spans.Item2.First();

            Assert.Equal(PredefinedErrorTypeNames.SyntaxError, firstSpan.Tag.ErrorType);

            var expectedToolTip = new ContainerElement(
                ContainerElementStyle.Wrapped,
                new ClassifiedTextElement(
                    new ClassifiedTextRun(
                        ClassificationTypeNames.Text,
                        "CS0246",
                        QuickInfoHyperLink.TestAccessor.CreateNavigationAction(
                            new Uri(
                                "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS0246)",
                                UriKind.Absolute
                                )
                            ),
                        "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS0246)"
                        ),
                    new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
                    new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
                    new ClassifiedTextRun(ClassificationTypeNames.Text, firstDiagnostic.Message)
                    )
                );

            ToolTipAssert.EqualContent(expectedToolTip, firstSpan.Tag.ToolTipContent);
        }
Exemplo n.º 6
0
        public async Task SemanticErrorReported()
        {
            using var workspace = TestWorkspace.CreateCSharp("class C : Bar { }", composition: SquiggleUtilities.CompositionWithSolutionCrawler);

            var spans = await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .GetDiagnosticsAndErrorSpans(workspace);

            Assert.Equal(1, spans.Item2.Count());

            var firstDiagnostic = spans.Item1.First();
            var firstSpan       = spans.Item2.First();

            Assert.Equal(PredefinedErrorTypeNames.SyntaxError, firstSpan.Tag.ErrorType);

            var expectedToolTip = new ContainerElement(
                ContainerElementStyle.Wrapped,
                new ClassifiedTextElement(
                    new ClassifiedTextRun(ClassificationTypeNames.Text, "CS0246"),
                    new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
                    new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
                    new ClassifiedTextRun(ClassificationTypeNames.Text, firstDiagnostic.Message)));

            ToolTipAssert.EqualContent(expectedToolTip, firstSpan.Tag.ToolTipContent);
        }
Exemplo n.º 7
0
        public async Task CustomizableTagsForUnnecessaryCode()
        {
            var workspaceXml =
                @"<Workspace>
    <Project Language=""C#"" CommonReferences=""true"">
        <Document FilePath = ""Test.cs"" >
// System is used - rest are unused.
using System.Collections;
using System;
using System.Diagnostics;
using System.Collections.Generic;

class Program
{
    void Test()
    {
        Int32 x = 2; // Int32 can be simplified.
        x += 1;
    }
}
        </Document>
    </Project>
</Workspace>";

            using var workspace = TestWorkspace.Create(workspaceXml, composition: SquiggleUtilities.CompositionWithSolutionCrawler);
            var options  = new Dictionary <OptionKey2, object>();
            var language = workspace.Projects.Single().Language;
            var preferIntrinsicPredefinedTypeOption      = new OptionKey2(CodeStyleOptions2.PreferIntrinsicPredefinedTypeKeywordInDeclaration, language);
            var preferIntrinsicPredefinedTypeOptionValue = new CodeStyleOption2 <bool>(value: true, notification: NotificationOption2.Error);

            options.Add(preferIntrinsicPredefinedTypeOption, preferIntrinsicPredefinedTypeOptionValue);

            workspace.ApplyOptions(options);

            var analyzerMap = new Dictionary <string, ImmutableArray <DiagnosticAnalyzer> >
            {
                {
                    LanguageNames.CSharp,
                    ImmutableArray.Create <DiagnosticAnalyzer>(
                        new CSharpSimplifyTypeNamesDiagnosticAnalyzer(),
                        new CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer(),
                        new ReportOnClassWithLink())
                }
            };

            var diagnosticsAndSpans = await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider> .GetDiagnosticsAndErrorSpans(workspace, analyzerMap);

            var spans =
                diagnosticsAndSpans.Item1
                .Zip(diagnosticsAndSpans.Item2, (diagnostic, span) => (diagnostic, span))
                .OrderBy(s => s.span.Span.Span.Start).ToImmutableArray();

            Assert.Equal(4, spans.Length);
            var first  = spans[0].span;
            var second = spans[1].span;
            var third  = spans[2].span;
            var fourth = spans[3].span;

            var expectedToolTip = new ContainerElement(
                ContainerElementStyle.Wrapped,
                new ClassifiedTextElement(
                    new ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005"),
                    new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
                    new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
                    new ClassifiedTextRun(ClassificationTypeNames.Text, CSharpAnalyzersResources.Using_directive_is_unnecessary)));

            Assert.Equal(PredefinedErrorTypeNames.Suggestion, first.Tag.ErrorType);
            ToolTipAssert.EqualContent(expectedToolTip, first.Tag.ToolTipContent);
            Assert.Equal(40, first.Span.Start);
            Assert.Equal(25, first.Span.Length);

            expectedToolTip = new ContainerElement(
                ContainerElementStyle.Wrapped,
                new ClassifiedTextElement(
                    new ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005"),
                    new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
                    new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
                    new ClassifiedTextRun(ClassificationTypeNames.Text, CSharpAnalyzersResources.Using_directive_is_unnecessary)));

            Assert.Equal(PredefinedErrorTypeNames.Suggestion, second.Tag.ErrorType);
            ToolTipAssert.EqualContent(expectedToolTip, second.Tag.ToolTipContent);
            Assert.Equal(82, second.Span.Start);
            Assert.Equal(60, second.Span.Length);

            expectedToolTip = new ContainerElement(
                ContainerElementStyle.Wrapped,
                new ClassifiedTextElement(
                    new ClassifiedTextRun(ClassificationTypeNames.Text, "id", QuickInfoHyperLink.TestAccessor.CreateNavigationAction(new Uri("https://github.com/dotnet/roslyn", UriKind.Absolute)), "https://github.com/dotnet/roslyn"),
                    new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
                    new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
                    new ClassifiedTextRun(ClassificationTypeNames.Text, "messageFormat")));

            Assert.Equal(PredefinedErrorTypeNames.Warning, third.Tag.ErrorType);
            ToolTipAssert.EqualContent(expectedToolTip, third.Tag.ToolTipContent);
            Assert.Equal(152, third.Span.Start);
            Assert.Equal(7, third.Span.Length);

            expectedToolTip = new ContainerElement(
                ContainerElementStyle.Wrapped,
                new ClassifiedTextElement(
                    new ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0049"),
                    new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
                    new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
                    new ClassifiedTextRun(ClassificationTypeNames.Text, WorkspacesResources.Name_can_be_simplified)));

            Assert.Equal(PredefinedErrorTypeNames.SyntaxError, fourth.Tag.ErrorType);
            ToolTipAssert.EqualContent(expectedToolTip, fourth.Tag.ToolTipContent);
            Assert.Equal(196, fourth.Span.Start);
            Assert.Equal(5, fourth.Span.Length);
        }
Exemplo n.º 8
0
 private static async Task <ImmutableArray <ITagSpan <IErrorTag> > > GetTagSpansAsync(TestWorkspace workspace)
 {
     return((await TestDiagnosticTagProducer <DiagnosticsSquiggleTaggerProvider, IErrorTag> .GetDiagnosticsAndErrorSpans(workspace)).Item2);
 }
Exemplo n.º 9
0
 private static async Task <ImmutableArray <ITagSpan <InlineDiagnosticsTag> > > GetTagSpansAsync(TestWorkspace workspace)
 {
     workspace.GlobalOptions.SetGlobalOption(new OptionKey(InlineDiagnosticsOptions.EnableInlineDiagnostics, LanguageNames.CSharp), true);
     return((await TestDiagnosticTagProducer <InlineDiagnosticsTaggerProvider, InlineDiagnosticsTag> .GetDiagnosticsAndErrorSpans(workspace)).Item2);
 }
 private static async Task <ImmutableArray <ITagSpan <InlineDiagnosticsTag> > > GetTagSpansAsync(TestWorkspace workspace)
 {
     workspace.ApplyOptions(new[] { KeyValuePairUtil.Create(new OptionKey2(InlineDiagnosticsOptions.EnableInlineDiagnostics, LanguageNames.CSharp), (object)true) });
     return((await TestDiagnosticTagProducer <InlineDiagnosticsTaggerProvider, InlineDiagnosticsTag> .GetDiagnosticsAndErrorSpans(workspace)).Item2);
 }