From 6c98832a87451c6f45e1ba294248782af44baf56 Mon Sep 17 00:00:00 2001 From: lindexi Date: Fri, 2 Feb 2024 17:05:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => Assets}/TestCode.cs | 44 +++++------ .../TelescopeIncrementalGeneratorTest.cs | 77 +++++++++++++++++++ .../Utils/MetadataReferenceProvider.cs | 37 +++++++++ .../Utils/TestCodeProvider.cs | 13 ++++ ...mpus.Telescope.SourceGenerator.Test.csproj | 25 +++++- 5 files changed, 169 insertions(+), 27 deletions(-) rename src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/{ => Assets}/TestCode.cs (62%) create mode 100644 src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/TelescopeIncrementalGeneratorTest.cs create mode 100644 src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/MetadataReferenceProvider.cs create mode 100644 src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/TestCodeProvider.cs diff --git a/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/TestCode.cs b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Assets/TestCode.cs similarity index 62% rename from src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/TestCode.cs rename to src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Assets/TestCode.cs index bae50ad..bc4256c 100644 --- a/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/TestCode.cs +++ b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Assets/TestCode.cs @@ -1,17 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace dotnetCampus.Telescope.SourceGenerator.Test; -internal static class TestCodeProvider -{ - public const string Source = -""" +using dotnetCampus.Telescope.SourceGeneratorAnalyzers.TestLib1; + +using System; + +namespace dotnetCampus.Telescope.SourceGenerator.Test.Assets; [F1] -public class CurrentFoo : DemoLib1.F1 +public class CurrentFoo : dotnetCampus.Telescope.SourceGeneratorAnalyzers.TestLib1.F1 { } @@ -20,7 +14,7 @@ abstract class F1 : Base { } -[Foo(1ul, FooEnum.N2, typeof(Base), null, Number2 = 2L, Type2 = typeof(Foo), FooEnum2 = FooEnum.N1, Type3 = null)] +[FooAttribute()] class Foo : Base { } @@ -29,8 +23,19 @@ class Base { } +public enum FooEnum +{ + N1, + N2, + N3, +} + class FooAttribute : Attribute { + public FooAttribute() + { + } + public FooAttribute(ulong number1, FooEnum fooEnum, Type? type1, Type? type3) { Number1 = number1; @@ -48,15 +53,4 @@ public FooAttribute(ulong number1, FooEnum fooEnum, Type? type1, Type? type3) public Type? Type1 { get; set; } public Type? Type2 { get; set; } public Type? Type3 { get; set; } -} - -public enum FooEnum -{ - N1, - N2, - N3, -} - - -"""; -} +} \ No newline at end of file diff --git a/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/TelescopeIncrementalGeneratorTest.cs b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/TelescopeIncrementalGeneratorTest.cs new file mode 100644 index 0000000..b1656b2 --- /dev/null +++ b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/TelescopeIncrementalGeneratorTest.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +using dotnetCampus.Telescope.SourceGenerator.Test.Utils; +using dotnetCampus.Telescope.SourceGeneratorAnalyzers; + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace dotnetCampus.Telescope.SourceGenerator.Test; + +[TestClass] +public class TelescopeIncrementalGeneratorTest +{ + [TestMethod] + public void TestCollectionAssembly() + { + var compilation = CreateCompilation(TestCodeProvider.GetTestCode()); + + compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(@" +using dotnetCampus.Telescope; +using dotnetCampus.Telescope.SourceGenerator.Test.Assets; + +[assembly: MarkExport(typeof(Base), typeof(FooAttribute))]")); + + var generator = new TelescopeIncrementalGenerator(); + var driver = CSharpGeneratorDriver.Create(generator); + + driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var diagnostics); + + Assert.AreEqual(0, diagnostics.Length); + + var attributedTypesExportCode = outputCompilation.SyntaxTrees.FirstOrDefault(t => Path.GetFileName(t.FilePath) == "__AttributedTypesExport__.cs"); + Assert.IsNotNull(attributedTypesExportCode); + var codeText = attributedTypesExportCode.GetText().ToString(); + // 内容如下 + /* + public partial class __AttributedTypesExport__ : ICompileTimeAttributedTypesExporter + { + AttributedTypeMetadata[] CompileTimeAttributedTypesExporter.ExportAttributeTypes() + { + return new AttributedTypeMetadata [] + { + new AttributedTypeMetadatatypeof(global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Foo), new global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.FooAttribute() { }, () => ew global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Foo()) + }; + } + } + */ + Assert.AreEqual(true, codeText.Contains("global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Base")); + Assert.AreEqual(true, codeText.Contains("global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.FooAttribute")); + Assert.AreEqual(true, codeText.Contains("typeof(global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Foo)")); + Assert.AreEqual(true, codeText.Contains("new global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Foo()")); + } + + private static CSharpCompilation CreateCompilation(string source) + { + return CSharpCompilation.Create("compilation", + new[] { CSharpSyntaxTree.ParseText(source) }, + new[] + { + MetadataReference.CreateFromFile(typeof(MarkExportAttribute).Assembly.Location), + MetadataReference.CreateFromFile(typeof(dotnetCampus.Telescope.SourceGeneratorAnalyzers.TestLib1.F1) + .Assembly.Location), + MetadataReference.CreateFromFile(typeof(dotnetCampus.Telescope.SourceGeneratorAnalyzers.TestLib2.F2) + .Assembly.Location), + MetadataReference.CreateFromFile(typeof(dotnetCampus.Telescope.SourceGeneratorAnalyzers.TestLib3.F3) + .Assembly.Location), + }.Concat(MetadataReferenceProvider.GetDotNetMetadataReferenceList()), + new CSharpCompilationOptions(OutputKind.ConsoleApplication)); + } +} diff --git a/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/MetadataReferenceProvider.cs b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/MetadataReferenceProvider.cs new file mode 100644 index 0000000..08968e6 --- /dev/null +++ b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/MetadataReferenceProvider.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.IO; +using System.Reflection; + +using Microsoft.CodeAnalysis; + +namespace dotnetCampus.Telescope.SourceGenerator.Test.Utils; + +internal static class MetadataReferenceProvider +{ + public static IReadOnlyList GetDotNetMetadataReferenceList() + { + if (_cacheList is not null) + { + return _cacheList; + } + + var metadataReferenceList = new List(); + var assembly = Assembly.Load("System.Runtime"); + foreach (var file in Directory.GetFiles(Path.GetDirectoryName(assembly.Location)!, "*.dll")) + { + try + { + metadataReferenceList.Add(MetadataReference.CreateFromFile(file)); + } + catch + { + // 忽略 + } + } + + _cacheList = metadataReferenceList; + return _cacheList; + } + + private static IReadOnlyList? _cacheList; +} \ No newline at end of file diff --git a/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/TestCodeProvider.cs b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/TestCodeProvider.cs new file mode 100644 index 0000000..017fb45 --- /dev/null +++ b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/TestCodeProvider.cs @@ -0,0 +1,13 @@ +using System.IO; + +namespace dotnetCampus.Telescope.SourceGenerator.Test.Utils; + +internal static class TestCodeProvider +{ + public static string GetTestCode() + { + var manifestResourceStream = typeof(TestCodeProvider).Assembly.GetManifestResourceStream("dotnetCampus.Telescope.SourceGenerator.Test.Assets.TestCode.cs")!; + var streamReader = new StreamReader(manifestResourceStream); + return streamReader.ReadToEnd(); + } +} \ No newline at end of file diff --git a/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/dotnetCampus.Telescope.SourceGenerator.Test.csproj b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/dotnetCampus.Telescope.SourceGenerator.Test.csproj index 34ffb86..e310d48 100644 --- a/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/dotnetCampus.Telescope.SourceGenerator.Test.csproj +++ b/src/SourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/dotnetCampus.Telescope.SourceGenerator.Test.csproj @@ -1,13 +1,34 @@  - net8.0 - enable + net6.0 enable + + + + + + + + + + + + + + + + + + + + + +