internal RefactoringTestState(RefactoringTestState other) : this( source : other.Source, spans : other.Spans, additionalFiles : other.AdditionalFiles, equivalenceKey : other.EquivalenceKey) { }
/// <summary> /// Verifies that refactoring will be applied correctly using specified <typeparamref name="TRefactoringProvider"/>. /// </summary> /// <param name="state"></param> /// <param name="expected"></param> /// <param name="options"></param> /// <param name="cancellationToken"></param> public async Task VerifyRefactoringAsync( RefactoringTestState state, ExpectedTestState expected, TestOptions options = null, CancellationToken cancellationToken = default) { if (state.Spans.IsEmpty) { Fail("Span on which a refactoring should be invoked was not found."); } options ??= Options; TRefactoringProvider refactoringProvider = Activator.CreateInstance <TRefactoringProvider>(); foreach (TextSpan span in state.Spans) { cancellationToken.ThrowIfCancellationRequested(); using (Workspace workspace = new AdhocWorkspace()) { (Document document, ImmutableArray <ExpectedDocument> expectedDocuments) = CreateDocument(workspace.CurrentSolution, state.Source, state.AdditionalFiles, options); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(compilerDiagnostics, options); CodeAction action = null; List <CodeAction> candidateActions = null; var context = new CodeRefactoringContext( document, span, a => { if (state.EquivalenceKey == null || string.Equals(a.EquivalenceKey, state.EquivalenceKey, StringComparison.Ordinal)) { if (action != null) { Fail($"Multiple refactorings registered by '{refactoringProvider.GetType().Name}'.", new CodeAction[] { action, a }); } action = a; } else { (candidateActions ??= new List <CodeAction>()).Add(a); } },
/// <summary> /// Verifies that refactoring will not be applied using specified <typeparamref name="TRefactoringProvider"/>. /// </summary> /// <param name="state"></param> /// <param name="options"></param> /// <param name="cancellationToken"></param> public async Task VerifyNoRefactoringAsync( RefactoringTestState state, TestOptions options = null, CancellationToken cancellationToken = default) { if (state.Spans.IsEmpty) { Assert.True(false, "Span on which a refactoring should be invoked was not found."); } cancellationToken.ThrowIfCancellationRequested(); options ??= Options; TRefactoringProvider refactoringProvider = Activator.CreateInstance <TRefactoringProvider>(); using (Workspace workspace = new AdhocWorkspace()) { (Document document, ImmutableArray <ExpectedDocument> expectedDocuments) = CreateDocument(workspace.CurrentSolution, state.Source, state.AdditionalFiles, options); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(compilerDiagnostics, options); foreach (TextSpan span in state.Spans) { cancellationToken.ThrowIfCancellationRequested(); var context = new CodeRefactoringContext( document, span, a => { if (state.EquivalenceKey == null || string.Equals(a.EquivalenceKey, state.EquivalenceKey, StringComparison.Ordinal)) { Assert.True(false, "No code refactoring expected."); } }, cancellationToken); await refactoringProvider.ComputeRefactoringsAsync(context); } } }