Find overriding implementations for virtual methods

This commit is contained in:
Koen
2022-12-12 00:15:54 +00:00
parent 2c2a2524de
commit fead7bb41a
10 changed files with 274 additions and 12 deletions
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
using EntityFrameworkCore.Projectables.Services;
using Microsoft.EntityFrameworkCore;
using ScenarioTests;
using VerifyXunit;
using Xunit;
#nullable disable
namespace EntityFrameworkCore.Projectables.FunctionalTests
{
[UsesVerify]
public class InheritedModelTests
{
public abstract class Base
{
public int Id { get; set; }
[Projectable]
public int ComputedProperty => SampleProperty + 1;
public virtual int SampleProperty => 0;
[Projectable]
public int ComputedMethod() => SampleMethod() + 1;
public virtual int SampleMethod() => 0;
}
public class Concrete : Base
{
[Projectable]
public override int SampleProperty => 1;
[Projectable]
public override int SampleMethod() => 1;
}
public class MoreConcrete : Concrete
{
}
[Fact]
public Task ProjectOverOverriddenPropertyImplementation()
{
using var dbContext = new SampleDbContext<Concrete>();
var query = dbContext.Set<Concrete>()
.Select(x => x.ComputedProperty);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task ProjectOverInheritedPropertyImplementation()
{
using var dbContext = new SampleDbContext<Concrete>();
var query = dbContext.Set<Concrete>()
.Select(x => x.ComputedProperty);
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task ProjectOverOverriddenMethodImplementation()
{
using var dbContext = new SampleDbContext<Concrete>();
var query = dbContext.Set<Concrete>()
.Select(x => x.ComputedMethod());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task ProjectOverInheritedMethodImplementation()
{
using var dbContext = new SampleDbContext<Concrete>();
var query = dbContext.Set<Concrete>()
.Select(x => x.ComputedMethod());
return Verifier.Verify(query.ToQueryString());
}
}
}
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.Extensions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Xunit;
namespace EntityFrameworkCore.Projectables.Tests.Extensions
@@ -18,6 +20,18 @@ namespace EntityFrameworkCore.Projectables.Tests.Extensions
}
}
class BaseType
{
public virtual void VirtualMethod(int arg1) { }
public virtual void GenericVirtualMethod<TArg>(TArg arg1) { }
}
class DerivedType : BaseType
{
public override void VirtualMethod(int arg1) { }
public override void GenericVirtualMethod<TArg>(TArg arg1) { }
}
[Fact]
public void GetNestedTypePath_OuterType_Returns1Entry()
{
@@ -28,7 +42,6 @@ namespace EntityFrameworkCore.Projectables.Tests.Extensions
Assert.Single(result);
}
[Fact]
public void GetNestedTypePath_InnerType_Returns2Entries()
{
@@ -59,5 +72,53 @@ namespace EntityFrameworkCore.Projectables.Tests.Extensions
Assert.Equal(typeof(TypeExtensionTests), result.First());
Assert.Equal(typeof(InnerType.SubsequentlyInnerType), result.Last());
}
[Fact]
public void GetOverridingMethod_BaseTypeVirtualMethod_FindsSameMethod()
{
var type = typeof(BaseType);
var method = typeof(BaseType).GetMethod("VirtualMethod")!;
var result = type.GetOverridingMethod(method);
Assert.Equal(method, result);
}
[Fact]
public void GetOverridingMethod_DerivedTypeVirtualMethod_FindsOverridingMethod()
{
var baseType = typeof(BaseType);
var baseMethod = baseType.GetMethod("VirtualMethod")!;
var derivedType = typeof(DerivedType);
var derivedMethod = typeof(DerivedType).GetMethod("VirtualMethod")!;
var resolvedMethod = derivedType.GetOverridingMethod(baseMethod);
Assert.Equal(derivedMethod, resolvedMethod);
}
[Fact]
public void GetOverridingMethod_BaseTypeGenericVirtualMethod_FindsSameMethod()
{
var type = typeof(BaseType);
var method = typeof(BaseType).GetMethod("GenericVirtualMethod")!;
var result = type.GetOverridingMethod(method);
Assert.Equal(method, result);
}
[Fact]
public void GetOverridingMethod_DerivedTypeGenericVirtualMethod_FindsOverridingMethod()
{
var baseType = typeof(BaseType);
var baseMethod = baseType.GetMethod("GenericVirtualMethod")!;
var derivedType = typeof(DerivedType);
var derivedMethod = typeof(DerivedType).GetMethod("GenericVirtualMethod")!;
var resolvedMethod = derivedType.GetOverridingMethod(baseMethod);
Assert.Equal(derivedMethod, resolvedMethod);
}
}
}