예제 #1
0
        public async Task Handle_InvalidFileKind()
        {
            // Arrange
            var documentPath = "c:\\Test.razor";
            var contents     = $"@page \"/test\"{Environment.NewLine}@code {{ private var x = 1; }}";
            var codeDocument = CreateCodeDocument(contents);

            codeDocument.SetFileKind(FileKinds.Legacy);

            var resolver = new ExtractToCodeBehindCodeActionResolver(new DefaultForegroundDispatcher(), CreateDocumentResolver(documentPath, codeDocument), FilePathNormalizer);
            var data     = JObject.FromObject(new ExtractToCodeBehindCodeActionParams()
            {
                Uri          = new Uri("c:/Test.razor"),
                RemoveStart  = 14,
                ExtractStart = 20,
                ExtractEnd   = 41,
                RemoveEnd    = 41,
            });

            // Act
            var workspaceEdit = await resolver.ResolveAsync(data, default);

            // Assert
            Assert.Null(workspaceEdit);
        }
        public async Task Handle_ExtractCodeBlockWithUsing()
        {
            // Arrange
            var documentPath = "c:/Test.razor";
            var documentUri  = new Uri(documentPath);
            var contents     = $"@page \"/test\"\n@using System.Diagnostics{Environment.NewLine}@code {{ private var x = 1; }}";
            var codeDocument = CreateCodeDocument(contents);

            var resolver     = new ExtractToCodeBehindCodeActionResolver(Dispatcher, CreateDocumentResolver(documentPath, codeDocument), FilePathNormalizer);
            var actionParams = new ExtractToCodeBehindCodeActionParams
            {
                Uri          = documentUri,
                RemoveStart  = contents.IndexOf("@code", StringComparison.Ordinal),
                ExtractStart = contents.IndexOf("{", StringComparison.Ordinal),
                ExtractEnd   = contents.IndexOf("}", StringComparison.Ordinal),
                RemoveEnd    = contents.IndexOf("}", StringComparison.Ordinal),
            };
            var data = JObject.FromObject(actionParams);

            // Act
            var workspaceEdit = await resolver.ResolveAsync(data, default);

            // Assert
            Assert.NotNull(workspaceEdit);
            Assert.NotNull(workspaceEdit.DocumentChanges);
            Assert.Equal(3, workspaceEdit.DocumentChanges !.Count());

            var documentChanges  = workspaceEdit.DocumentChanges !.ToArray();
            var createFileChange = documentChanges[0];

            Assert.True(createFileChange.IsCreateFile);

            var editCodeDocumentChange = documentChanges[1];

            Assert.NotNull(editCodeDocumentChange.TextDocumentEdit);
            var editCodeDocumentEdit = editCodeDocumentChange.TextDocumentEdit !.Edits.First();

            Assert.True(editCodeDocumentEdit.Range.Start.TryGetAbsoluteIndex(codeDocument.GetSourceText(), _logger, out var removeStart));
            Assert.Equal(actionParams.RemoveStart, removeStart);
            Assert.True(editCodeDocumentEdit.Range.End.TryGetAbsoluteIndex(codeDocument.GetSourceText(), _logger, out var removeEnd));
            Assert.Equal(actionParams.RemoveEnd, removeEnd);

            var editCodeBehindChange = documentChanges[2];

            Assert.NotNull(editCodeBehindChange.TextDocumentEdit);
            var editCodeBehindEdit = editCodeBehindChange.TextDocumentEdit !.Edits.First();

            Assert.Contains("using System.Diagnostics", editCodeBehindEdit.NewText, StringComparison.Ordinal);
            Assert.Contains("public partial class Test", editCodeBehindEdit.NewText, StringComparison.Ordinal);
            Assert.Contains("private var x = 1", editCodeBehindEdit.NewText, StringComparison.Ordinal);
            Assert.Contains("namespace test.Pages", editCodeBehindEdit.NewText, StringComparison.Ordinal);
        }
예제 #3
0
        public async Task Handle_MissingFile()
        {
            // Arrange
            var resolver = new ExtractToCodeBehindCodeActionResolver(new DefaultForegroundDispatcher(), EmptyDocumentResolver, FilePathNormalizer);
            var data     = JObject.FromObject(new ExtractToCodeBehindCodeActionParams()
            {
                Uri          = new Uri("c:/Test.razor"),
                RemoveStart  = 14,
                ExtractStart = 19,
                ExtractEnd   = 41,
                RemoveEnd    = 41,
            });

            // Act
            var workspaceEdit = await resolver.ResolveAsync(data, default);

            // Assert
            Assert.Null(workspaceEdit);
        }