Skip to content

Commit c5574ab

Browse files
authored
Add test for implicit new() with constructor arguments (target-typed new(arg1, arg2))
1 parent dbac58b commit c5574ab

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <auto-generated/>
2+
#nullable disable
3+
using EntityFrameworkCore.Projectables;
4+
using Foo;
5+
6+
namespace EntityFrameworkCore.Projectables.Generated
7+
{
8+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
9+
static class Foo_Dest_From_P0_Foo_Source
10+
{
11+
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.Source, global::Foo.Dest>> Expression()
12+
{
13+
return (global::Foo.Source s) => new global::Foo.Dest(s.Name, s.Value);
14+
}
15+
}
16+
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/MethodTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,4 +874,40 @@ class Dest {
874874

875875
return Verifier.Verify(result.GeneratedTrees[0].ToString());
876876
}
877+
878+
[Fact]
879+
public Task ImplicitNewExpression_WithConstructorArguments_RewritesToExplicitType()
880+
{
881+
var compilation = CreateCompilation(@"
882+
using EntityFrameworkCore.Projectables;
883+
884+
namespace Foo {
885+
class Source {
886+
public string Name { get; set; }
887+
public int Value { get; set; }
888+
}
889+
890+
class Dest {
891+
public string Name { get; }
892+
public int Value { get; }
893+
894+
public Dest(string name, int value)
895+
{
896+
Name = name;
897+
Value = value;
898+
}
899+
900+
[Projectable]
901+
public static Dest From(Source s) => new(s.Name, s.Value);
902+
}
903+
}
904+
");
905+
906+
var result = RunGenerator(compilation);
907+
908+
Assert.Empty(result.Diagnostics);
909+
Assert.Single(result.GeneratedTrees);
910+
911+
return Verifier.Verify(result.GeneratedTrees[0].ToString());
912+
}
877913
}

0 commit comments

Comments
 (0)