Example #1
0
 internal RefactoringTestData(RefactoringTestData other)
     : this(
         source : other.Source,
         spans : other.Spans,
         additionalFiles : other.AdditionalFiles,
         equivalenceKey : other.EquivalenceKey)
 {
 }
Example #2
0
        /// <summary>
        /// Verifies that refactoring will be applied correctly using specified <typeparamref name="TRefactoringProvider"/>.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="expected"></param>
        /// <param name="options"></param>
        /// <param name="cancellationToken"></param>
        public async Task VerifyRefactoringAsync(
            RefactoringTestData data,
            ExpectedTestState expected,
            TestOptions options = null,
            CancellationToken cancellationToken = default)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (expected == null)
            {
                throw new ArgumentNullException(nameof(expected));
            }

            if (data.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 data.Spans)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (Workspace workspace = new AdhocWorkspace())
                {
                    (Document document, ImmutableArray <ExpectedDocument> expectedDocuments) = CreateDocument(workspace.CurrentSolution, data.Source, data.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 (data.EquivalenceKey == null ||
                            string.Equals(a.EquivalenceKey, data.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);
                        }
                    },