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); }
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)); } }
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); }
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); }
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"]); }
public void Convert_Generic_NullableLongToNullableInt() { var result = SafeMap.Convert <long?, int?>(2L); Assert.AreEqual(2, result); }
public void Convert_Generic_NullableTimeSpanToNullableTimeSpan() { var result = SafeMap.Convert <TimeSpan?, TimeSpan?>(TimeSpan.FromDays(28)); Assert.AreEqual(TimeSpan.FromDays(28), result); }
public void Convert_Generic_IntToNullableInt() { var result = SafeMap.Convert <int, int?>(2); Assert.AreEqual(2, result); }
public void Convert_GenericWithFormat_StringToInt() { var result = SafeMap.Convert <string, int>("10", CultureInfo.CurrentCulture); Assert.AreEqual(10, result); }
public void Convert_Generic_StringToInt() { var result = SafeMap.Convert <string, int>("10"); Assert.AreEqual(10, result); }
public void Convert_NonGenericWithFormat_StringToInt() { var result = SafeMap.Convert("10", typeof(string), typeof(int), CultureInfo.CurrentCulture); Assert.AreEqual(10, result); }
public void Convert_NonGeneric_StringToInt() { var result = SafeMap.Convert("10", typeof(string), typeof(int)); Assert.AreEqual(10, result); }