Fix for projectable instance members returning complex types

This commit is contained in:
Koen Bekkenutte
2021-11-26 19:53:18 +08:00
parent f03aa16680
commit af76e5b0a0
4 changed files with 113 additions and 6 deletions
@@ -0,0 +1,16 @@
using System;
using EntityFrameworkCore.Projectables;
using Foo;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression()
{
return (global::Foo.C @this) =>
@this.Bar;
}
}
}
@@ -0,0 +1,20 @@
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
using Foo;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class Foo_EntityExtensions_Entity_Something
{
public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression()
{
return (global::Foo.EntityExtensions.Entity entity) =>
new Entity(entity.Id) {
FullName = entity.FullName
};
}
}
}
@@ -76,6 +76,30 @@ namespace Foo {
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task MinimalProjectableComputedProperty()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projectables;
namespace Foo {
class C {
public int Bar { get; set; }
[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 SimpleProjectableComputedProperty()
{
@@ -900,6 +924,41 @@ namespace Foo {
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task MixPrimaryConstructorAndProperties()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
namespace Foo {
public static class EntityExtensions
{
public record Entity(int Id)
{
public int Id { get; set; }
public string? FullName { get; set; }
[Projectable]
public static Entity Something(Entity entity)
=> new Entity(entity.Id) {
FullName = entity.FullName
};
}
}
}
");
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)