AutoMapper/AutoMapper — Issue #143: EnumMapper doesn't evaluate nor respect condition
Author: kalebpederson
Created: 2011-11-04 21:57:33+00:00
State: open

Here's a testcase started from a SO post:

[Flags] public enum Enum1 { One, Two };
[Flags] public enum Enum2 { Three, Four }

public class EnumTestSource
{
public IEnumerable Prop1{ get; set; }
public IEnumerable Prop2{ get; set; }
}

public class EnumTestDest
{
public Enum1 Prop1 { get; set; }
public Enum2 Prop2 { get; set; }
}

[TestFixture]
public class EnumMapperTest
{
[Test]
public void Mapper_respects_condition()
{
var _c1Called = false;
var _c2Called = false;
Mapper.CreateMap()
.ForMember(m => m.Prop1, o => { o.Condition(c => { _c1Called = true; return !c.IsSourceValueNull; });
o.MapFrom(f => f.Prop1.Aggregate((current, next) => current | next));
})
.ForMember(m => m.Prop2, o => { o.Condition(c => { _c2Called = true; return !c.IsSourceValueNull; } );
o.MapFrom(f => f.Prop2.Aggregate((current, next) => current | next));
});
var src = new EnumTestSource {Prop1 = new[] {Enum1.One}, Prop2 = null};
var dest = Mapper.Map(src); // will throw
Assert.IsTrue(_c1Called);
Assert.IsTrue(_c2Called);
}
}