Exemplo n.º 1
0
        public static Task <TestWorkspace> CreateWorkspaceAsync(
            XElement workspaceElement,
            bool completed                = true,
            bool openDocuments            = true,
            ExportProvider exportProvider = null,
            string workspaceKind          = null)
        {
            if (workspaceElement.Name != WorkspaceElementName)
            {
                throw new ArgumentException();
            }

            exportProvider = exportProvider ?? TestExportProvider.ExportProviderWithCSharpAndVisualBasic;

            var workspace = new TestWorkspace(exportProvider, workspaceKind);

            var projectMap = new Dictionary <string, TestHostProject>();
            var documentElementToFilePath    = new Dictionary <XElement, string>();
            var projectElementToAssemblyName = new Dictionary <XElement, string>();
            var filePathToTextBufferMap      = new Dictionary <string, ITextBuffer>();
            int projectIdentifier            = 0;
            int documentIdentifier           = 0;

            foreach (var projectElement in workspaceElement.Elements(ProjectElementName))
            {
                var project = CreateProject(
                    workspaceElement,
                    projectElement,
                    exportProvider,
                    workspace,
                    projectElementToAssemblyName,
                    documentElementToFilePath,
                    filePathToTextBufferMap,
                    ref projectIdentifier,
                    ref documentIdentifier);
                Assert.False(projectMap.ContainsKey(project.AssemblyName));
                projectMap.Add(project.AssemblyName, project);
                workspace.Projects.Add(project);
            }

            var documentFilePaths = new HashSet <string>();

            foreach (var project in projectMap.Values)
            {
                foreach (var document in project.Documents)
                {
                    Assert.True(document.IsLinkFile || documentFilePaths.Add(document.FilePath));
                }
            }

            var submissions = CreateSubmissions(workspace, workspaceElement.Elements(SubmissionElementName), exportProvider);

            foreach (var submission in submissions)
            {
                projectMap.Add(submission.AssemblyName, submission);
            }

            var solution = new TestHostSolution(projectMap.Values.ToArray());

            workspace.AddTestSolution(solution);

            foreach (var projectElement in workspaceElement.Elements(ProjectElementName))
            {
                foreach (var projectReference in projectElement.Elements(ProjectReferenceElementName))
                {
                    var fromName = projectElementToAssemblyName[projectElement];
                    var toName   = projectReference.Value;

                    var fromProject = projectMap[fromName];
                    var toProject   = projectMap[toName];

                    var aliases = projectReference.Attributes(AliasAttributeName).Select(a => a.Value).ToImmutableArray();

                    workspace.OnProjectReferenceAdded(fromProject.Id, new ProjectReference(toProject.Id, aliases.Any() ? aliases : default(ImmutableArray <string>)));
                }
            }

            for (int i = 1; i < submissions.Count; i++)
            {
                if (submissions[i].CompilationOptions == null)
                {
                    continue;
                }

                for (int j = i - 1; j >= 0; j--)
                {
                    if (submissions[j].CompilationOptions != null)
                    {
                        workspace.OnProjectReferenceAdded(submissions[i].Id, new ProjectReference(submissions[j].Id));
                        break;
                    }
                }
            }

            foreach (var project in projectMap.Values)
            {
                foreach (var document in project.Documents)
                {
                    if (openDocuments)
                    {
                        workspace.OnDocumentOpened(document.Id, document.GetOpenTextContainer(), isCurrentContext: !document.IsLinkFile);
                    }

                    workspace.Documents.Add(document);
                }
            }

            return(Task.FromResult(workspace));
        }
        public static TestWorkspace CreateWorkspace(
            XElement workspaceElement,
            bool completed = true,
            bool openDocuments = true,
            ExportProvider exportProvider = null,
            string workspaceKind = null)
        {
            if (workspaceElement.Name != WorkspaceElementName)
            {
                throw new ArgumentException();
            }

            exportProvider = exportProvider ?? TestExportProvider.ExportProviderWithCSharpAndVisualBasic;

            var workspace = new TestWorkspace(exportProvider, workspaceKind);

            var projectMap = new Dictionary<string, TestHostProject>();
            var documentElementToFilePath = new Dictionary<XElement, string>();
            var projectElementToAssemblyName = new Dictionary<XElement, string>();
            var filePathToTextBufferMap = new Dictionary<string, ITextBuffer>();
            int projectIdentifier = 0;
            int documentIdentifier = 0;

            foreach (var projectElement in workspaceElement.Elements(ProjectElementName))
            {
                var project = CreateProject(
                    workspaceElement,
                    projectElement,
                    exportProvider,
                    workspace,
                    projectElementToAssemblyName,
                    documentElementToFilePath,
                    filePathToTextBufferMap,
                    ref projectIdentifier,
                    ref documentIdentifier);
                Assert.False(projectMap.ContainsKey(project.AssemblyName));
                projectMap.Add(project.AssemblyName, project);
                workspace.Projects.Add(project);
            }

            var documentFilePaths = new HashSet<string>();
            foreach (var project in projectMap.Values)
            {
                foreach (var document in project.Documents)
                {
                    Assert.True(document.IsLinkFile || documentFilePaths.Add(document.FilePath));
                }
            }

            var submissions = CreateSubmissions(workspace, workspaceElement.Elements(SubmissionElementName), exportProvider);

            foreach (var submission in submissions)
            {
                projectMap.Add(submission.AssemblyName, submission);
            }

            var solution = new TestHostSolution(projectMap.Values.ToArray());
            workspace.AddTestSolution(solution);

            foreach (var projectElement in workspaceElement.Elements(ProjectElementName))
            {
                foreach (var projectReference in projectElement.Elements(ProjectReferenceElementName))
                {
                    var fromName = projectElementToAssemblyName[projectElement];
                    var toName = projectReference.Value;

                    var fromProject = projectMap[fromName];
                    var toProject = projectMap[toName];

                    var aliases = projectReference.Attributes(AliasAttributeName).Select(a => a.Value).ToImmutableArray();

                    workspace.OnProjectReferenceAdded(fromProject.Id, new ProjectReference(toProject.Id, aliases.Any() ? aliases : default(ImmutableArray<string>)));
                }
            }

            for (int i = 1; i < submissions.Count; i++)
            {
                if (submissions[i].CompilationOptions == null)
                {
                    continue;
                }

                for (int j = i - 1; j >= 0; j--)
                {
                    if (submissions[j].CompilationOptions != null)
                    {
                        workspace.OnProjectReferenceAdded(submissions[i].Id, new ProjectReference(submissions[j].Id));
                        break;
                    }
                }
            }

            foreach (var project in projectMap.Values)
            {
                foreach (var document in project.Documents)
                {
                    if (openDocuments)
                    {
                        workspace.OnDocumentOpened(document.Id, document.GetOpenTextContainer(), isCurrentContext: !document.IsLinkFile);
                    }

                    workspace.Documents.Add(document);
                }
            }

            return workspace;
        }