Ejemplo n.º 1
0
        public async Task Handle_NewComponent_SupportsFileCreationTrue_ReturnsResult()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "<NewComponent></NewComponent>";
            var request      = new RazorCodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(new Position(0, 0), new Position(0, 0)),
            };

            var location = new SourceLocation(1, -1, -1);
            var context  = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(contents.IndexOf("Component", StringComparison.Ordinal), 9), supportsFileCreation: true);

            var provider = new ComponentAccessibilityCodeActionProvider(new DefaultTagHelperFactsService(), FilePathNormalizer);

            // Act
            var commandOrCodeActionContainer = await provider.ProvideAsync(context, default);

            // Assert
            var command = Assert.Single(commandOrCodeActionContainer);

            Assert.Equal("Create component from tag", command.Title);
            Assert.NotNull(command.Data);
        }
Ejemplo n.º 2
0
        public async Task Handle_ExistingComponent_SupportsFileCreationFalse_ReturnsResults()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "<Component></Component>";
            var request      = new RazorCodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(new Position(0, 0), new Position(0, 0)),
            };

            var location = new SourceLocation(1, -1, -1);
            var context  = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(contents.IndexOf("Component", StringComparison.Ordinal), 9), supportsFileCreation: false);

            var provider = new ComponentAccessibilityCodeActionProvider(new DefaultTagHelperFactsService(), FilePathNormalizer);

            // Act
            var commandOrCodeActionContainer = await provider.ProvideAsync(context, default);

            // Assert
            Assert.Collection(commandOrCodeActionContainer,
                              e =>
            {
                Assert.Equal("@using Fully.Qualified", e.Title);
                Assert.NotNull(e.Data);
                Assert.Null(e.Edit);
            },
                              e =>
            {
                Assert.Equal("Fully.Qualified.Component", e.Title);
                Assert.NotNull(e.Edit);
                Assert.NotNull(e.Edit.DocumentChanges);
                Assert.Null(e.Data);
            });
        }
Ejemplo n.º 3
0
        public async Task Handle_ExistingComponent()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "<Component></Component>";
            var request      = new CodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(new Position(0, 0), new Position(0, 0)),
            };

            var location = new SourceLocation(1, -1, -1);
            var context  = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(contents.IndexOf("Component"), 9));

            var provider = new ComponentAccessibilityCodeActionProvider(new DefaultTagHelperFactsService(), FilePathNormalizer);

            // Act
            var commandOrCodeActionContainer = await provider.ProvideAsync(context, default);

            // Assert
            Assert.Equal(3, commandOrCodeActionContainer.Count());
            Assert.Equal("@using Fully.Qualified", commandOrCodeActionContainer.ElementAt(0).Command.Title);
            Assert.Equal("Fully.Qualified.Component", commandOrCodeActionContainer.ElementAt(1).CodeAction.Title);
            Assert.Equal("Create component from tag", commandOrCodeActionContainer.ElementAt(2).Command.Title);
        }
Ejemplo n.º 4
0
        public async Task Handle_CursorOutsideComponent()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = " <Component></Component>";
            var request      = new RazorCodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(new Position(0, 0), new Position(0, 0)),
            };

            var location = new SourceLocation(0, -1, -1);
            var context  = CreateRazorCodeActionContext(request, location, documentPath, contents, new SourceSpan(contents.IndexOf("Component", StringComparison.Ordinal), 9));

            var provider = new ComponentAccessibilityCodeActionProvider(new DefaultTagHelperFactsService(), FilePathNormalizer);

            // Act
            var commandOrCodeActionContainer = await provider.ProvideAsync(context, default);

            // Assert
            Assert.Null(commandOrCodeActionContainer);
        }
Ejemplo n.º 5
0
        public async Task Handle_InvalidSyntaxTree_NoStartNode()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "";
            var request      = new RazorCodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(),
            };

            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 ComponentAccessibilityCodeActionProvider(new DefaultTagHelperFactsService(), FilePathNormalizer);

            // Act
            var commandOrCodeActionContainer = await provider.ProvideAsync(context, default);

            // Assert
            Assert.Null(commandOrCodeActionContainer);
        }