public void Mapper_ConditionMappingIsConfigured_FinishOnFirstHit() { var profile = new MappingProfile(); profile.CreateProfile <FakeSource, FakeReceiver>() .UseAsDefault() .For(x => x.StringValue, x => { x.If(HasCode("D")).ThenDo(ExpressionResolve(c => "No hit: never")); x.If(HasCode("D1")).ThenDo(ExpressionResolve(c => "First hit: should")); x.If(HasCode("D1") | HasCode("D2")).ThenDo(ExpressionResolve(c => "Second hit: never")); x.Do(ExpressionResolve(c => "Default hit: never")); }) .For(x => x.IntValue, x => { x.If(HasCode("X")).ThenDo(ExpressionResolve(c => - 1)); x.If(HasCode("XF")).ThenDo(ExpressionResolve(c => - 2)); x.If(HasCode("D2")).ThenDo(ExpressionResolve(c => 10)); x.If(HasCode("D1") | HasCode("D2")).ThenDo(ExpressionResolve(c => - 3)); x.Do(ExpressionResolve(c => - 4)); }); _mapperConfiguration.LoadProfile(profile); var source = CreateSource(codes: new[] { "D1", "D2" }); var receiver = _subject.Map <FakeSource, FakeReceiver>(source); Assert.AreEqual("First hit: should", receiver.StringValue); Assert.AreEqual(10, receiver.IntValue); }
public void BuildProfileMaps_DuplicatedProfileMap_Throws() { var exception = Assert.Throws <InvalidOperationException>(() => { _subject .CreateProfile <FakeSource, FakeReceiver>() .UseAsDefault() .For(x => x.StringValue, x => x.Do(NoAction <string>())); _subject .CreateProfile <FakeSource, FakeReceiver>() .UseAsDefault() .For(x => x.IntValue, x => x.Do(NoAction <int>())); _subject.BuildProfileMaps(); }); Assert.AreEqual( "There is duplicate for: <SampleMapper.Tests.Fakes.FakeSource, SampleMapper.Tests.Fakes.FakeReceiver> IsDefault True", exception.Message); }
public void Mapper_DirectMappingIsConfigured_VerifyMapping() { var profile = new MappingProfile(); profile.CreateProfile <FakeSource, FakeReceiver>() .UseAsDefault() .For(x => x.StringValue, x => x.Do(ExpressionResolve(c => c.StringValue))) .For(x => x.IntValue, x => x.Do(ExpressionResolve(c => c.IntValue))); _mapperConfiguration.LoadProfile(profile); var source = CreateSource(); var receiver = _subject.Map <FakeSource, FakeReceiver>(source); Assert.AreEqual(source.StringValue, receiver.StringValue); Assert.AreEqual(source.IntValue, receiver.IntValue); }
public void Map_UsedPropertyMaps_CheckOverloading() { var propertyMaps = PropertyMapsBuilder <FakeSource, FakeReceiver> .Create() .For(x => x.IntValue, x => x.Do(ExpressionResolve(c => 10))) .For(x => x.StringValue, x => x.Do(ExpressionResolve(c => "VAL"))) .Build(); var profile = new MappingProfile(); profile.CreateProfile <FakeSource, FakeReceiver>() .UseAsDefault() .UsePropertyMaps(propertyMaps) .For(x => x.IntValue, x => x.Do(ExpressionResolve(c => - 5))); _mapperConfiguration.LoadProfile(profile); var receiver = _subject.Map <FakeSource, FakeReceiver>(CreateSource()); Assert.AreEqual("VAL", receiver.StringValue); Assert.AreEqual(-5, receiver.IntValue); }