Skip to content

Commit

Permalink
加上单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Feb 2, 2024
1 parent c61674e commit 6c98832
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
}

Expand All @@ -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
{
}
Expand All @@ -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;
Expand All @@ -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,
}
""";
}
}
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));
}
}
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;
}
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();
}
}
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>

0 comments on commit 6c98832

Please sign in to comment.