示例#1
0
        /// <summary>
        /// Create a project using the inputted strings as sources.
        /// </summary>
        /// <param name="sources">Classes in the form of strings</param>
        /// <param name="language">The language the source code is in</param>
        /// <param name="solution">The created workspace containing the project</param>
        /// <returns>A Project created out of the Douments created from the source strings</returns>
        public static Project CreateProject(string[] sources,
                                            out CustomWorkspace workspace, string language = LanguageNames.CSharp)
        {
            var fileNamePrefix = DefaultFilePathPrefix;
            var fileExt        = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;

            var projectId = ProjectId.CreateNewId(debugName: TestProjectName);

            workspace = new CustomWorkspace();

            var projectInfo = ProjectInfo.Create(projectId, VersionStamp.Create(), TestProjectName,
                                                 TestProjectName, language,
                                                 metadataReferences: ImmutableList.Create(
                                                     CorlibReference, SystemCoreReference, RegexReference,
                                                     CSharpSymbolsReference, CodeAnalysisReference, JsonNetReference));

            workspace.AddProject(projectInfo);

            const int count = 0;

            foreach (var source in sources)
            {
                var newFileName = fileNamePrefix + count + "." + fileExt;
                workspace.AddDocument(projectId, newFileName, SourceText.From(source));
            }

            var project = workspace.CurrentSolution.GetProject(projectId);
            var newCompilationOptions = project.CompilationOptions.WithSpecificDiagnosticOptions(diagOptions);
            var solution   = workspace.CurrentSolution.WithProjectCompilationOptions(projectId, newCompilationOptions);
            var newProject = solution.GetProject(projectId);

            return(newProject);
        }
        /// <summary>
        /// Create a project using the inputted strings as sources.
        /// </summary>
        /// <param name="sources">Classes in the form of strings</param>
        /// <param name="language">The language the source code is in</param>
        /// <returns>A Project created out of the Douments created from the source strings</returns>
        private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp)
        {
            string fileNamePrefix = DefaultFilePathPrefix;
            string fileExt        = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;

            var projectId = ProjectId.CreateNewId(debugName: TestProjectName);

            var solution = new CustomWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, language)
                           .AddMetadataReference(projectId, CorlibReference)
                           .AddMetadataReference(projectId, SystemCoreReference)
                           .AddMetadataReference(projectId, CSharpSymbolsReference)
                           .AddMetadataReference(projectId, CodeAnalysisReference);

            int count = 0;

            foreach (var source in sources)
            {
                var newFileName = fileNamePrefix + count + "." + fileExt;
                var documentId  = DocumentId.CreateNewId(projectId, debugName: newFileName);
                solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
                count++;
            }
            return(solution.GetProject(projectId));
        }
示例#3
0
        public void TestAddDocument_DocumentInfo()
        {
            using (var ws = new CustomWorkspace())
            {
                var project = ws.AddProject("TestProject", LanguageNames.CSharp);
                var info = DocumentInfo.Create(DocumentId.CreateNewId(project.Id), "code.cs");
                var doc = ws.AddDocument(info);

                Assert.Equal(ws.CurrentSolution.GetDocument(info.Id), doc);
                Assert.Equal(info.Name, doc.Name);                
            }
        }
示例#4
0
        public void TestAddDocument_DocumentInfo()
        {
            using (var ws = new CustomWorkspace())
            {
                var project = ws.AddProject("TestProject", LanguageNames.CSharp);
                var info    = DocumentInfo.Create(DocumentId.CreateNewId(project.Id), "code.cs");
                var doc     = ws.AddDocument(info);

                Assert.Equal(ws.CurrentSolution.GetDocument(info.Id), doc);
                Assert.Equal(info.Name, doc.Name);
            }
        }
示例#5
0
        public void TestAddDocument_NameAndText()
        {
            using (var ws = new CustomWorkspace())
            {
                var project = ws.AddProject("TestProject", LanguageNames.CSharp);
                var name = "code.cs";
                var source = "class C {}";
                var doc = ws.AddDocument(project.Id, name, SourceText.From(source));

                Assert.Equal(name, doc.Name);
                Assert.Equal(source, doc.GetTextAsync().Result.ToString());
            }
        }
示例#6
0
        public void TestAddDocument_NameAndText()
        {
            using (var ws = new CustomWorkspace())
            {
                var project = ws.AddProject("TestProject", LanguageNames.CSharp);
                var name    = "code.cs";
                var source  = "class C {}";
                var doc     = ws.AddDocument(project.Id, name, SourceText.From(source));

                Assert.Equal(name, doc.Name);
                Assert.Equal(source, doc.GetTextAsync().Result.ToString());
            }
        }
        private Project CreateProject()
        {
            var projectId = ProjectId.CreateNewId(debugName: projectName);

            var solution = new CustomWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, projectName, projectName, LanguageNames.CSharp)
                           .AddMetadataReference(projectId, CorlibReference)
                           .AddMetadataReference(projectId, SystemCoreReference)
                           .AddMetadataReference(projectId, CSharpSymbolsReference)
                           .AddMetadataReference(projectId, CodeAnalysisReference);

            foreach (var file in files)
            {
                var documentId = DocumentId.CreateNewId(projectId, debugName: file.Path);
                solution = solution.AddDocument(documentId, file.Path, SourceText.From(file.Content));
            }
            return(solution.GetProject(projectId));
        }
示例#8
0
        private Project CreateSolution(string source)
        {
            var testProjectName = "Test";
            var projectId       = ProjectId.CreateNewId(testProjectName);

            var references = new[]
            {
                s_CorlibReference,
                s_SystemCoreReference,
                s_MSTestReference,
                s_XunitReference
            };

            var solution = new CustomWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, testProjectName, testProjectName, LanguageNames.CSharp)
                           .AddMetadataReferences(projectId, references);

            var fileName   = "File.cs";
            var documentId = DocumentId.CreateNewId(projectId, fileName);

            solution = solution.AddDocument(documentId, fileName, SourceText.From(source));
            return(solution.GetProject(projectId));
        }
        protected static Project CreateProject(string[] sources, string language = LanguageNames.CSharp)
        {
            string fileNamePrefix = DefaultFilePathPrefix;
            string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;

            var projectId = ProjectId.CreateNewId(debugName: TestProjectName);

            var solution = new CustomWorkspace()
                .CurrentSolution
                .AddProject(projectId, TestProjectName, TestProjectName, language)
                .AddMetadataReference(projectId, CorlibReference)
                .AddMetadataReference(projectId, SystemCoreReference)
                .AddMetadataReference(projectId, CSharpSymbolsReference)
                .AddMetadataReference(projectId, TestBase.SystemRef);

            int count = 0;
            foreach (var source in sources)
            {
                var newFileName = fileNamePrefix + count + "." + fileExt;
                var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
                solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
                count++;
            }

            return solution.GetProject(projectId);
        }