public void ShouldMapAnInRangeWholeNumberDecimalToANullableInt()
        {
            var source = new PublicGetMethod <decimal>(53632.00m);
            var result = Mapper.Map(source).ToANew <PublicProperty <int?> >();

            result.Value.ShouldBe((int)source.GetValue());
        }
        public void ShouldHandleANullEnumerableMember()
        {
            var source = new PublicGetMethod <int[]>(value: null);
            var result = Mapper.Map(source).ToANew <Dictionary <string, string> >();

            result.ShouldBeEmpty();
        }
        public void ShouldRestrictAPostMappingCallbackBySourceType()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper
                .WhenMapping
                .From <PublicField <string> >()
                .To <PublicProperty <string> >()
                .After
                .MappingEnds
                .Call((pf, pp, i) => pp.Value = "SetByCallback");

                var nonMatchingSource = new PublicGetMethod <string>("SetBySource");
                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicProperty <string> >();

                nonMatchingResult.Value.ShouldBe("SetBySource");

                var matchingSource = new PublicField <string> {
                    Value = "SetBySource"
                };
                var matchingResult = mapper.Map(matchingSource).ToANew <PublicProperty <string> >();

                matchingResult.Value.ShouldBe("SetByCallback");
            }
        }
        public void ShouldMapAnInRangeWholeNumberDecimalToANullableCharacter()
        {
            var source = new PublicGetMethod <decimal>(5.00m);
            var result = Mapper.Map(source).ToANew <PublicProperty <char?> >();

            result.Value.ShouldBe('5');
        }
Esempio n. 5
0
        public void ShouldMapAWholeNumberDecimalToANullableDouble()
        {
            var source = new PublicGetMethod <decimal>(5332.00m);
            var result = Mapper.Map(source).ToANew <PublicProperty <double?> >();

            result.Value.ShouldBe(5332);
        }
        public void ShouldMapAnDecimalOneToANullableBool()
        {
            var source = new PublicGetMethod <decimal>(1);
            var result = Mapper.Map(source).ToANew <PublicProperty <bool?> >();

            result.Value.ShouldBeTrue();
        }
        public void ShouldIgnoreGetMethodsBySourceTypeTargetTypeAndMethodInfoMatcher()
        {
            using (var mapper = Mapper.CreateNew())
            {
                var value = 1;

                // ReSharper disable once AccessToModifiedClosure
                mapper.WhenMapping
                .From <PublicGetMethod <int> >()
                .To <PublicSetMethod <int> >()
                .IgnoreSourceMembersWhere(member =>
                                          member.IsGetMethodMatching(m => value == 1));

                var matchingSource    = new PublicGetMethod <int>(999);
                var nonMatchingSource = new { Value = 111 };

                var matchingResult = mapper.Map(matchingSource).ToANew <PublicSetMethod <int> >();
                matchingResult.Value.ShouldBeDefault();

                value = 2;
                var nonMatchingFilterResult = mapper.Map(matchingSource).ToANew <PublicField <int> >();
                nonMatchingFilterResult.Value.ShouldBe(999);

                var nonMatchingTargetResult = mapper.Map(matchingSource).ToANew <PublicField <int> >();
                nonMatchingTargetResult.Value.ShouldBe(999);

                var nonMatchingSourceResult = mapper.Map(nonMatchingSource).ToANew <PublicSetMethod <int> >();
                nonMatchingSourceResult.Value.ShouldBe(111);

                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicField <int> >();
                nonMatchingResult.Value.ShouldBe(111);
            }
        }
        public void ShouldIgnoreSetMethodsBySourceTypeAndTargetType()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicGetMethod <int> >()
                .To <PublicSetMethod <int> >()
                .IgnoreTargetMembersWhere(member => member.IsSetMethod);

                var matchingSource    = new PublicGetMethod <int>(888);
                var nonMatchingSource = new { Value = 333 };

                var matchingResult = mapper.Map(matchingSource).ToANew <PublicSetMethod <int> >();
                matchingResult.Value.ShouldBeDefault();

                var nonMatchingTargetResult = mapper.Map(matchingSource).ToANew <PublicField <int> >();
                nonMatchingTargetResult.Value.ShouldBe(888);

                var nonMatchingSourceResult = mapper.Map(nonMatchingSource).ToANew <PublicSetMethod <int> >();
                nonMatchingSourceResult.Value.ShouldBe(333);

                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicField <int> >();
                nonMatchingResult.Value.ShouldBe(333);
            }
        }
Esempio n. 9
0
        public void ShouldMapAnInRangeWholeNumberDecimalToANullableShort()
        {
            var source = new PublicGetMethod <decimal>(5362.00m);
            var result = Mapper.Map(source).ToANew <PublicProperty <short?> >();

            result.Value.ShouldBe(5362);
        }
Esempio n. 10
0
        public void ShouldConvertASimpleTypeConstructorArgument()
        {
            var source = new PublicGetMethod <string>("80.6537");
            var result = Mapper.Map(source).ToANew <PublicCtor <decimal> >();

            result.Value.ShouldBe(80.6537);
        }
        public void ShouldRestrictAPreMappingCallbackBySourceTypeConditionally()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper
                .WhenMapping
                .From <PublicProperty <string> >()
                .To <PublicField <string> >()
                .Before
                .MappingBegins
                .If((pp, pf, i) => !i.HasValue && pp.Value.StartsWith("H"))
                .Call(ctx => ctx.Source.Value = "SetByCallback");

                var nonMatchingSource = new PublicGetMethod <string>("Harold");
                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicField <string> >();

                nonMatchingResult.Value.ShouldBe("Harold");

                var matchingSource = new PublicProperty <string> {
                    Value = "Harold"
                };
                var matchingResult = mapper.Map(matchingSource).ToANew <PublicField <string> >();

                matchingResult.Value.ShouldBe("SetByCallback");
            }
        }
        public void ShouldIgnoreSetMethodsBySourceTypeTargetTypeAndMethodInfoMatcher()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicGetMethod <int> >()
                .To <PublicSetMethod <int> >()
                .IgnoreTargetMembersWhere(member =>
                                          member.IsSetMethodMatching(m =>
                                                                     m.GetParameters()[0].ParameterType == typeof(int)));

                var matchingSource    = new PublicGetMethod <int>(999);
                var nonMatchingSource = new { Value = 111 };

                var matchingResult = mapper.Map(matchingSource).ToANew <PublicSetMethod <int> >();
                matchingResult.Value.ShouldBeDefault();

                var nonMatchingTargetResult = mapper.Map(matchingSource).ToANew <PublicField <int> >();
                nonMatchingTargetResult.Value.ShouldBe(999);

                var nonMatchingSourceResult = mapper.Map(nonMatchingSource).ToANew <PublicSetMethod <int> >();
                nonMatchingSourceResult.Value.ShouldBe(111);

                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicField <int> >();
                nonMatchingResult.Value.ShouldBe(111);
            }
        }
Esempio n. 13
0
        public void ShouldMapANullableGuidToAGuid()
        {
            var source = new PublicGetMethod <Guid?>(Guid.NewGuid());
            var result = Mapper.Map(source).ToANew <PublicField <Guid> >();

            result.Value.ShouldBe(source.GetValue().GetValueOrDefault());
        }
        public void ShouldMapASimpleTypeMemberToAConvertibleTypedDictionary()
        {
            var source = new PublicGetMethod <string>("6473");
            var result = Mapper.Map(source).ToANew <Dictionary <string, short> >();

            result["GetValue"].ShouldBe(6473);
        }
Esempio n. 15
0
        public void ShouldUseAParameterisedConstructor()
        {
            var source = new PublicGetMethod <string>("Barney");
            var result = Mapper.Map(source).ToANew <PublicCtor <string> >();

            result.ShouldNotBeNull();
            result.Value.ShouldBe("Barney");
        }
Esempio n. 16
0
        public void ShouldMapAStringToAGuid()
        {
            var guid   = Guid.NewGuid();
            var source = new PublicGetMethod <string>(guid.ToString());
            var result = Mapper.Map(source).ToANew <PublicSetMethod <Guid> >();

            result.Value.ShouldBe(guid);
        }
Esempio n. 17
0
        public void ShouldMapAnObjectGuidToAGuid()
        {
            var guid   = Guid.NewGuid();
            var source = new PublicGetMethod <object>(guid);
            var result = Mapper.Map(source).ToANew <PublicSetMethod <Guid> >();

            result.Value.ShouldBe(guid);
        }
        public void ShouldMapALongOnToAPopulatedString()
        {
            var source = new PublicGetMethod <long>(20190738);
            var target = Mapper.Map(source).OnTo(new PublicField <string> {
                Value = "Boo!"
            });

            target.Value.ShouldBe("Boo!");
        }
Esempio n. 19
0
        public void ShouldMapOnToASetMethod()
        {
            var source = new PublicGetMethod <double>(5643723);
            var target = new PublicSetMethod <double>();

            Mapper.Map(source).OnTo(target);

            target.Value.ShouldBe(source.GetValue());
        }
Esempio n. 20
0
        public void ShouldOverwriteADefaultSimpleTypePropertyValue()
        {
            var source = new PublicGetMethod <decimal>(6372.00m);
            var target = new PublicField <decimal?> {
                Value = null
            };

            Mapper.Map(source).OnTo(target);

            target.Value.ShouldBe(source.GetValue());
        }
        public void ShouldOverwriteADefaultSimpleTypePropertyValue()
        {
            var source = new PublicGetMethod <decimal>(6372.00m);
            var target = new PublicPropertyStruct <decimal?> {
                Value = null
            };

            var result = Mapper.Map(source).OnTo(target);

            result.Value.ShouldBe(6372.00m);
        }
        public void ShouldHandleANullReadOnlyNestedMemberProperty()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.CreateAReadOnlyFieldUsing(default(Address));

                var source = new PublicGetMethod <Address>(new Address {
                    Line1 = "Not happening..."
                });
                var result = mapper.Map(source).ToANew <PublicReadOnlyField <Address> >();

                result.Value.ShouldBeNull();
            }
        }
        public void ShouldHandleANullSourceMember()
        {
            var source = new PublicGetMethod <IEnumerable <byte> >(null);

            var target = new PublicProperty <ICollection <byte> >
            {
                Value = new List <byte> {
                    1, 2, 4, 8
                }
            };

            var result = Mapper.Map(source).OnTo(target);

            result.Value.ShouldNotBeNull();
            result.Value.ShouldBeSameAs(target.Value);
        }
        public void ShouldWrapAnExceptionThrownInAConfiguredExpression()
        {
            Should.Throw <MappingException>(() =>
            {
                using (var mapper = Mapper.CreateNew())
                {
                    mapper.WhenMapping
                    .From <PublicGetMethod <string> >()
                    .To <PublicField <short> >()
                    .Map((s, t) => int.Parse(s.GetValue()) / 0)
                    .To(x => x.Value);

                    var source = new PublicGetMethod <string>("1234");
                    var result = mapper.Map(source).ToANew <PublicField <short> >();

                    result.Value.ShouldBeDefault();
                }
            });
        }
        public void ShouldIgnoreGetMethodsByMemberTypeAndTargetType()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicGetMethod <DateTime> >()
                .IgnoreSourceMembersWhere(member => member.IsGetMethod);

                var nonMatchingSource = new PublicProperty <DateTime> {
                    Value = DateTime.Today
                };
                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicSetMethod <string> >();
                nonMatchingResult.Value.ShouldBe(DateTime.Today);

                var matchingSource = new PublicGetMethod <DateTime>(DateTime.Today);
                var matchingResult = mapper.Map(matchingSource).ToANew <PublicSetMethod <DateTime> >();
                matchingResult.Value.ShouldBeDefault();
            }
        }
        public void ShouldConditionallyApplyAConfiguredMember()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicGetMethod <int> >()
                .ToANew <PublicSetMethod <string> >()
                .If(ctx => ctx.Source.GetValue() % 2 == 0)
                .Map(ctx => ctx.Source.GetValue())
                .To <string>(x => x.SetValue);

                var matchingSource = new PublicGetMethod <int>(6);
                var matchingResult = mapper.Map(matchingSource).ToANew <PublicSetMethod <string> >();

                matchingResult.Value.ShouldBe("6");

                var nonMatchingSource = new PublicGetMethod <int>(7);
                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicSetMethod <string> >();

                nonMatchingResult.Value.ShouldBeNull();
            }
        }
Esempio n. 27
0
        public void ShouldCreateADerivedTypeInAnExistingMemberEnumerableUsingRuntimeTypes()
        {
            using (var mapper = Mapper.CreateNew())
            {
                var personId = Guid.NewGuid();

                var source = new PublicGetMethod <object>(new List <PersonViewModel>
                {
                    new PersonViewModel {
                        Id = personId, Name = "Bob"
                    },
                    new CustomerViewModel {
                        Name = "Fred"
                    }
                });
                var target = new PublicField <object>
                {
                    Value = new Collection <Person>
                    {
                        new Person {
                            Id = personId
                        }
                    }
                };
                var result = mapper.Map(source).OnTo(target);

                var resultValues = (Collection <Person>)result.Value;
                resultValues.Count.ShouldBe(2);

                resultValues.First().ShouldBeOfType <Person>();
                resultValues.First().Id.ShouldBe(personId);
                resultValues.First().Name.ShouldBe("Bob");

                resultValues.Second().ShouldBeOfType <Customer>();
                resultValues.Second().Name.ShouldBe("Fred");
            }
        }