public void FailedTestDefaultMapper()
        {
            StringBuilder buffer = new StringBuilder();

            var mapper = FactoryMapper.DynamicResolutionMapper<Student, Person>
                (
                    n => buffer.AppendLine(string.Format("ToString before mapping: {0}", n.ToString())),
                    r => { throw new Exception("error"); }
                );

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };
            mapper.Map(st);
        }
        public void FailedTestDefaultMerger()
        {
            StringBuilder buffer = new StringBuilder();

            var merger = FactoryMapper.DynamicResolutionMerger<Student, Person>
                (
                    r => { throw new Exception("error"); },
                    r => buffer.AppendLine(string.Format("ToString after mapping: {0}", r.ToString()))
                );

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.Merge(st, pr);
        }
        public void TestCustomMapper()
        {
            IList<IPropertyMapper<Student, Person>> propMappers = new List<IPropertyMapper<Student, Person>>
                {
                    new PropertyMapper<Student, Person>( (student, person) => person.Name = student.Name.ToUpper(), "Name", "Name")
                    , new PropertyMapper<Student, Person>( (student, person) => person.AnnoNascita = student.AnnoNascita )
                };

            ISourceMapper<Student, Person> mapper = new SourceMapper<Student, Person>(propMappers, null, null);

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };
            Person pr = mapper.Map(st);

            Assert.AreEqual(st.Name.ToUpper(), pr.Name, "Property [Name] was not set.");
            Assert.AreNotEqual(st.Surname.ToUpper(), pr.Surname);
            Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita);
        }
        public void TestReferenceProperty()
        {
            IList<IPropertyMapper<Student, Person>> propMappers = new List<IPropertyMapper<Student, Person>>
                {
                    new PropertyMapper<Student, Person>( (student, person) => person.Name = student.Name, "Name", "Name")
                    ,new PropertyMapper<Student, Person>( (student, person) => person.AnnoNascita = student.AnnoNascita )
                    ,new PropertyMapper<Student, Person>( (student, person) => person.Parent = student.Father )
                };

            ISourceMapper<Student, Person> mapper = new SourceMapper<Student, Person>(propMappers, null, null);

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1990, Father = new PersonaGiuridica { Name = "father", Surname = "father_surname", AnnoNascita = 1970, Code = "nick"} };
            Person pr = mapper.Map(st);

            Assert.AreEqual(st.Name, pr.Name);
            Assert.AreNotEqual(st.Surname, pr.Surname);
            Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita);
            Assert.AreEqual(st.Father, pr.Parent);
            Assert.AreSame(st.Father, pr.Parent);
            Assert.AreEqual(st.Father.GetType(), pr.Parent.GetType());
        }
        public void TestDefaultMergerIgnoringException()
        {
            StringBuilder buffer = new StringBuilder();

            IList<IPropertyMapper<Student, Person>> propMappers = new List<IPropertyMapper<Student, Person>>
                {
                    new PropertyMapper<Student, Person>( (student, person) => person.Name = student.Name, "Name", "Name")
                    ,new PropertyMapper<Student, Person>( (student, person) => person.AnnoNascita = student.AnnoNascita )
                    ,new PropertyMapper<Student, Person>( (student, person) => person.Parent = ((Student)null).Father )
                };

            var merger = new SourceMerger<Student, Person>(propMappers,
                    null,
                    r => buffer.AppendLine(string.Format("ToString after mapping: {0}", r.ToString()))
                );

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.IgnoreExceptionOnMapping = true;
            merger.Merge(st, pr);

            merger.IgnoreExceptionOnMapping = false;
            merger.Merge(st, pr);
        }
        public void TestDefaultMerger2()
        {
            StringBuilder buffer = new StringBuilder();

            var merger = FactoryMapper.DynamicResolutionMerger<Student, Person>
                (
                    n => buffer.AppendLine(string.Format("ToString before mapping: {0}", n.ToString())),
                    r => buffer.AppendLine(string.Format("ToString after mapping: {0}", r.ToString()))
                );

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.Merge(st, pr);

            Assert.AreEqual(st.Name, pr.Name);
            Assert.AreEqual(st.Surname, pr.Surname);
            Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita);
        }
        public void TestDefaultMerger()
        {
            var merger = FactoryMapper.DynamicResolutionMerger<Student, Person>();

            Assert.IsTrue(merger.PropertyMappers.Count() == 3);

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 1 };
            Person pr = new Person();

            merger.Merge(st, pr);

            Assert.AreEqual(st.Name, pr.Name);
            Assert.AreEqual(st.Surname, pr.Surname);
            Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita);
        }
        public void TestDefaultMapper()
        {
            var mapper = FactoryMapper.DynamicResolutionMapper<Student, Person>();

            Student st = new Student{Name = "mario", Surname = "monti", AnnoNascita = 19};
            Person pr = mapper.Map(st);

            Assert.AreEqual(st.Name, pr.Name);
            Assert.AreEqual(st.Surname, pr.Surname);
            Assert.AreEqual(st.AnnoNascita, pr.AnnoNascita);
        }
        public void TestMapper()
        {
            TransformerObserver observer = new TransformerObserver();
            ISourceMapper<Student, Person> mapper1 = FactoryMapper.DynamicResolutionMapper<Student, Person>();

            observer.RegisterMapper(mapper1);
            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };

            var res = observer.TryToMap<Student, Person>(st);
            Assert.IsNotNull(res);
            Assert.AreEqual(st.Name, res.Name);
            Assert.AreEqual(st.Surname, res.Surname);
            Assert.IsNull(st.Father);
            Assert.IsNull(res.Parent);
        }
        public void RegisterMapperTest2()
        {
            TransformerObserver observer = new TransformerObserver();
            IList<IPropertyMapper<Student, Person>> propMappers = new List<IPropertyMapper<Student, Person>>
                {
                    new PropertyMapper<Student, Person>( (student, person) => person.Name = student.Name, "Name", "Name")
                    ,new PropertyMapper<Student, Person>( (student, person) => person.AnnoNascita = student.AnnoNascita )
                    ,new PropertyMapper<Student, Person>( (student, person) => person.Parent = student.Father )
                };
            SourceMapper<Student, Person> mapper1 = new SourceMapper<Student, Person>(propMappers, null, null);

            Assert.IsTrue(observer.RegisterMapper(mapper1));
            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type1));
            Assert.IsTrue(observer.RegisterMapper(mapper1, KeyService.Type2));
            Assert.IsFalse(observer.RegisterMapper(mapper1));
            Assert.IsFalse(observer.RegisterMapper(mapper1, "default"));

            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };

            var res1 = observer.TryToMap<Student, Person>(st);
            Assert.IsNotNull(res1);

            var res11 = observer.TryToMap(st, typeof(Person));
            Assert.IsTrue(res11 is Person);
            Assert.IsNotNull(res11);

            var res2 = observer.TryToMap(st, typeof(Person), KeyService.Type1);
            Assert.IsNotNull(res2);

            var res22 = observer.TryToMap(st, typeof (Person), KeyService.Type1);
            Assert.IsNotNull(res22);

            var res0 = observer.TryToMap(st, typeof (Person), KeyService.Type3);
            Assert.IsNull(res0);

            var res00 = observer.TryToMap<Student, Person>(st, KeyService.Type3);
            Assert.IsNull(res00);
        }
        public void RegisterMapperTest1()
        {
            TransformerObserver observer = new TransformerObserver();
            IList<IPropertyMapper<Student, Person>> propMappers = new List<IPropertyMapper<Student, Person>>
                {
                    new PropertyMapper<Student, Person>( (student, person) => person.Name = student.Name, "Name", "Name")
                    ,new PropertyMapper<Student, Person>( (student, person) => person.AnnoNascita = student.AnnoNascita )
                    ,new PropertyMapper<Student, Person>( (student, person) => person.Parent = student.Father )
                };
            SourceMapper<Student, Person> mapper1 = new SourceMapper<Student, Person>(propMappers, null, null);

            observer.RegisterMapper(mapper1);
            Student st = new Student { Name = "mario", Surname = "monti", AnnoNascita = 19 };
            StudentDetails st2 = new StudentDetails { Name = "mario", Surname = "monti", AnnoNascita = 19, CF = "CF"};

            var res = observer.TryToMap<Student, Person>(st);
            var res2 = observer.TryToMap<Student, Person>(st2);
            Assert.IsNotNull(res);
            Assert.IsNotNull(res2);
        }