-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...tor/Test/dotnetCampus.Telescope.SourceGenerator.Test/TelescopeIncrementalGeneratorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Base, lobal::dotnetCampus.Telescope.SourceGenerator.Test.Assets.FooAttribute> | ||
{ | ||
AttributedTypeMetadata<global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Base, global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.FooAttribute>[] CompileTimeAttributedTypesExporter<global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Base, lobal::dotnetCampus.Telescope.SourceGenerator.Test.Assets.FooAttribute>.ExportAttributeTypes() | ||
{ | ||
return new AttributedTypeMetadata<global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Base, lobal::dotnetCampus.Telescope.SourceGenerator.Test.Assets.FooAttribute> [] | ||
{ | ||
new AttributedTypeMetadata<global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.Base, global::dotnetCampus.Telescope.SourceGenerator.Test.Assets.FooAttribute>typeof(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)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...rator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/MetadataReferenceProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MetadataReference> GetDotNetMetadataReferenceList() | ||
{ | ||
if (_cacheList is not null) | ||
{ | ||
return _cacheList; | ||
} | ||
|
||
var metadataReferenceList = new List<MetadataReference>(); | ||
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<MetadataReference>? _cacheList; | ||
} |
13 changes: 13 additions & 0 deletions
13
...ourceGenerator/Test/dotnetCampus.Telescope.SourceGenerator.Test/Utils/TestCodeProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
25 changes: 23 additions & 2 deletions
25
...tCampus.Telescope.SourceGenerator.Test/dotnetCampus.Telescope.SourceGenerator.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Assets\**\*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" /> | ||
|
||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeRefactoring.Testing.MSTest" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" Version="1.1.1" /> | ||
|
||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.2.0" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Tradition\dotnetCampus.Telescope\dotnetCampus.Telescope.csproj" /> | ||
<ProjectReference Include="..\..\Analyzers\dotnetCampus.Telescope.SourceGeneratorAnalyzers.csproj" /> | ||
<ProjectReference Include="..\TestLib\TestLib3\dotnetCampus.Telescope.SourceGeneratorAnalyzers.TestLib3.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |