|
| 1 | +using System; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using FluentAssertions.Execution; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Microsoft.CodeAnalysis.Text; |
| 9 | + |
| 10 | +using XunitAssert = Xunit.Assert; |
| 11 | +using System.Net.Http; |
| 12 | +using System.Collections.Concurrent; |
| 13 | +using System.Collections.ObjectModel; |
| 14 | + |
| 15 | +namespace FluentAssertions.Analyzers.TestUtils |
| 16 | +{ |
| 17 | + public class CsProjectGenerator |
| 18 | + { |
| 19 | + static CsProjectGenerator() |
| 20 | + { |
| 21 | + References = new[] |
| 22 | + { |
| 23 | + typeof(object), // System.Private.CoreLib |
| 24 | + typeof(Console), // System |
| 25 | + typeof(Uri), // System.Private.Uri |
| 26 | + typeof(Enumerable), // System.Linq |
| 27 | + typeof(CSharpCompilation), // Microsoft.CodeAnalysis.CSharp |
| 28 | + typeof(Compilation), // Microsoft.CodeAnalysis |
| 29 | + typeof(AssertionScope), // FluentAssertions.Core |
| 30 | + typeof(AssertionExtensions), // FluentAssertions |
| 31 | + typeof(HttpRequestMessage), // System.Net.Http |
| 32 | + typeof(ImmutableArray), // System.Collections.Immutable |
| 33 | + typeof(ConcurrentBag<>), // System.Collections.Concurrent |
| 34 | + typeof(ReadOnlyDictionary<,>), // System.ObjectModel |
| 35 | + typeof(Microsoft.VisualStudio.TestTools.UnitTesting.Assert), // MsTest |
| 36 | + typeof(XunitAssert), // Xunit |
| 37 | + }.Select(type => type.GetTypeInfo().Assembly.Location) |
| 38 | + .Append(GetSystemAssemblyPathByName("System.Globalization.dll")) |
| 39 | + .Append(GetSystemAssemblyPathByName("System.Text.RegularExpressions.dll")) |
| 40 | + .Append(GetSystemAssemblyPathByName("System.Runtime.Extensions.dll")) |
| 41 | + .Append(GetSystemAssemblyPathByName("System.Data.Common.dll")) |
| 42 | + .Append(GetSystemAssemblyPathByName("System.Threading.Tasks.dll")) |
| 43 | + .Append(GetSystemAssemblyPathByName("System.Runtime.dll")) |
| 44 | + .Append(GetSystemAssemblyPathByName("System.Reflection.dll")) |
| 45 | + .Append(GetSystemAssemblyPathByName("System.Xml.dll")) |
| 46 | + .Append(GetSystemAssemblyPathByName("System.Xml.XDocument.dll")) |
| 47 | + .Append(GetSystemAssemblyPathByName("System.Private.Xml.Linq.dll")) |
| 48 | + .Append(GetSystemAssemblyPathByName("System.Linq.Expressions.dll")) |
| 49 | + .Append(GetSystemAssemblyPathByName("System.Collections.dll")) |
| 50 | + .Append(GetSystemAssemblyPathByName("netstandard.dll")) |
| 51 | + .Append(GetSystemAssemblyPathByName("System.Xml.ReaderWriter.dll")) |
| 52 | + .Append(GetSystemAssemblyPathByName("System.Private.Xml.dll")) |
| 53 | + .Select(location => (MetadataReference)MetadataReference.CreateFromFile(location)) |
| 54 | + .ToImmutableArray(); |
| 55 | + |
| 56 | + string GetSystemAssemblyPathByName(string assemblyName) |
| 57 | + { |
| 58 | + var root = System.IO.Path.GetDirectoryName(typeof(object).Assembly.Location); |
| 59 | + return System.IO.Path.Combine(root, assemblyName); |
| 60 | + } |
| 61 | + } |
| 62 | + // based on http://code.fitness/post/2017/02/using-csharpscript-with-netstandard.html |
| 63 | + public static string GetSystemAssemblyPathByName(string assemblyName) |
| 64 | + { |
| 65 | + var root = System.IO.Path.GetDirectoryName(typeof(object).Assembly.Location); |
| 66 | + return System.IO.Path.Combine(root, assemblyName); |
| 67 | + } |
| 68 | + |
| 69 | + private static readonly ImmutableArray<MetadataReference> References; |
| 70 | + |
| 71 | + private static readonly string DefaultFilePathPrefix = "Test"; |
| 72 | + private static readonly string CSharpDefaultFileExt = "cs"; |
| 73 | + private static readonly string VisualBasicDefaultExt = "vb"; |
| 74 | + private static readonly string TestProjectName = "TestProject"; |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Given an array of strings as sources and a language, turn them into a project and return the documents and spans of it. |
| 78 | + /// </summary> |
| 79 | + /// <param name="sources">Classes in the form of strings</param> |
| 80 | + /// <param name="language">The language the source code is in</param> |
| 81 | + /// <returns>A Tuple containing the Documents produced from the sources and their TextSpans if relevant</returns> |
| 82 | + public static Document[] GetDocuments(string[] sources, string language) |
| 83 | + { |
| 84 | + if (language != LanguageNames.CSharp && language != LanguageNames.VisualBasic) |
| 85 | + { |
| 86 | + throw new ArgumentException("Unsupported Language"); |
| 87 | + } |
| 88 | + |
| 89 | + var project = CreateProject(sources, language); |
| 90 | + var documents = project.Documents.ToArray(); |
| 91 | + |
| 92 | + if (sources.Length != documents.Length) |
| 93 | + { |
| 94 | + throw new SystemException("Amount of sources did not match amount of Documents created"); |
| 95 | + } |
| 96 | + |
| 97 | + return documents; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Create a Document from a string through creating a project that contains it. |
| 102 | + /// </summary> |
| 103 | + /// <param name="source">Classes in the form of a string</param> |
| 104 | + /// <param name="language">The language the source code is in</param> |
| 105 | + /// <returns>A Document created from the source string</returns> |
| 106 | + public static Document CreateDocument(string source, string language = LanguageNames.CSharp) |
| 107 | + { |
| 108 | + return CreateProject(new[] { source }, language).Documents.First(); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Create a project using the inputted strings as sources. |
| 113 | + /// </summary> |
| 114 | + /// <param name="sources">Classes in the form of strings</param> |
| 115 | + /// <param name="language">The language the source code is in</param> |
| 116 | + /// <returns>A Project created out of the Documents created from the source strings</returns> |
| 117 | + public static Project CreateProject(string[] sources, string language = LanguageNames.CSharp) |
| 118 | + { |
| 119 | + string fileNamePrefix = DefaultFilePathPrefix; |
| 120 | + string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; |
| 121 | + |
| 122 | + var projectId = ProjectId.CreateNewId(debugName: TestProjectName); |
| 123 | + |
| 124 | + var solution = new AdhocWorkspace() |
| 125 | + .CurrentSolution |
| 126 | + .AddProject(projectId, TestProjectName, TestProjectName, language) |
| 127 | + .AddMetadataReferences(projectId, References); |
| 128 | + |
| 129 | + int count = 0; |
| 130 | + foreach (var source in sources) |
| 131 | + { |
| 132 | + var newFileName = fileNamePrefix + count + "." + fileExt; |
| 133 | + var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); |
| 134 | + solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); |
| 135 | + count++; |
| 136 | + } |
| 137 | + return solution.GetProject(projectId); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
0 commit comments