public async Task Handle_InFunctionsDirective_SupportsFileCreationTrue_ReturnsResult()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "@page \"/test\"\n@functions { private var x = 1; }";
            var request      = new RazorCodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(),
            };

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

            var provider = new ExtractToCodeBehindCodeActionProvider();

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

            // Assert
            var codeAction = Assert.Single(commandOrCodeActionContainer);
            var razorCodeActionResolutionParams = codeAction.Data.ToObject <RazorCodeActionResolutionParams>();
            var actionParams = (razorCodeActionResolutionParams.Data as JObject).ToObject <ExtractToCodeBehindCodeActionParams>();

            Assert.Equal(14, actionParams.RemoveStart);
            Assert.Equal(24, actionParams.ExtractStart);
            Assert.Equal(47, actionParams.ExtractEnd);
            Assert.Equal(47, actionParams.RemoveEnd);
        }
Exemplo n.º 2
0
        public async Task Handle_InCodeDirective()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "@page \"/test\"\n@code { private var x = 1; }";
            var request      = new CodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(),
            };

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

            var provider = new ExtractToCodeBehindCodeActionProvider();

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

            // Assert
            Assert.Single(commandOrCodeActionContainer);
            var actionParams = commandOrCodeActionContainer
                               .First().Command.Arguments[0]
                               .ToObject <RazorCodeActionResolutionParams>().Data
                               .ToObject <ExtractToCodeBehindCodeActionParams>();

            Assert.Equal(14, actionParams.RemoveStart);
            Assert.Equal(19, actionParams.ExtractStart);
            Assert.Equal(42, actionParams.ExtractEnd);
            Assert.Equal(42, actionParams.RemoveEnd);
        }
        public async Task Handle_InCodeDirectiveMalformed_ReturnsNull()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "@page \"/test\"\n@code";
            var request      = new RazorCodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(),
            };

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

            var provider = new ExtractToCodeBehindCodeActionProvider();

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

            // Assert
            Assert.Null(commandOrCodeActionContainer);
        }
Exemplo n.º 4
0
        public async Task Handle_InCodeDirectiveWithMarkup()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "@page \"/test\"\n@code { void Test() { <h1>Hello, world!</h1> } }";
            var request      = new CodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(),
            };

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

            var provider = new ExtractToCodeBehindCodeActionProvider();

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

            // Assert
            Assert.Null(commandOrCodeActionContainer);
        }
Exemplo n.º 5
0
        public async Task Handle_InvalidFileKind()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var contents     = "@page \"/test\"\n@code {}";
            var request      = new CodeActionParams()
            {
                TextDocument = new TextDocumentIdentifier(new Uri(documentPath)),
                Range        = new Range(),
            };

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

            context.CodeDocument.SetFileKind(FileKinds.Legacy);

            var provider = new ExtractToCodeBehindCodeActionProvider();

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

            // Assert
            Assert.Null(commandOrCodeActionContainer);
        }