Пример #1
0
        protected async Task VerifyRefactoringAsync(string sourceWithMarkup, string expected, CancellationToken cancellationToken = default)
        {
            TestFileMarkupParser.GetPositionAndSpans(sourceWithMarkup, out string source, cursorPosition: out _, out IList <TextSpan> textSpans);
            var textSpan = Assert.Single(textSpans);

            var document = CreateDocument(source, LanguageNames.CSharp);
            var actions  = new List <CodeAction>();
            var context  = new CodeRefactoringContext(document, textSpan, (a) => actions.Add(a), cancellationToken);
            var codeRefactoringProvider = GetCodeRefactoringProvider();
            await codeRefactoringProvider.ComputeRefactoringsAsync(context);

            var codeAction = actions.First();
            var operations = await codeAction.GetOperationsAsync(cancellationToken);

            var solution = operations.OfType <ApplyChangesOperation>().Single().ChangedSolution;

            document = solution.GetDocument(document.Id);

            var newDocumentString = getStringFromDocument(document);

            Assert.Equal(expected, newDocumentString);
        private static TestHostDocument CreateDocument(
            TestWorkspace workspace,
            XElement workspaceElement,
            XElement documentElement,
            ExportProvider exportProvider,
            HostLanguageServices languageServiceProvider,
            IDocumentServiceProvider documentServiceProvider,
            ref int documentId)
        {
            var isLinkFileAttribute = documentElement.Attribute(IsLinkFileAttributeName);
            var isLinkFile          = isLinkFileAttribute != null && ((bool?)isLinkFileAttribute).HasValue && ((bool?)isLinkFileAttribute).Value;

            if (isLinkFile)
            {
                // This is a linked file. Use the filePath and markup from the referenced document.

                var originalAssemblyName = documentElement.Attribute(LinkAssemblyNameAttributeName)?.Value;
                var originalProjectName  = documentElement.Attribute(LinkProjectNameAttributeName)?.Value;

                if (originalAssemblyName == null && originalProjectName == null)
                {
                    throw new ArgumentException($"Linked files must specify either a {LinkAssemblyNameAttributeName} or {LinkProjectNameAttributeName}");
                }

                var originalProject = workspaceElement.Elements(ProjectElementName).FirstOrDefault(p =>
                {
                    if (originalAssemblyName != null)
                    {
                        return(p.Attribute(AssemblyNameAttributeName)?.Value == originalAssemblyName);
                    }
                    else
                    {
                        return(p.Attribute(ProjectNameAttribute)?.Value == originalProjectName);
                    }
                });

                if (originalProject == null)
                {
                    if (originalProjectName != null)
                    {
                        throw new ArgumentException($"Linked file's {LinkProjectNameAttributeName} '{originalProjectName}' project not found.");
                    }
                    else
                    {
                        throw new ArgumentException($"Linked file's {LinkAssemblyNameAttributeName} '{originalAssemblyName}' project not found.");
                    }
                }

                var originalDocumentPath = documentElement.Attribute(LinkFilePathAttributeName)?.Value;

                if (originalDocumentPath == null)
                {
                    throw new ArgumentException($"Linked files must specify a {LinkFilePathAttributeName}");
                }

                documentElement = originalProject.Elements(DocumentElementName).FirstOrDefault(d =>
                {
                    return(d.Attribute(FilePathAttributeName)?.Value == originalDocumentPath);
                });

                if (documentElement == null)
                {
                    throw new ArgumentException($"Linked file's LinkFilePath '{originalDocumentPath}' file not found.");
                }
            }

            var markupCode = documentElement.NormalizedValue();
            var fileName   = GetFileName(workspace, documentElement, ref documentId);

            var folders        = GetFolders(documentElement);
            var optionsElement = documentElement.Element(ParseOptionsElementName);

            // TODO: Allow these to be specified.
            var codeKind = SourceCodeKind.Regular;

            if (optionsElement != null)
            {
                var attr = optionsElement.Attribute(KindAttributeName);
                codeKind = attr == null
                    ? SourceCodeKind.Regular
                    : (SourceCodeKind)Enum.Parse(typeof(SourceCodeKind), attr.Value);
            }

            TestFileMarkupParser.GetPositionAndSpans(markupCode,
                                                     out var code, out int?cursorPosition, out ImmutableDictionary <string, ImmutableArray <TextSpan> > spans);

            var testDocumentServiceProvider = GetDocumentServiceProvider(documentElement);

            if (documentServiceProvider == null)
            {
                documentServiceProvider = testDocumentServiceProvider;
            }
            else if (testDocumentServiceProvider != null)
            {
                AssertEx.Fail($"The document attributes on file {fileName} conflicted");
            }

            return(new TestHostDocument(
                       exportProvider, languageServiceProvider, code, fileName, fileName, cursorPosition, spans, codeKind, folders, isLinkFile, documentServiceProvider));
        }