Fixed broken tests

This commit is contained in:
Koen
2022-10-13 13:22:55 +01:00
parent ed7f8c5940
commit a640053048
7 changed files with 95 additions and 5 deletions

View File

@@ -138,7 +138,7 @@ namespace EntityFrameworkCore.Projectables.Generated
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static class {generatedClassName}
{{
public static System.Linq.Expressions.Expression<System.Func<{lambdaTypeArguments.Arguments}, {projectable.ReturnTypeName}>> Expression{(projectable.TypeParameterList?.Parameters.Any() == true ? projectable.TypeParameterList.ToString() : string.Empty)}()");
public static System.Linq.Expressions.Expression<System.Func<{(lambdaTypeArguments.Arguments.Any() ? $"{lambdaTypeArguments.Arguments}, " : "")}{projectable.ReturnTypeName}>> Expression{(projectable.TypeParameterList?.Parameters.Any() == true ? projectable.TypeParameterList.ToString() : string.Empty)}()");
if (projectable.ConstraintClauses is not null)
{

View File

@@ -47,10 +47,13 @@ namespace EntityFrameworkCore.Projectables.Services
var mappedArgumentExpression = (parameterIndex, node.Object) switch {
(0, not null) => node.Object,
(_, not null) => node.Arguments[parameterIndex - 1],
(_, null) => node.Arguments[parameterIndex]
(_, null) => node.Arguments.Count > parameterIndex ? node.Arguments[parameterIndex] : null
};
_expressionArgumentReplacer.ParameterArgumentMapping.Add(parameterExpession, mappedArgumentExpression);
if (mappedArgumentExpression is not null)
{
_expressionArgumentReplacer.ParameterArgumentMapping.Add(parameterExpession, mappedArgumentExpression);
}
}
var updatedBody = _expressionArgumentReplacer.Visit(reflectedExpression.Body);

View File

@@ -52,7 +52,7 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
[Fact]
public Task ExtensionMethodAcceptingDbContext()
{
using var dbContext = new SampleDbContext<Entity>();
using var dbContext = new SampleDbContext<Entity>(Infrastructure.CompatibilityMode.Full);
var sampleQuery = dbContext.Set<Entity>()
.Select(x => dbContext.Set<Entity>().Where(y => y.Id > x.Id).FirstOrDefault());

View File

@@ -0,0 +1,19 @@
// <auto-generated/>
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static class _Foo_Zero
{
public static System.Linq.Expressions.Expression<System.Func<int>> Expression()
{
return () =>
0;
}
}
}

View File

@@ -0,0 +1,19 @@
// <auto-generated/>
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static class _Foo_Zero
{
public static System.Linq.Expressions.Expression<System.Func<int, int>> Expression()
{
return (int x) =>
0;
}
}
}

View File

@@ -1074,6 +1074,53 @@ namespace Foo {
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task StaticMethodWithNoParameters()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
public static class Foo {
[Projectable]
public static int Zero() => 0;
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task StaticMethodWithParameters()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
public static class Foo {
[Projectable]
public static int Zero(int x) => 0;
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ConstMember()
{

View File

@@ -3,7 +3,9 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />