Esempio n. 1
0
        public void CreateMap_PersonToPersonSwedish()
        {
            SafeMap.CreateMap <Person, PersonSwedish>(
                cfg =>
            {
                cfg.Map(x => x.Id, x => x.PersonId);
                cfg.Map(x => x.Name, x => x.Namn);
                cfg.Map(x => x.Age, x => x.Ålder);
                cfg.Map(x => x.Length, x => x.Längd);
                cfg.Map(x => x.BirthDate, x => x.Födelsedag);
            });

            var person = new Person
            {
                Id        = Guid.NewGuid(),
                Name      = "Magnus Unger",
                Age       = 38,
                Length    = 1.85m,
                BirthDate = new DateTime(1977, 03, 04),
            };
            var result = SafeMap.Convert <Person, PersonSwedish>(person);

            Assert.AreEqual(person.Id, result.PersonId);
            Assert.AreEqual(person.Name, result.Namn);
            Assert.AreEqual(person.Age, result.Ålder);
            Assert.AreEqual(person.Length, result.Längd);
            Assert.AreEqual(person.BirthDate, result.Födelsedag);
        }
Esempio n. 2
0
        private IEnumerable <T> InnerMatches(string input)
        {
            foreach (Match match in _regex.Matches(input))
            {
                var valuesDictionary = new Dictionary <string, string[]>();
                foreach (var groupName in _groupNames)
                {
                    var matchGroup = match.Groups[groupName];

                    if (!matchGroup.Success)
                    {
                        valuesDictionary[groupName] = null;
                    }
                    else if (matchGroup.Captures.Count > 1)
                    {
                        valuesDictionary[groupName] = Enumerate(matchGroup.Captures).ToArray();
                    }
                    else
                    {
                        valuesDictionary[groupName] = new [] { matchGroup.Value };
                    }
                }
                yield return(SafeMap.Convert <Dictionary <string, string[]>, T>(valuesDictionary, _formatProvider));
            }
        }
Esempio n. 3
0
        public void SetConfiguration_ShouldResetPreviousConfiguredConverters()
        {
            SafeMap.Configuration.SetConvertMethod <int, string>(i => i.ToString(CultureInfo.InvariantCulture) + " pcs");

            var result1 = SafeMap.Convert <int, string>(10);

            SafeMap.Configuration = new MapConfiguration();
            var result2 = SafeMap.Convert <int, string>(10);

            Assert.AreEqual("10 pcs", result1);
            Assert.AreEqual("10", result2);
        }
Esempio n. 4
0
        public void CreateMap_NameValueCollectionToClassPropertyInt_StringIndexer()
        {
            SafeMap.CreateMap <NameValueCollection, ClassProperty <int> >(
                cfg =>
            {
                cfg.MapGetIndexer((x, key) => x[key]);
                cfg.Map <string, int>("Value2", x => x.Value);
            });

            var input = new NameValueCollection {
                { "Value2", "1337" }
            };
            var result = SafeMap.Convert <NameValueCollection, ClassProperty <int> >(input);

            Assert.AreEqual(1337, result.Value);
        }
Esempio n. 5
0
        public void CreateMap_ClassPropertyIntToNameValueCollection_Add()
        {
            SafeMap.Configuration.SetConvertMethod <int, string>(i => i.ToString(CultureInfo.InvariantCulture));
            SafeMap.CreateMap <ClassProperty <int>, NameValueCollection>(
                cfg =>
            {
                cfg.MapSetIndexer <string>((x, key, val) => x.Add(key, val));
                cfg.Map <int, string>(x => x.Value, "Value2");
            });

            var input = new ClassProperty <int> {
                Value = 1337
            };
            var result = SafeMap.Convert <ClassProperty <int>, NameValueCollection>(input);

            Assert.AreEqual("1337", result["Value2"]);
        }
Esempio n. 6
0
        public void Convert_Generic_NullableLongToNullableInt()
        {
            var result = SafeMap.Convert <long?, int?>(2L);

            Assert.AreEqual(2, result);
        }
Esempio n. 7
0
        public void Convert_Generic_NullableTimeSpanToNullableTimeSpan()
        {
            var result = SafeMap.Convert <TimeSpan?, TimeSpan?>(TimeSpan.FromDays(28));

            Assert.AreEqual(TimeSpan.FromDays(28), result);
        }
Esempio n. 8
0
        public void Convert_Generic_IntToNullableInt()
        {
            var result = SafeMap.Convert <int, int?>(2);

            Assert.AreEqual(2, result);
        }
Esempio n. 9
0
        public void Convert_GenericWithFormat_StringToInt()
        {
            var result = SafeMap.Convert <string, int>("10", CultureInfo.CurrentCulture);

            Assert.AreEqual(10, result);
        }
Esempio n. 10
0
        public void Convert_Generic_StringToInt()
        {
            var result = SafeMap.Convert <string, int>("10");

            Assert.AreEqual(10, result);
        }
Esempio n. 11
0
        public void Convert_NonGenericWithFormat_StringToInt()
        {
            var result = SafeMap.Convert("10", typeof(string), typeof(int), CultureInfo.CurrentCulture);

            Assert.AreEqual(10, result);
        }
Esempio n. 12
0
        public void Convert_NonGeneric_StringToInt()
        {
            var result = SafeMap.Convert("10", typeof(string), typeof(int));

            Assert.AreEqual(10, result);
        }