示例#1
0
 internal void AddTestSolution(TestHostSolution solution) =>
 this.OnSolutionAdded(
     SolutionInfo.Create(
         solution.Id,
         solution.Version,
         solution.FilePath,
         projects: solution.Projects.Select(p => p.ToProjectInfo())
         )
     );
示例#2
0
        internal void SetSolution(TestHostSolution solution)
        {
            // set up back pointer to this project.
            if (this.Documents != null)
            {
                foreach (var doc in this.Documents)
                {
                    doc.SetProject(this);
                }

                foreach (var doc in this.AdditionalDocuments)
                {
                    doc.SetProject(this);
                }
            }
        }
        public static async 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)
                    {
                        await workspace.OnDocumentOpenedAsync(document.Id, document.GetOpenTextContainer(), isCurrentContext : !document.IsLinkFile);
                    }

                    workspace.Documents.Add(document);
                }
            }

            return(workspace);
        }
        internal static TestWorkspace Create(
            XElement workspaceElement,
            bool completed                = true,
            bool openDocuments            = true,
            ExportProvider exportProvider = null,
            string workspaceKind          = null,
            IDocumentServiceProvider documentServiceProvider    = null,
            bool ignoreUnchangeableDocumentsWhenApplyingChanges = true)
        {
            if (workspaceElement.Name != WorkspaceElementName)
            {
                throw new ArgumentException();
            }

            exportProvider ??= TestExportProvider.ExportProviderWithCSharpAndVisualBasic;

            var workspace = new TestWorkspace(exportProvider, workspaceKind, ignoreUnchangeableDocumentsWhenApplyingChanges: ignoreUnchangeableDocumentsWhenApplyingChanges);

            var projectNameToTestHostProject = new Dictionary <string, TestHostProject>();
            var projectElementToProjectName  = new Dictionary <XElement, string>();
            var filePathToTextBufferMap      = new Dictionary <string, ITextBuffer>();
            var projectIdentifier            = 0;
            var documentIdentifier           = 0;

            foreach (var projectElement in workspaceElement.Elements(ProjectElementName))
            {
                var project = CreateProject(
                    workspaceElement,
                    projectElement,
                    exportProvider,
                    workspace,
                    filePathToTextBufferMap,
                    documentServiceProvider,
                    ref projectIdentifier,
                    ref documentIdentifier);
                Assert.False(projectNameToTestHostProject.ContainsKey(project.Name), $"The workspace XML already contains a project with name {project.Name}");
                projectNameToTestHostProject.Add(project.Name, project);
                projectElementToProjectName.Add(projectElement, project.Name);
                workspace.Projects.Add(project);
            }

            var documentFilePaths = new HashSet <string>();

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

                    workspace.Documents.Add(document);
                }
            }

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

            foreach (var submission in submissions)
            {
                projectNameToTestHostProject.Add(submission.Name, submission);
                workspace.Documents.Add(submission.Documents.Single());
            }

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

            workspace.AddTestSolution(solution);

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

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

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

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

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

                for (var 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 projectNameToTestHostProject.Values)
            {
                foreach (var document in project.Documents)
                {
                    if (openDocuments)
                    {
                        // This implicitly opens the document in the workspace by fetching the container.
                        document.GetOpenTextContainer();
                    }
                }
            }

            return(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;
        }
        internal void SetSolution(TestHostSolution solution)
        {
            // set up back pointer to this project.
            if (this.Documents != null)
            {
                foreach (var doc in this.Documents)
                {
                    doc.SetProject(this);
                }

                foreach (var doc in this.AdditionalDocuments)
                {
                    doc.SetProject(this);
                }
            }
        }
        internal void InitializeDocuments(
            XElement workspaceElement,
            bool openDocuments = true,
            IDocumentServiceProvider documentServiceProvider = null)
        {
            if (workspaceElement.Name != WorkspaceElementName)
            {
                throw new ArgumentException();
            }

            var projectNameToTestHostProject = new Dictionary <string, TestHostProject>();
            var projectElementToProjectName  = new Dictionary <XElement, string>();
            var projectIdentifier            = 0;
            var documentIdentifier           = 0;

            foreach (var projectElement in workspaceElement.Elements(ProjectElementName))
            {
                var project = CreateProject(
                    workspaceElement,
                    projectElement,
                    ExportProvider,
                    this,
                    documentServiceProvider,
                    ref projectIdentifier,
                    ref documentIdentifier);

                Assert.False(projectNameToTestHostProject.ContainsKey(project.Name), $"The workspace XML already contains a project with name {project.Name}");
                projectNameToTestHostProject.Add(project.Name, project);
                projectElementToProjectName.Add(projectElement, project.Name);
                Projects.Add(project);
            }

            var documentFilePaths = new HashSet <string>();

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

                    Documents.Add(document);
                }
            }

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

            foreach (var submission in submissions)
            {
                projectNameToTestHostProject.Add(submission.Name, submission);
                Documents.Add(submission.Documents.Single());
            }

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

            AddTestSolution(solution);

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

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

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

                    OnProjectReferenceAdded(fromProject.Id, new ProjectReference(toProject.Id, aliases.Any() ? aliases : default));
                }
            }

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

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

            foreach (var project in projectNameToTestHostProject.Values)
            {
                foreach (var document in project.Documents)
                {
                    if (openDocuments && !document.IsSourceGenerated)
                    {
                        // This implicitly opens the document in the workspace by fetching the container.
                        document.GetOpenTextContainer();
                    }
                }
            }
        }