Add project files.

This commit is contained in:
Koen Bekkenutte
2021-05-27 04:17:35 +08:00
commit 1c83f3d6ad
48 changed files with 2554 additions and 0 deletions
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.NetStandard20" Version="1.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Verify.Xunit" Version="11.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Remove="**\*.verified.txt" />
<None Remove="**\*.received.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projections.Generator\EntityFrameworkCore.Projections.Generator.csproj" />
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projections\EntityFrameworkCore.Projections.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
projectionTarget => 0;
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
projectionTarget => projectionTarget.Bar + projectionTarget.Bar + projectionTarget.Bar;
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression(int a, string b, object d) =>
projectionTarget => a;
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression(int i) =>
projectionTarget => i;
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
projectionTarget => projectionTarget.Bar();
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
projectionTarget => projectionTarget.Bar;
}
}
@@ -0,0 +1,12 @@
using System;
using System.Linq;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, D>> Expression() =>
projectionTarget => projectionTarget.Dees.First();
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_D_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C.D, int>> Expression() =>
projectionTarget => projectionTarget.Bar + 1;
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
projectionTarget => projectionTarget.Bar + 1;
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
projectionTarget => 1;
}
}
@@ -0,0 +1,11 @@
using System;
using EntityFrameworkCore.Projections;
namespace EntityFrameworkCore.Projections.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<Foo.C, int>> Expression() =>
projectionTarget => 1;
}
}
@@ -0,0 +1,372 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
namespace EntityFrameworkCore.Projections.Generator.Tests
{
[UsesVerify]
public class ProjectionExpressionGeneratorTests
{
readonly ITestOutputHelper _testOutputHelper;
public ProjectionExpressionGeneratorTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void EmtpyCode_Noop()
{
var compilation = CreateCompilation(@"
class C { }
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Empty(result.GeneratedTrees);
}
[Fact]
public Task SimpleProjectableMethod()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
[Projectable]
public int Foo() => 1;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task SimpleProjectableProperty()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
[Projectable]
public int Foo => 1;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task SimpleProjectableComputedProperty()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
public int Bar { get; set; }
[Projectable]
public int Foo => Bar + 1;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task SimpleProjectableComputedInNestedClassProperty()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
class D {
public int Bar { get; set; }
[Projectable]
public int Foo => Bar + 1;
}
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ProjectableComputedPropertyUsingThis()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
public int Bar { get; set; }
[Projectable]
public int Foo => this.Bar;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ProjectableComputedPropertyMethod()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
public int Bar() => 1;
[Projectable]
public int Foo => Bar();
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task MoreComplexProjectableComputedProperty()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
public int Bar { get; set; }
[Projectable]
public int Foo => Bar + this.Bar + Bar;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ArgumentlessProjectableComputedMethod()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
[Projectable]
public int Foo() => 0;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ProjectableComputedMethodWithSingleArgument()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
[Projectable]
public int Foo(int i) => i;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ProjectableComputedMethodWithMultipleArguments()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projections;
namespace Foo {
class C {
[Projectable]
public int Foo(int a, string b, object d) => a;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ProjectablePropertyToNavigationalProperty()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using EntityFrameworkCore.Projections;
namespace Foo {
class D { }
class C {
public System.Collections.Generic.List<D> Dees { get; set; }
[Projectable]
public D Foo => Dees.First();
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
#region Helpers
Compilation CreateCompilation(string source, bool expectedToCompile = true)
{
var references = Basic.Reference.Assemblies.NetStandard20.All.ToList();
references.Add(MetadataReference.CreateFromFile(typeof(ProjectableAttribute).Assembly.Location));
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("compilation",
new[] { CSharpSyntaxTree.ParseText(source) },
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
#if DEBUG
if (expectedToCompile)
{
var compilationDiagnostics = compilation.GetDiagnostics();
if (!compilationDiagnostics.IsEmpty)
{
_testOutputHelper.WriteLine($"Original compilation diagnostics produced:");
foreach (var diagnostic in compilationDiagnostics)
{
_testOutputHelper.WriteLine($" > " + diagnostic.ToString());
}
if (compilationDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
{
Debug.Fail("Compilation diagnostics produced");
}
}
}
#endif
return compilation;
}
private GeneratorDriverRunResult RunGenerator(Compilation compilation)
{
_testOutputHelper.WriteLine("Running generator and updating compilation...");
var subject = new ProjectionExpressionGenerator();
var driver = CSharpGeneratorDriver
.Create(subject)
.RunGenerators(compilation);
var result = driver.GetRunResult();
if (result.Diagnostics.IsEmpty)
{
_testOutputHelper.WriteLine("Run did not produce diagnostics");
}
else
{
_testOutputHelper.WriteLine($"Diagnostics produced:");
foreach (var diagnostic in result.Diagnostics)
{
_testOutputHelper.WriteLine($" > " + diagnostic.ToString());
}
}
foreach (var newSyntaxTree in result.GeneratedTrees)
{
_testOutputHelper.WriteLine($"Produced syntax tree with path produced: {newSyntaxTree.FilePath}");
_testOutputHelper.WriteLine(newSyntaxTree.GetText().ToString());
}
return driver.GetRunResult();
}
#endregion
}
}
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projections\EntityFrameworkCore.Projections.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projections.Extensions;
using Xunit;
namespace EntityFrameworkCore.Projections.Tests.Extensions
{
public class TypeExtensionTests
{
class InnerType
{
public class SubsequentlyInnerType
{
}
}
[Fact]
public void GetNestedTypePath_OuterType_Returns1Entry()
{
var subject = typeof(TypeExtensionTests);
var result = subject.GetNestedTypePath();
Assert.Single(result);
}
[Fact]
public void GetNestedTypePath_InnerType_Returns2Entries()
{
var subject = typeof(InnerType);
var result = subject.GetNestedTypePath();
Assert.Equal(2, result.Count());
}
[Fact]
public void GetNestedTypePath_SubsequentlyInnerType_Returns3Entries()
{
var subject = typeof(InnerType.SubsequentlyInnerType);
var result = subject.GetNestedTypePath();
Assert.Equal(3, result.Count());
}
[Fact]
public void GetNestedTypePath_SubsequentlyInnerType_ReturnsTypesInOrder()
{
var subject = typeof(InnerType.SubsequentlyInnerType);
var result = subject.GetNestedTypePath();
Assert.Equal(typeof(TypeExtensionTests), result.First());
Assert.Equal(typeof(InnerType.SubsequentlyInnerType), result.Last());
}
}
}
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projections.Services;
using Xunit;
namespace EntityFrameworkCore.Projections.Tests.Services
{
public class ProjectionExpressionClassNameGeneratorTests
{
[Theory]
[InlineData("ns", new string[] { "a" }, "m", "ns_a_m")]
[InlineData("ns", new string[] { "a", "b" }, "m", "ns_a_b_m")]
[InlineData(null, new string[] { "a" }, "m", "_a_m")]
public void GenerateName(string? namespaceName, string[] nestedTypeNames, string memberName, string expected)
{
var result = ProjectionExpressionClassNameGenerator.GenerateName(namespaceName, nestedTypeNames, memberName);
Assert.Equal(expected, result);
}
[Fact]
public void GeneratedFullName()
{
var expected = $"{ProjectionExpressionClassNameGenerator.Namespace}.{ProjectionExpressionClassNameGenerator.GenerateName("a", new[] { "b" }, "m")}";
var result = ProjectionExpressionClassNameGenerator.GenerateFullName("a", new [] { "b" }, "m");
Assert.Equal(expected, result);
}
}
}