Skip to content

Commit

Permalink
add unit test for issue AutoMapper#2266 . remove extra methods. expos…
Browse files Browse the repository at this point in the history
…e PrimitiveHelper.GetInheritedMember method
  • Loading branch information
mjalil committed Aug 15, 2017
1 parent 4c4f902 commit 276baea
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/AutoMapper/Configuration/Internal/PrimitiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ public static TValue GetOrDefault<TKey, TValue>(IDictionary<TKey, TValue> dictio
private static IEnumerable<MemberInfo> GetAllMembers(this Type type) =>
type.GetTypeInheritance().Concat(type.GetTypeInfo().ImplementedInterfaces).SelectMany(i => i.GetDeclaredMembers());

private static MemberInfo GetInheritedMember(this Type type, string name) => type.GetAllMembers().FirstOrDefault(mi => mi.Name == name);
public static MemberInfo GetInheritedMember(this Type type, string name) => type.GetAllMembers().FirstOrDefault(mi => mi.Name == name);

public static MethodInfo GetInheritedMethod(Type type, string name)
=> type.GetInheritedMember(name) as MethodInfo ?? throw new ArgumentOutOfRangeException(nameof(name), $"Cannot find method {name} of type {type}.");

public static MemberInfo GetFieldOrProperty(Type type, string name)
=> type.GetInheritedMember(name) ?? throw new ArgumentOutOfRangeException(nameof(name), $"Cannot find member {name} of type {type}.");

public static bool HasFieldOrProperty(Type type, string name)
=> type.GetInheritedMember(name) != null;

public static bool IsNullableType(Type type)
=> type.IsGenericType(typeof(Nullable<>));

Expand Down
5 changes: 3 additions & 2 deletions src/AutoMapper/Configuration/MappingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;
using AutoMapper.Internal;
using AutoMapper.Configuration.Internal;

namespace AutoMapper.Configuration
{
Expand Down Expand Up @@ -540,8 +541,8 @@ public void Configure(TypeMap typeMap)
var attrs = destProperty.GetCustomAttributes(true);
if (attrs.Any(x => x is IgnoreMapAttribute))
{
IgnoreDestinationMember(destProperty);
if (typeMap.SourceType.HasFieldOrProperty(destProperty.Name))
IgnoreDestinationMember(destProperty);
if (typeMap.SourceType.GetInheritedMember(destProperty.Name) != null)
{
_reverseMap?.ForMember(destProperty.Name, opt => opt.Ignore());
}
Expand Down
3 changes: 0 additions & 3 deletions src/AutoMapper/Configuration/PrimitiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public static MethodInfo GetInheritedMethod(this Type type, string name)
public static MemberInfo GetFieldOrProperty(this Type type, string name)
=> PrimitiveHelper.GetFieldOrProperty(type, name);

public static bool HasFieldOrProperty(this Type type, string name)
=> PrimitiveHelper.HasFieldOrProperty(type, name);

public static bool IsNullableType(this Type type)
=> PrimitiveHelper.IsNullableType(type);

Expand Down
17 changes: 17 additions & 0 deletions src/UnitTests/IgnoreAllTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,22 @@ public void Ignore_On_Source_Field()
destination.ShouldNotBeMapped.ShouldBe(null);

}

public class Source2
{
}

public class Destination2
{
[IgnoreMap]
public string ShouldNotThrowExceptionOnReverseMapping { get; set; }
}

[Fact]
public void Sould_not_throw_exception_when_reverse_property_does_not_exist()
{
typeof(ArgumentOutOfRangeException).ShouldNotBeThrownBy(() => new MapperConfiguration(cfg => cfg.CreateMap<Source2, Destination2>()
.ReverseMap()));
}
}
}

0 comments on commit 276baea

Please sign in to comment.