Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 96 additions & 12 deletions src/TemplateTest/CreateMapExpressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Security;
using System.Reflection;

namespace TemplateTest
{
Expand Down Expand Up @@ -85,13 +88,14 @@ public void TestRegressionMapperGenerationTranslation()
Implements = new[] { typeof(IMyTypeMapper) },
Namespace = "Benchmark",
TypeName = "CustomerMapper",
IsInternal = true,
IsInternal = false,
GeneratedAttributes = new(new[] {new MapsterToolGeneratedMapperAttribute()})
};

var translator = new ExpressionTranslator(definitions);

foreach (var method in typeof(IMyTypeMapper).GetMethods())
foreach (var method in typeof(IMyTypeMapper).GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance)
.Where(x=>x.IsPublicOrInternal()))
{
if (method.IsGenericMethod)
continue;
Expand All @@ -109,11 +113,14 @@ public void TestRegressionMapperGenerationTranslation()
expr,
ExpressionTranslator.LambdaType.PublicMethod,
typeof(IMyTypeMapper),
method.Name
method.Name,
!method.IsPublic
);
}

foreach (var prop in typeof(IMyTypeMapper).GetProperties())
foreach (var prop in typeof(IMyTypeMapper).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(x=>x.IsGetterPublicOrInternal())
)
{
if (!prop.PropertyType.IsGenericType)
continue;
Expand All @@ -131,25 +138,102 @@ public void TestRegressionMapperGenerationTranslation()
expr,
ExpressionTranslator.LambdaType.PublicLambda,
typeof(IMyTypeMapper),
prop.Name
prop.Name,
!prop.GetMethod?.IsPublic ?? false
);
}


var txt = translator.ToString();

var src = new Address() { City = "City 17"};

IMyTypeMapper mapper = new CustomerMapper();

var result = mapper.Map(src);


Assert.IsTrue(txt.Contains("Expression<Func<AddressDTO, Address>> TemplateTest.IMyTypeMapper.Projection"));
Assert.IsTrue(txt.Contains("AddressDTO TemplateTest.IMyTypeMapper.Map"));
Assert.IsTrue(txt.Contains("[MapsterToolGeneratedMapper]"));
}

}



public partial class CustomerMapper : IMyTypeMapper
{

AddressDTO TemplateTest.IMyTypeMapper.Map(Address p1)
{
return p1 == null ? null : new AddressDTO()
{
Id = p1.Id,
City = p1.City,
Country = p1.Country
};
}

Expression<Func<AddressDTO, Address>> TemplateTest.IMyTypeMapper.Projection => p2 => new Address()
{
Id = p2.Id,
City = p2.City,
Country = p2.Country
};
}




public static class MethodInfoExtensions
{
public static bool IsPublicOrInternal(this MethodInfo method)
{
if (method == null) throw new ArgumentNullException(nameof(method));

return !method.IsPrivate
&& !method.IsFamily
&& !method.IsFamilyOrAssembly
&& !method.IsFamilyAndAssembly
&& (method.IsPublic || true);
}



public static bool IsGetterPublicOrInternal(this PropertyInfo property)
{
if (property == null) throw new ArgumentNullException(nameof(property));

MethodInfo? getMethod = property.GetMethod;

if (getMethod == null) return false;

return !getMethod.IsPrivate
&& !getMethod.IsFamily
&& !getMethod.IsFamilyOrAssembly
&& !getMethod.IsFamilyAndAssembly
&& (getMethod.IsPublic || true);
}
}

internal interface IMyTypeMapper













public interface IMyTypeMapper
{
AddressDTO Map(Address p1);
Expression<Func<AddressDTO, Address>> Projection { get; }
internal AddressDTO Map(Address p1);
internal Expression<Func<AddressDTO, Address>> Projection { get; }
}

public class Address
Expand All @@ -172,10 +256,10 @@ public class Customer
public int Id { get; set; }
public string Name { get; set; }
public decimal? Credit { get; set; }
public Address Address { get; set; }
public Address HomeAddress { get; set; }
public Address[] Addresses { get; set; }
public ICollection<Address> WorkAddresses { get; set; }
//public Address Address { get; set; }
//public Address HomeAddress { get; set; }
//public Address[] Addresses { get; set; }
//public ICollection<Address> WorkAddresses { get; set; }
}

public class CustomerDTO
Expand Down
Loading