public async Task Handle_MissingDiagnostics_ReturnsEmpty() { // Arrange var documentPath = "c:/Test.razor"; var contents = ""; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = null } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(0, 0)); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); var csharpCodeActions = new[] { new CodeAction() { Title = "System.Net.Dns" } }; // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Empty(results); }
public async Task Handle_EmptyCodeActions_ReturnsEmpty() { // Arrange var documentPath = "c:/Test.razor"; var contents = ""; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = new Container <Diagnostic>( new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0246") } ) } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(0, 0)); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); var csharpCodeActions = Array.Empty <CodeAction>(); // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Empty(results); }
public async Task Handle_ValidCodeAction_VS_ReturnsCodeActions() { // Arrange var documentPath = "c:/Test.razor"; var contents = "@code { Path; }"; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = new Container <Diagnostic>() } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(8, 4), supportsCodeActionResolve: true); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); var csharpCodeActions = new[] { new RazorCodeAction() { Title = "System.IO.Path", Name = "FullyQualify" }, new RazorCodeAction() { Title = "using System.IO;", Name = "AddImport" } }; // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Collection(results, r => { Assert.Equal("@using System.IO", r.Title); Assert.Null(r.Edit); Assert.NotNull(r.Data); var resolutionParams = (r.Data as JObject).ToObject <RazorCodeActionResolutionParams>(); Assert.Equal(LanguageServerConstants.CodeActions.AddUsing, resolutionParams.Action); }, r => { Assert.Equal("System.IO.Path", r.Title); Assert.Null(r.Edit); Assert.NotNull(r.Data); } ); }
public async Task Handle_InvalidDiagnostics_ReturnsEmpty() { // Arrange var documentPath = "c:/Test.razor"; var contents = ""; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = new Container <Diagnostic>( new Diagnostic() { // Invalid as Error is expected Severity = DiagnosticSeverity.Warning, Code = new DiagnosticCode("CS0246") }, new Diagnostic() { // Invalid as CS error code is expected Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode(0246) }, new Diagnostic() { // Invalid as CS0246 or CS0103 is expected Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0183") } ) } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(0, 0)); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); var csharpCodeActions = new[] { new CodeAction() { Title = "System.Net.Dns" } }; // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Empty(results); }
public async Task Handle_InvalidDiagnostic_StartOutOfRange_ValidCodeAction_ReturnsEmpty() { // Arrange var documentPath = "c:/Test.razor"; var contents = "@code { \nPath; }"; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = new Container <Diagnostic>( new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0246"), Range = new Range( new Position(0, 9), new Position(1, 12) ) } ) } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(8, 4)); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); var csharpCodeActions = new[] { new CodeAction() { Title = "System.IO.Path" } }; // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Empty(results); }
public async Task Handle_ValidDiagnostic_MultipleValidCodeActions_VSCode_ReturnsMultipleCodeActions() { // Arrange var documentPath = "c:/Test.razor"; var contents = "@code { Path; }"; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = new Container <Diagnostic>( new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0132") }, new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0246"), Range = new Range( new Position(0, 8), new Position(0, 12) ) }, new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0183") } ) } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(8, 4), supportsCodeActionResolve: false); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); var csharpCodeActions = new[] { new CodeAction() { Title = "Fully qualify 'Path' -> System.IO.Path" }, new CodeAction() { Title = "Fully qualify 'Path' -> SuperSpecialNamespace.Path" } }; // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Collection(results, r => { Assert.Equal("@using SuperSpecialNamespace", r.Title); Assert.Null(r.Edit); Assert.NotNull(r.Data); var resolutionParams = (r.Data as JObject).ToObject <RazorCodeActionResolutionParams>(); Assert.Equal(LanguageServerConstants.CodeActions.AddUsing, resolutionParams.Action); }, r => { Assert.Equal("@using System.IO", r.Title); Assert.Null(r.Edit); Assert.NotNull(r.Data); var resolutionParams = (r.Data as JObject).ToObject <RazorCodeActionResolutionParams>(); Assert.Equal(LanguageServerConstants.CodeActions.AddUsing, resolutionParams.Action); }, r => { Assert.Equal("Fully qualify 'Path' -> SuperSpecialNamespace.Path", r.Title); Assert.NotNull(r.Edit); Assert.Null(r.Data); }, r => { Assert.Equal("Fully qualify 'Path' -> System.IO.Path", r.Title); Assert.NotNull(r.Edit); Assert.Null(r.Data); } ); }
public async Task Handle_ValidDiagnostic_InvalidCodeAction_ReturnsEmpty() { // Arrange var documentPath = "c:/Test.razor"; var contents = "@code { Path; }"; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = new Container <Diagnostic>( new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0132") }, new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0246"), Range = new Range( new Position(0, 8), new Position(0, 12) ) }, new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0183") } ) } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(8, 4)); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); // A valid code actions is expected to end with `Path` as that's the `associatedText` // indicated in the `Diagnostic.Range` for `CS0246` above. var csharpCodeActions = new[] { new CodeAction() { Title = "System.IO.OneThing" }, new CodeAction() { Title = "System.IO.SomethingElse" } }; // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Empty(results); }
public async Task Handle_ValidDiagnostic_ValidCodeAction_ReturnsCodeActions(string errorCode) { // Arrange var documentPath = "c:/Test.razor"; var contents = "@code { Path; }"; var request = new CodeActionParams() { TextDocument = new TextDocumentIdentifier(new Uri(documentPath)), Range = new Range(), Context = new CodeActionContext() { Diagnostics = new Container <Diagnostic>( new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0132") }, new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode(errorCode), Range = new Range( new Position(0, 8), new Position(0, 12) ) }, new Diagnostic() { Severity = DiagnosticSeverity.Error, Code = new DiagnosticCode("CS0183") } ) } }; var location = new SourceLocation(0, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(8, 4)); context.CodeDocument.SetFileKind(FileKinds.Legacy); var provider = new TypeAccessibilityCodeActionProvider(); var csharpCodeActions = new[] { new RazorCodeAction() { Title = "System.IO.Path" }, new RazorCodeAction() { Title = "System.IO.SomethingElse" } }; // Act var results = await provider.ProvideAsync(context, csharpCodeActions, default); // Assert Assert.Collection(results, r => { Assert.Equal("@using System.IO", r.Title); Assert.Null(r.Edit); Assert.NotNull(r.Data); var resolutionParams = Assert.IsType <RazorCodeActionResolutionParams>(r.Data); Assert.Equal(LanguageServerConstants.CodeActions.AddUsing, resolutionParams.Action); }, r => { Assert.Equal("System.IO.Path", r.Title); Assert.NotNull(r.Edit); Assert.Null(r.Data); } ); }