protected static Project CreateProject( FileAndSource[] sources, string language = LanguageNames.CSharp, bool addLanguageSpecificCodeAnalysisReference = true, Solution addToSolution = null, string projectName = _testProjectName) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; CompilationOptions options = language == LanguageNames.CSharp ? s_CSharpDefaultOptions : s_visualBasicDefaultOptions; ProjectId projectId = ProjectId.CreateNewId(debugName: projectName); Project project = (addToSolution ?? new AdhocWorkspace().CurrentSolution) .AddProject(projectId, projectName, projectName, language) .AddMetadataReference(projectId, s_corlibReference) .AddMetadataReference(projectId, s_systemCoreReference) .AddMetadataReference(projectId, s_systemXmlReference) .AddMetadataReference(projectId, s_systemXmlDataReference) .AddMetadataReference(projectId, s_codeAnalysisReference) .AddMetadataReference(projectId, SystemRuntimeFacadeRef) .AddMetadataReference(projectId, SystemThreadingFacadeRef) .AddMetadataReference(projectId, SystemThreadingTaskFacadeRef) //.AddMetadataReference(projectId, TestBase.SystemRef) //.AddMetadataReference(projectId, TestBase.SystemRuntimeFacadeRef) //.AddMetadataReference(projectId, TestBase.SystemThreadingFacadeRef) //.AddMetadataReference(projectId, TestBase.SystemThreadingTaskFacadeRef) .AddMetadataReference(projectId, s_immutableCollectionsReference) .AddMetadataReference(projectId, s_workspacesReference) .AddMetadataReference(projectId, s_systemDiagnosticsDebugReference) .AddMetadataReference(projectId, s_systemDataReference) .WithProjectCompilationOptions(projectId, options) .GetProject(projectId); // Enable IOperation Feature on the project var parseOptions = project.ParseOptions.WithFeatures(project.ParseOptions.Features.Concat(SpecializedCollections.SingletonEnumerable(KeyValuePair.Create("IOperation", "true")))); project = project.WithParseOptions(parseOptions); if (addLanguageSpecificCodeAnalysisReference) { MetadataReference symbolsReference = language == LanguageNames.CSharp ? s_csharpSymbolsReference : s_visualBasicSymbolsReference; project = project.AddMetadataReference(symbolsReference); } if (language == LanguageNames.VisualBasic) { project = project.AddMetadataReference(s_visualBasicReference); } int count = 0; foreach (FileAndSource source in sources) { string newFileName = source.FilePath ?? fileNamePrefix + count++ + "." + fileExt; DocumentId documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); project = project.AddDocument(newFileName, SourceText.From(source.Source)).Project; } return project; }
protected static Diagnostic[] GetSortedDiagnostics(FileAndSource[] sources, string language, DiagnosticAnalyzer analyzer, bool addLanguageSpecificCodeAnalysisReference = true, string projectName = _testProjectName) { Tuple<Document[], bool, TextSpan?[]> documentsAndUseSpan = GetDocumentsAndSpans(sources, language, addLanguageSpecificCodeAnalysisReference, projectName); Document[] documents = documentsAndUseSpan.Item1; bool useSpans = documentsAndUseSpan.Item2; TextSpan?[] spans = documentsAndUseSpan.Item3; return GetSortedDiagnostics(analyzer, documents, useSpans ? spans : null); }
protected static Tuple<Document[], bool, TextSpan?[]> GetDocumentsAndSpans(FileAndSource[] sources, string language, bool addLanguageSpecificCodeAnalysisReference = true, string projectName = _testProjectName) { Assert.True(language == LanguageNames.CSharp || language == LanguageNames.VisualBasic, "Unsupported language"); var spans = new TextSpan?[sources.Length]; bool useSpans = false; for (int i = 0; i < sources.Length; i++) { string fileName = language == LanguageNames.CSharp ? "Test" + i + ".cs" : "Test" + i + ".vb"; string source; int? pos; TextSpan? span; MarkupTestFile.GetPositionAndSpan(sources[i].Source, out source, out pos, out span); sources[i].Source = source; spans[i] = span; if (span != null) { useSpans = true; } } Project project = CreateProject(sources, language, addLanguageSpecificCodeAnalysisReference, null, projectName); Document[] documents = project.Documents.ToArray(); Assert.Equal(sources.Length, documents.Length); return Tuple.Create(documents, useSpans, spans); }
protected void Verify(FileAndSource[] sources, string language, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expected) { GetSortedDiagnostics(sources, language, analyzer).Verify(analyzer, PrintActualDiagnosticsOnFailure, ExpectedDiagnosticsAssertionTemplate, expected); }
protected void Verify(FileAndSource[] sources, string language, DiagnosticAnalyzer analyzer, bool addLanguageSpecificCodeAnalysisReference, params DiagnosticResult[] expected) { GetSortedDiagnostics(sources, language, analyzer, addLanguageSpecificCodeAnalysisReference).Verify(analyzer, PrintActualDiagnosticsOnFailure, ExpectedDiagnosticsAssertionTemplate, expected); }
protected void VerifyBasic(FileAndSource[] sources, params DiagnosticResult[] expected) { Verify(sources, LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), expected); }
protected void VerifyCSharp(FileAndSource[] sources, params DiagnosticResult[] expected) { Verify(sources, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), expected); }
protected static Project CreateProject(FileAndSource[] sources, string language = LanguageNames.CSharp, bool addLanguageSpecificCodeAnalysisReference = true, Solution addToSolution = null) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var options = language == LanguageNames.CSharp ? s_CSharpDefaultOptions : s_visualBasicDefaultOptions; var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var solution = (addToSolution ?? new AdhocWorkspace().CurrentSolution) .AddProject(projectId, TestProjectName, TestProjectName, language) .AddMetadataReference(projectId, s_corlibReference) .AddMetadataReference(projectId, s_systemCoreReference) .AddMetadataReference(projectId, s_systemXmReference) .AddMetadataReference(projectId, s_codeAnalysisReference) .AddMetadataReference(projectId, SystemRuntimeFacadeRef) .AddMetadataReference(projectId, SystemThreadingFacadeRef) .AddMetadataReference(projectId, SystemThreadingTaskFacadeRef) //.AddMetadataReference(projectId, TestBase.SystemRef) //.AddMetadataReference(projectId, TestBase.SystemRuntimeFacadeRef) //.AddMetadataReference(projectId, TestBase.SystemThreadingFacadeRef) //.AddMetadataReference(projectId, TestBase.SystemThreadingTaskFacadeRef) .AddMetadataReference(projectId, s_immutableCollectionsReference) .AddMetadataReference(projectId, s_workspacesReference) .AddMetadataReference(projectId, s_systemDiagnosticsDebugReference) .WithProjectCompilationOptions(projectId, options); if (addLanguageSpecificCodeAnalysisReference) { var symbolsReference = language == LanguageNames.CSharp ? s_CSharpSymbolsReference : s_visualBasicSymbolsReference; var project = solution.GetProject(projectId); project = project.AddMetadataReference(symbolsReference); solution = project.Solution; } int count = 0; foreach (var source in sources) { var newFileName = source.FilePath ?? fileNamePrefix + count++ + "." + fileExt; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source.Source)); } return solution.GetProject(projectId); }
protected static Diagnostic[] GetSortedDiagnostics(FileAndSource[] sources, string language, DiagnosticAnalyzer analyzer, bool addLanguageSpecificCodeAnalysisReference = true) { var documentsAndUseSpan = GetDocumentsAndSpans(sources, language, addLanguageSpecificCodeAnalysisReference); var documents = documentsAndUseSpan.Item1; var useSpans = documentsAndUseSpan.Item2; var spans = documentsAndUseSpan.Item3; return GetSortedDiagnostics(analyzer, documents, useSpans ? spans : null); }
protected void Verify(FileAndSource[] sources, string language, DiagnosticAnalyzer analyzer, bool addLanguageSpecificCodeAnalysisReference, params DiagnosticResult[] expected) { GetSortedDiagnostics(sources, language, analyzer, addLanguageSpecificCodeAnalysisReference).Verify(analyzer, expected); }
protected void Verify(FileAndSource[] sources, string language, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expected) { GetSortedDiagnostics(sources, language, analyzer).Verify(analyzer, expected); }
protected void Verify(FileAndSource[] sources, string language, DiagnosticAnalyzer analyzer, IEnumerable<TestAdditionalDocument> additionalFiles, params DiagnosticResult[] expected) { GetSortedDiagnostics(sources, language, analyzer, additionalFiles: additionalFiles).Verify(analyzer, PrintActualDiagnosticsOnFailure, ExpectedDiagnosticsAssertionTemplate, expected); }