public static ChangeSignatureTestState Create(string markup, string languageName, ParseOptions parseOptions = null, OptionsCollection options = null) { var workspace = languageName switch { "XML" => TestWorkspace.Create(markup, composition: s_composition), LanguageNames.CSharp => TestWorkspace.CreateCSharp(markup, composition: s_composition, parseOptions: (CSharpParseOptions)parseOptions), LanguageNames.VisualBasic => TestWorkspace.CreateVisualBasic(markup, composition: s_composition, parseOptions: parseOptions, compilationOptions: new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)), _ => throw new ArgumentException("Invalid language name.") }; options?.SetGlobalOptions(workspace.GlobalOptions); return(new ChangeSignatureTestState(workspace)); }
private void Verify( string initialMarkup, string expectedMarkup, Action <TestWorkspace, IWpfTextView, IEditorOperationsFactoryService> execute, bool useTabs, string newLine, bool trimTrailingWhiteSpace, OptionsCollection globalOptions) { using (var workspace = CreateTestWorkspace(initialMarkup)) { var testDocument = workspace.Documents.Single(); Assert.True(testDocument.CursorPosition.HasValue, "No caret position set!"); var startCaretPosition = testDocument.CursorPosition.Value; var view = testDocument.GetTextView(); globalOptions?.SetGlobalOptions(workspace.GlobalOptions); var optionsFactory = workspace.GetService <IEditorOptionsFactoryService>(); var editorOptions = optionsFactory.GetOptions(view.TextBuffer); editorOptions.SetOptionValue(DefaultOptions.ConvertTabsToSpacesOptionId, !useTabs); editorOptions.SetOptionValue(DefaultOptions.NewLineCharacterOptionId, newLine); view.Options.SetOptionValue(DefaultOptions.TrimTrailingWhiteSpaceOptionId, trimTrailingWhiteSpace); if (testDocument.SelectedSpans.Any()) { var selectedSpan = testDocument.SelectedSpans[0]; var isReversed = selectedSpan.Start == startCaretPosition; view.Selection.Select(new SnapshotSpan(view.TextSnapshot, selectedSpan.Start, selectedSpan.Length), isReversed); } view.Caret.MoveTo(new SnapshotPoint(view.TextSnapshot, testDocument.CursorPosition.Value)); execute( workspace, view, workspace.GetService <IEditorOperationsFactoryService>()); MarkupTestFile.GetPosition(expectedMarkup, out var expectedCode, out int _); var actual = view.TextSnapshot.GetText(); Assert.Equal(expectedCode, actual); var endCaretPosition = view.Caret.Position.BufferPosition.Position; var actualWithCaret = actual.Insert(endCaretPosition, "$$"); Assert.Equal(expectedMarkup, actualWithCaret); } }
public EventHookupTestState(XElement workspaceElement, OptionsCollection options) : base(workspaceElement, s_composition) { _commandHandler = new EventHookupCommandHandler( Workspace.GetService <IThreadingContext>(), Workspace.GetService <IInlineRenameService>(), Workspace.GetService <EventHookupSessionManager>(), Workspace.GetService <IGlobalOptionService>(), Workspace.GetService <IAsynchronousOperationListenerProvider>()); _testSessionHookupMutex = new Mutex(false); _commandHandler.TESTSessionHookupMutex = _testSessionHookupMutex; options?.SetGlobalOptions(Workspace.GlobalOptions); }
public static ExtractInterfaceTestState Create( string markup, string languageName, CompilationOptions compilationOptions = null, ParseOptions parseOptions = null, OptionsCollection options = null) { var workspace = languageName == LanguageNames.CSharp ? TestWorkspace.CreateCSharp(markup, composition: Composition, compilationOptions: compilationOptions, parseOptions: parseOptions) : TestWorkspace.CreateVisualBasic(markup, composition: Composition, compilationOptions: compilationOptions, parseOptions: parseOptions); options?.SetGlobalOptions(workspace.GlobalOptions); return(new ExtractInterfaceTestState(workspace)); }
internal static Holder CreateSession(TestWorkspace workspace, char opening, char closing, OptionsCollection globalOptions = null) { var document = workspace.Documents.First(); var provider = Assert.IsType <BraceCompletionSessionProvider>(workspace.GetService <IBraceCompletionSessionProvider>()); var openingPoint = new SnapshotPoint(document.GetTextBuffer().CurrentSnapshot, document.CursorPosition.Value); var textView = document.GetTextView(); globalOptions?.SetGlobalOptions(workspace.GlobalOptions); workspace.GlobalOptions.SetEditorOptions(textView.Options.GlobalOptions, document.Project.Language); if (provider.TryCreateSession(textView, openingPoint, opening, closing, out var session)) { return(new Holder(workspace, session)); } workspace.Dispose(); return(null); }
internal async Task TestAsync(string testCode, string expected, OptionsCollection?options = null, ParseOptions?parseOptions = null) { using var workspace = CreateTestWorkspace(testCode, parseOptions); options?.SetGlobalOptions(workspace.GlobalOptions); var solution = workspace.CurrentSolution; var document = workspace.CurrentSolution.Projects.First().Documents.First(); var languageServices = document.Project.LanguageServices; var cleanupOptions = options?.ToAnalyzerConfigOptions(languageServices).GetCodeCleanupOptions(allowImportsInHiddenRegions: false, fallbackOptions: null, languageServices) ?? CodeCleanupOptions.GetDefault(languageServices); var formattingService = document.GetRequiredLanguageService <INewDocumentFormattingService>(); var formattedDocument = await formattingService.FormatNewDocumentAsync(document, hintDocument : null, cleanupOptions, CancellationToken.None); var actual = await formattedDocument.GetTextAsync(); AssertEx.EqualOrDiff(expected, actual.ToString()); }
internal static async Task VerifyExpectedTextAsync( string intentName, string activeDocument, string[] additionalDocuments, string[] expectedTexts, OptionsCollection?options = null, string?intentData = null, string?priorText = null) { var documentSet = additionalDocuments.Prepend(activeDocument).ToArray(); using var workspace = TestWorkspace.CreateCSharp(documentSet, composition: EditorTestCompositions.EditorFeatures); options?.SetGlobalOptions(workspace.GlobalOptions); var intentSource = workspace.ExportProvider.GetExportedValue <IIntentSourceProvider>(); // The first document will be the active document. var document = workspace.Documents.Single(d => d.Name == "test1.cs"); var textBuffer = document.GetTextBuffer(); // Get the text change to rewind the document to the correct pre-intent location. var rewindTextChange = new TextChange(document.AnnotatedSpans["typed"].Single(), priorText ?? string.Empty); // Get the current snapshot span to pass in. var currentSnapshot = new SnapshotSpan(textBuffer.CurrentSnapshot, new Span(0, textBuffer.CurrentSnapshot.Length)); var priorSelection = TextSpan.FromBounds(rewindTextChange.Span.Start, rewindTextChange.Span.Start); if (document.AnnotatedSpans.ContainsKey("priorSelection")) { priorSelection = document.AnnotatedSpans["priorSelection"].Single(); } var intentContext = new IntentRequestContext( intentName, currentSnapshot, ImmutableArray.Create(rewindTextChange), priorSelection, intentData: intentData); var results = await intentSource.ComputeIntentsAsync(intentContext, CancellationToken.None).ConfigureAwait(false); // For now, we're just taking the first result to match intellicode behavior. var result = results.First(); var actualDocumentTexts = new List <string>(); foreach (var documentChange in result.DocumentChanges) { // Get the document and open it. Since we're modifying the text buffer we don't care about linked documents. var documentBuffer = workspace.GetTestDocument(documentChange.Key).GetTextBuffer(); using var edit = documentBuffer.CreateEdit(); foreach (var change in documentChange.Value) { edit.Replace(change.Span.ToSpan(), change.NewText); } edit.Apply(); actualDocumentTexts.Add(documentBuffer.CurrentSnapshot.GetText()); } Assert.Equal(expectedTexts.Length, actualDocumentTexts.Count); foreach (var expectedText in expectedTexts) { Assert.True(actualDocumentTexts.Contains(expectedText)); } }