|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | +using Microsoft.CodeAnalysis.Text; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Collections.Immutable; |
| 12 | +using System.Linq; |
| 13 | + |
| 14 | +namespace TestHelper |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Class for turning strings into documents and getting the diagnostics on them |
| 18 | + /// All methods are static |
| 19 | + /// </summary> |
| 20 | + public abstract partial class DiagnosticVerifier |
| 21 | + { |
| 22 | + private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); |
| 23 | + private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location); |
| 24 | + private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location); |
| 25 | + private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location); |
| 26 | + |
| 27 | + internal static string DefaultFilePathPrefix = "Test"; |
| 28 | + internal static string CSharpDefaultFileExt = "cs"; |
| 29 | + internal static string VisualBasicDefaultExt = "vb"; |
| 30 | + internal static string TestProjectName = "TestProject"; |
| 31 | + |
| 32 | + #region Get Diagnostics |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Given classes in the form of strings, their language, and an IDiagnosticAnalyzer to apply to it, return the diagnostics found in the string after converting it to a document. |
| 36 | + /// </summary> |
| 37 | + /// <param name="sources">Classes in the form of strings</param> |
| 38 | + /// <param name="language">The language the source classes are in</param> |
| 39 | + /// <param name="analyzer">The analyzer to be run on the sources</param> |
| 40 | + /// <returns>An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location</returns> |
| 41 | + private static Diagnostic[] GetSortedDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer) |
| 42 | + { |
| 43 | + return GetSortedDiagnosticsFromDocuments(analyzer, GetDocuments(sources, language)); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Given an analyzer and a document to apply it to, run the analyzer and gather an array of diagnostics found in it. |
| 48 | + /// The returned diagnostics are then ordered by location in the source document. |
| 49 | + /// </summary> |
| 50 | + /// <param name="analyzer">The analyzer to run on the documents</param> |
| 51 | + /// <param name="documents">The Documents that the analyzer will be run on</param> |
| 52 | + /// <returns>An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location</returns> |
| 53 | + protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents) |
| 54 | + { |
| 55 | + var projects = new HashSet<Project>(); |
| 56 | + foreach (var document in documents) |
| 57 | + { |
| 58 | + projects.Add(document.Project); |
| 59 | + } |
| 60 | + |
| 61 | + var diagnostics = new List<Diagnostic>(); |
| 62 | + foreach (var project in projects) |
| 63 | + { |
| 64 | + var compilationWithAnalyzers = project.GetCompilationAsync().Result.WithAnalyzers(ImmutableArray.Create(analyzer)); |
| 65 | + var diags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result; |
| 66 | + foreach (var diag in diags) |
| 67 | + { |
| 68 | + if (diag.Location == Location.None || diag.Location.IsInMetadata) |
| 69 | + { |
| 70 | + diagnostics.Add(diag); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + for (int i = 0; i < documents.Length; i++) |
| 75 | + { |
| 76 | + var document = documents[i]; |
| 77 | + var tree = document.GetSyntaxTreeAsync().Result; |
| 78 | + if (tree == diag.Location.SourceTree) |
| 79 | + { |
| 80 | + diagnostics.Add(diag); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + var results = SortDiagnostics(diagnostics); |
| 88 | + diagnostics.Clear(); |
| 89 | + return results; |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Sort diagnostics by location in source document |
| 94 | + /// </summary> |
| 95 | + /// <param name="diagnostics">The list of Diagnostics to be sorted</param> |
| 96 | + /// <returns>An IEnumerable containing the Diagnostics in order of Location</returns> |
| 97 | + private static Diagnostic[] SortDiagnostics(IEnumerable<Diagnostic> diagnostics) |
| 98 | + { |
| 99 | + return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); |
| 100 | + } |
| 101 | + |
| 102 | + #endregion |
| 103 | + |
| 104 | + #region Set up compilation and documents |
| 105 | + /// <summary> |
| 106 | + /// Given an array of strings as sources and a language, turn them into a project and return the documents and spans of it. |
| 107 | + /// </summary> |
| 108 | + /// <param name="sources">Classes in the form of strings</param> |
| 109 | + /// <param name="language">The language the source code is in</param> |
| 110 | + /// <returns>A Tuple containing the Documents produced from the sources and their TextSpans if relevant</returns> |
| 111 | + private static Document[] GetDocuments(string[] sources, string language) |
| 112 | + { |
| 113 | + if (language != LanguageNames.CSharp && language != LanguageNames.VisualBasic) |
| 114 | + { |
| 115 | + throw new ArgumentException("Unsupported Language"); |
| 116 | + } |
| 117 | + |
| 118 | + var project = CreateProject(sources, language); |
| 119 | + var documents = project.Documents.ToArray(); |
| 120 | + |
| 121 | + if (sources.Length != documents.Length) |
| 122 | + { |
| 123 | + throw new InvalidOperationException("Amount of sources did not match amount of Documents created"); |
| 124 | + } |
| 125 | + |
| 126 | + return documents; |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Create a Document from a string through creating a project that contains it. |
| 131 | + /// </summary> |
| 132 | + /// <param name="source">Classes in the form of a string</param> |
| 133 | + /// <param name="language">The language the source code is in</param> |
| 134 | + /// <returns>A Document created from the source string</returns> |
| 135 | + protected static Document CreateDocument(string source, string language = LanguageNames.CSharp) |
| 136 | + { |
| 137 | + return CreateProject(new[] { source }, language).Documents.First(); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Create a project using the inputted strings as sources. |
| 142 | + /// </summary> |
| 143 | + /// <param name="sources">Classes in the form of strings</param> |
| 144 | + /// <param name="language">The language the source code is in</param> |
| 145 | + /// <returns>A Project created out of the Documents created from the source strings</returns> |
| 146 | + private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp) |
| 147 | + { |
| 148 | + string fileNamePrefix = DefaultFilePathPrefix; |
| 149 | + string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; |
| 150 | + |
| 151 | + var projectId = ProjectId.CreateNewId(debugName: TestProjectName); |
| 152 | + |
| 153 | + var solution = new AdhocWorkspace() |
| 154 | + .CurrentSolution |
| 155 | + .AddProject(projectId, TestProjectName, TestProjectName, language) |
| 156 | + .AddMetadataReference(projectId, CorlibReference) |
| 157 | + .AddMetadataReference(projectId, SystemCoreReference) |
| 158 | + .AddMetadataReference(projectId, CSharpSymbolsReference) |
| 159 | + .AddMetadataReference(projectId, CodeAnalysisReference); |
| 160 | + |
| 161 | + int count = 0; |
| 162 | + foreach (var source in sources) |
| 163 | + { |
| 164 | + var newFileName = fileNamePrefix + count + "." + fileExt; |
| 165 | + var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); |
| 166 | + solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); |
| 167 | + count++; |
| 168 | + } |
| 169 | + return solution.GetProject(projectId); |
| 170 | + } |
| 171 | + #endregion |
| 172 | + } |
| 173 | +} |
| 174 | + |
0 commit comments