private static void TestWorker(
            string inputMarkup,
            string expectedOutputMarkup,
            Action callback,
            bool verifyUndo         = true,
            IndentStyle indentStyle = IndentStyle.Smart,
            bool useTabs            = false)
        {
            using var workspace = TestWorkspace.CreateCSharp(inputMarkup);

            // TODO: set SmartIndent to textView.Options (https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1412138)
            workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
                                                                            .WithChangedOption(AutoFormattingOptions.Metadata.SmartIndent, LanguageNames.CSharp, indentStyle)));

            if (useTabs && expectedOutputMarkup != null)
            {
                Assert.Contains("\t", expectedOutputMarkup);
            }

            var document = workspace.Documents.Single();
            var view     = document.GetTextView();

            view.Options.SetOptionValue(DefaultOptions.ConvertTabsToSpacesOptionId, !useTabs);
            view.Options.SetOptionValue(DefaultOptions.TabSizeOptionId, 4);

            var originalSnapshot   = view.TextBuffer.CurrentSnapshot;
            var originalSelections = document.SelectedSpans;

            var snapshotSpans = new List <SnapshotSpan>();

            foreach (var selection in originalSelections)
            {
                snapshotSpans.Add(selection.ToSnapshotSpan(originalSnapshot));
            }

            view.SetMultiSelection(snapshotSpans);

            var undoHistoryRegistry = workspace.GetService <ITextUndoHistoryRegistry>();
            var commandHandler      = workspace.ExportProvider.GetCommandHandler <SplitStringLiteralCommandHandler>(nameof(SplitStringLiteralCommandHandler));

            if (!commandHandler.ExecuteCommand(new ReturnKeyCommandArgs(view, view.TextBuffer), TestCommandExecutionContext.Create()))
            {
                callback();
            }

            if (expectedOutputMarkup != null)
            {
                MarkupTestFile.GetSpans(expectedOutputMarkup,
                                        out var expectedOutput, out ImmutableArray <TextSpan> expectedSpans);

                Assert.Equal(expectedOutput, view.TextBuffer.CurrentSnapshot.AsText().ToString());
                Assert.Equal(expectedSpans.First().Start, view.Caret.Position.BufferPosition.Position);

                if (verifyUndo)
                {
                    // Ensure that after undo we go back to where we were to begin with.
                    var history = undoHistoryRegistry.GetHistory(document.GetTextBuffer());
                    history.Undo(count: originalSelections.Count);

                    var currentSnapshot = document.GetTextBuffer().CurrentSnapshot;
                    Assert.Equal(originalSnapshot.GetText(), currentSnapshot.GetText());
                    Assert.Equal(originalSelections.First().Start, view.Caret.Position.BufferPosition.Position);
                }
            }
        }
 public void SendInvokeCompletionList(Action <InvokeCompletionListCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new InvokeCompletionListCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 public void SendInsertSnippetCommand(Action <InsertSnippetCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new InsertSnippetCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 public bool SendBackTab(Func <BackTabKeyCommandArgs, CommandExecutionContext, bool> commandHandler)
 => commandHandler(new BackTabKeyCommandArgs(TextView, SubjectBuffer), TestCommandExecutionContext.Create());
 public bool SendReturn(Func <ReturnKeyCommandArgs, CommandExecutionContext, bool> commandHandler)
 => commandHandler(new ReturnKeyCommandArgs(TextView, SubjectBuffer), TestCommandExecutionContext.Create());
 public void SendToggleCompletionMode(Action <ToggleCompletionModeCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 {
     commandHandler(new ToggleCompletionModeCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 }
 public void SendBackspace(Action <BackspaceKeyCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new BackspaceKeyCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
        private void TestWorker(
            string inputMarkup, string expectedOutputMarkup, Action callback)
        {
            using (var workspace = TestWorkspace.CreateCSharp(inputMarkup))
            {
                var document = workspace.Documents.Single();
                var view     = document.GetTextView();

                var originalSnapshot  = view.TextBuffer.CurrentSnapshot;
                var originalSelection = document.SelectedSpans.Single();
                view.SetSelection(originalSelection.ToSnapshotSpan(originalSnapshot));

                var undoHistoryRegistry = workspace.GetService <ITextUndoHistoryRegistry>();
                var commandHandler      = new SplitStringLiteralCommandHandler(
                    undoHistoryRegistry,
                    workspace.GetService <IEditorOperationsFactoryService>());

                if (!commandHandler.ExecuteCommand(new ReturnKeyCommandArgs(view, view.TextBuffer), TestCommandExecutionContext.Create()))
                {
                    callback();
                }

                if (expectedOutputMarkup != null)
                {
                    MarkupTestFile.GetSpans(expectedOutputMarkup,
                                            out var expectedOutput, out ImmutableArray <TextSpan> expectedSpans);

                    Assert.Equal(expectedOutput, view.TextBuffer.CurrentSnapshot.AsText().ToString());
                    Assert.Equal(expectedSpans.Single().Start, view.Caret.Position.BufferPosition.Position);

                    // Ensure that after undo we go back to where we were to begin with.
                    var history = undoHistoryRegistry.GetHistory(document.TextBuffer);
                    history.Undo(count: 1);

                    var currentSnapshot = document.TextBuffer.CurrentSnapshot;
                    Assert.Equal(originalSnapshot.GetText(), currentSnapshot.GetText());
                    Assert.Equal(originalSelection.Start, view.Caret.Position.BufferPosition.Position);
                }
            }
        }
예제 #9
0
        private void AssertFormatAfterTypeChar(string code, string expected, Dictionary <OptionKey, object> changedOptionSet = null)
        {
            using (var workspace = TestWorkspace.CreateCSharp(code))
            {
                if (changedOptionSet != null)
                {
                    var options = workspace.Options;
                    foreach (var entry in changedOptionSet)
                    {
                        options = options.WithChangedOption(entry.Key, entry.Value);
                    }

                    workspace.Options = options;
                }

                var subjectDocument = workspace.Documents.Single();

                var commandHandler = workspace.GetService <FormatCommandHandler>();
                var typedChar      = subjectDocument.GetTextBuffer().CurrentSnapshot.GetText(subjectDocument.CursorPosition.Value - 1, 1);
                commandHandler.ExecuteCommand(new TypeCharCommandArgs(subjectDocument.GetTextView(), subjectDocument.TextBuffer, typedChar[0]), () => { }, TestCommandExecutionContext.Create());

                var newSnapshot = subjectDocument.TextBuffer.CurrentSnapshot;

                Assert.Equal(expected, newSnapshot.GetText());
            }
        }
예제 #10
0
        protected void Test(string expected, string code, bool completionActive = false, bool assertNextHandlerInvoked = false)
#pragma warning restore IDE0060 // Remove unused parameter
        {
            // WPF is required for some reason: https://github.com/dotnet/roslyn/issues/46286
            using var workspace = TestWorkspace.Create(Language, compilationOptions: null, parseOptions: null, new[] { code }, composition: EditorTestCompositions.EditorFeaturesWpf);

            var view               = workspace.Documents.Single().GetTextView();
            var buffer             = workspace.Documents.Single().GetTextBuffer();
            var nextHandlerInvoked = false;

            view.Caret.MoveTo(new SnapshotPoint(buffer.CurrentSnapshot, workspace.Documents.Single(d => d.CursorPosition.HasValue).CursorPosition.Value));

            var commandHandler = GetCommandHandler(workspace);
            var nextHandler    = assertNextHandlerInvoked ? () => nextHandlerInvoked = true : CreateNextHandler(workspace);

            commandHandler.ExecuteCommand(new AutomaticLineEnderCommandArgs(view, buffer), nextHandler, TestCommandExecutionContext.Create());

            Test(view, buffer, expected);

            Assert.Equal(assertNextHandlerInvoked, nextHandlerInvoked);
        }
 public void SendSurroundWithCommand(Action <SurroundWithCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new SurroundWithCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
        /// <summary>
        /// verifyUndo is needed because of https://github.com/dotnet/roslyn/issues/28033
        /// Most tests will continue to verifyUndo, but select tests will skip it due to
        /// this known test infrastructure issure. This bug does not represent a product
        /// failure.
        /// </summary>
        private void TestWorker(
            string inputMarkup,
            string expectedOutputMarkup,
            Action callback,
            bool verifyUndo         = true,
            IndentStyle indentStyle = IndentStyle.Smart,
            bool useTabs            = false)
        {
            using var workspace = TestWorkspace.CreateCSharp(inputMarkup);
            workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
                                                                            .WithChangedOption(SmartIndent, LanguageNames.CSharp, indentStyle)
                                                                            .WithChangedOption(UseTabs, LanguageNames.CSharp, useTabs)));

            if (useTabs && expectedOutputMarkup != null)
            {
                Assert.Contains("\t", expectedOutputMarkup);
            }

            var document = workspace.Documents.Single();
            var view     = document.GetTextView();

            var originalSnapshot   = view.TextBuffer.CurrentSnapshot;
            var originalSelections = document.SelectedSpans;

            var snapshotSpans = new List <SnapshotSpan>();

            foreach (var selection in originalSelections)
            {
                snapshotSpans.Add(selection.ToSnapshotSpan(originalSnapshot));
            }
            view.SetMultiSelection(snapshotSpans);

            var undoHistoryRegistry = workspace.GetService <ITextUndoHistoryRegistry>();
            var commandHandler      = new SplitStringLiteralCommandHandler(
                undoHistoryRegistry,
                workspace.GetService <IEditorOperationsFactoryService>());

            if (!commandHandler.ExecuteCommand(new ReturnKeyCommandArgs(view, view.TextBuffer), TestCommandExecutionContext.Create()))
            {
                callback();
            }

            if (expectedOutputMarkup != null)
            {
                MarkupTestFile.GetSpans(expectedOutputMarkup,
                                        out var expectedOutput, out ImmutableArray <TextSpan> expectedSpans);

                Assert.Equal(expectedOutput, view.TextBuffer.CurrentSnapshot.AsText().ToString());
                Assert.Equal(expectedSpans.First().Start, view.Caret.Position.BufferPosition.Position);

                if (verifyUndo)
                {
                    // Ensure that after undo we go back to where we were to begin with.
                    var history = undoHistoryRegistry.GetHistory(document.GetTextBuffer());
                    history.Undo(count: originalSelections.Count);

                    var currentSnapshot = document.GetTextBuffer().CurrentSnapshot;
                    Assert.Equal(originalSnapshot.GetText(), currentSnapshot.GetText());
                    Assert.Equal(originalSelections.First().Start, view.Caret.Position.BufferPosition.Position);
                }
            }
        }
예제 #13
0
        protected void ToggleCommentWithProjectionBuffer(string surfaceBufferMarkup, string subjectBufferMarkup, string entireExpectedMarkup)
        {
            using (var workspace = GetWorkspace(subjectBufferMarkup, GetExportProvider()))
            {
                var document = workspace.CreateProjectionBufferDocument(surfaceBufferMarkup, workspace.Documents);
                SetupSelection(document.GetTextView(), document.SelectedSpans.Select(s => Span.FromBounds(s.Start, s.End)));

                var commandHandler        = GetToggleCommentCommandHandler(workspace);
                var textView              = document.GetTextView();
                var originalSubjectBuffer = GetBufferForContentType(ContentTypeNames.CSharpContentType, textView);

                commandHandler.ExecuteCommand(textView, originalSubjectBuffer, ValueTuple.Create(), TestCommandExecutionContext.Create());
                AssertCommentResult(textView.TextBuffer, textView, entireExpectedMarkup);
            }
        }
예제 #14
0
        protected void ToggleCommentMultiple(string markup, string[] expectedText)
        {
            using (var workspace = GetWorkspace(markup, GetExportProvider()))
            {
                var doc = workspace.Documents.First();
                SetupSelection(doc.GetTextView(), doc.SelectedSpans.Select(s => Span.FromBounds(s.Start, s.End)));

                var commandHandler = GetToggleCommentCommandHandler(workspace);
                var textView       = doc.GetTextView();
                var textBuffer     = doc.GetTextBuffer();

                for (var i = 0; i < expectedText.Length; i++)
                {
                    commandHandler.ExecuteCommand(textView, textBuffer, ValueTuple.Create(), TestCommandExecutionContext.Create());
                    AssertCommentResult(doc.GetTextBuffer(), textView, expectedText[i]);
                }
            }
        }
 public void SendTypeChar(char typeChar, Action <TypeCharCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 {
     commandHandler(new TypeCharCommandArgs(TextView, SubjectBuffer, typeChar), nextHandler, TestCommandExecutionContext.Create());
 }
예제 #16
0
        public void TestExtractMethodCommandHandlerErrorMessage()
        {
            var markupCode = @"class A
{
    [|void Method() {}|]
}";

            using var workspace = TestWorkspace.CreateCSharp(markupCode, composition: EditorTestCompositions.EditorFeaturesWpf);
            var testDocument = workspace.Documents.Single();

            var view = testDocument.GetTextView();

            view.Selection.Select(new SnapshotSpan(
                                      view.TextBuffer.CurrentSnapshot, testDocument.SelectedSpans[0].Start, testDocument.SelectedSpans[0].Length), isReversed: false);

            var callBackService = workspace.Services.GetService <INotificationService>() as INotificationServiceCallback;
            var called          = false;

            callBackService.NotificationCallback = (t, m, s) => called = true;

            var handler = new ExtractMethodCommandHandler(
                workspace.GetService <IThreadingContext>(),
                workspace.GetService <ITextBufferUndoManagerProvider>(),
                workspace.GetService <IInlineRenameService>());

            handler.ExecuteCommand(new ExtractMethodCommandArgs(view, view.TextBuffer), TestCommandExecutionContext.Create());

            Assert.True(called);
        }
 public void SendSelectAll(Action <SelectAllCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 {
     commandHandler(new SelectAllCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 }
        public void SendCopyToInteractive()
        {
            var copyToInteractiveArgs = new CopyToInteractiveCommandArgs(TextView, SubjectBuffer);

            CopyToInteractiveCommandHandler.ExecuteCommand(copyToInteractiveArgs, TestCommandExecutionContext.Create());
        }
예제 #19
0
 public void SendEscape()
 => _commandHandler.ExecuteCommand(new EscapeKeyCommandArgs(_view, _view.TextBuffer), TestCommandExecutionContext.Create());
        public void ExecuteInInteractive()
        {
            var executeInInteractiveArgs = new ExecuteInInteractiveCommandArgs(TextView, SubjectBuffer);

            ExecuteInInteractiveCommandHandler.ExecuteCommand(executeInInteractiveArgs, TestCommandExecutionContext.Create());
        }
 public void SendWordDeleteToStart(Action <WordDeleteToStartCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new WordDeleteToStartCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 public void SendTab(Action <TabKeyCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 {
     commandHandler(new TabKeyCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 }
 public void SendReturn(Action <ReturnKeyCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new ReturnKeyCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 public bool SendTab(Func <TabKeyCommandArgs, CommandExecutionContext, bool> commandHandler)
 {
     return(commandHandler(new TabKeyCommandArgs(TextView, SubjectBuffer), TestCommandExecutionContext.Create()));
 }
 public void SendPaste(Action <PasteCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new PasteCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 public bool SendSurroundWithCommand(Func <SurroundWithCommandArgs, CommandExecutionContext, bool> commandHandler)
 {
     return(commandHandler(new SurroundWithCommandArgs(TextView, SubjectBuffer), TestCommandExecutionContext.Create()));
 }
 public void SendCommitUniqueCompletionListItem(Action <CommitUniqueCompletionListItemCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 => commandHandler(new CommitUniqueCompletionListItemCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 public void SendInvokeSignatureHelp(Action <InvokeSignatureHelpCommandArgs, Action, CommandExecutionContext> commandHandler, Action nextHandler)
 {
     commandHandler(new InvokeSignatureHelpCommandArgs(TextView, SubjectBuffer), nextHandler, TestCommandExecutionContext.Create());
 }
 public bool SendInsertSnippetCommand(Func <InsertSnippetCommandArgs, CommandExecutionContext, bool> commandHandler)
 => commandHandler(new InsertSnippetCommandArgs(TextView, SubjectBuffer), TestCommandExecutionContext.Create());
예제 #30
0
        private static void UncommentSelection(string markup, string expected)
        {
            using (var workspace = TestWorkspace.CreateCSharp(markup))
            {
                var doc = workspace.Documents.First();
                SetupSelection(doc.GetTextView(), doc.SelectedSpans.Select(s => Span.FromBounds(s.Start, s.End)));

                var commandHandler = new CommentUncommentSelectionCommandHandler(
                    workspace.ExportProvider.GetExportedValue <ITextUndoHistoryRegistry>(),
                    workspace.ExportProvider.GetExportedValue <IEditorOperationsFactoryService>());
                var textView   = doc.GetTextView();
                var textBuffer = doc.GetTextBuffer();
                commandHandler.ExecuteCommand(textView, textBuffer, Operation.Uncomment, TestCommandExecutionContext.Create());

                Assert.Equal(expected, doc.TextBuffer.CurrentSnapshot.GetText());
            }
        }