コード例 #1
0
ファイル: UnitTest1.cs プロジェクト: Rubben/AVE201617VeraoG6
        public void TestMethodTimeILFasterthanReflex()
        {
            Student​ s = new Student();

            s.NrF   = 27721;
            s.NameF = "ze Manel";

            IMapper <Student​, Person> m = AutoMapper.Build <Student​, Person>().Bind(Mapping.FieldsReflex);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            for (int i = 0; i < 50000; i++)
            {
                Person p = (Person)m.Map(s);
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;


            m = AutoMapper.Build <Student​, Person>().Bind(Mapping.FieldsIl);


            watch = System.Diagnostics.Stopwatch.StartNew();


            for (int i = 0; i < 50000; i++)
            {
                Person p = (Person)m.Map(s);
            }
            watch.Stop();
            var elapsedMs1 = watch.ElapsedMilliseconds;

            Assert.IsTrue(elapsedMs1 < elapsedMs);
        }
コード例 #2
0
        public void TestMapWithCustomAttributes()
        {
            AutoMapper.mappers.Clear();
            IMapper m = AutoMapper
                        .Build(typeof(Student), typeof(Person))
                        .Bind(new Mapping(typeof(ToMapAttribute)))
                        .Match("Nr", "Id");
            Student s = new Student
            {
                Nr     = 27721,
                Name   = "Ze Manel",
                Height = 170,
                Org    = new School
                {
                    Name       = "FCT",
                    MembersIds = new int[] { 1, 2, 3 },
                    Location   = "Lisboa, Portugal"
                }
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Name, p.Name);
            Assert.AreEqual(s.Nr, p.Id);
            Assert.AreNotEqual(s.Height, p.Height);
            Assert.IsNull(p.Org);
        }
コード例 #3
0
        public void testMappingArray()
        {
            Mapper m = AutoMapper
                       .Build(typeof(Student), typeof(Person))
                       .Bind(Mapping.Fields)
                       .Match("Nr", "Id")
                       .Match("Courses", "Subjects");

            Student[] stds = { new Student {
                                   Nr = 42929, Name = "Gonçalo Barros"
                               }, new Student{
                                   Nr = 42144, Name = "Nuno Cardoso"
                               }, new Student{
                                   Nr = 42140, Name = "Marco Agostinho"
                               } };
            stds[0].Courses = new Course [] { new Course("AVE"), new Course("LS") };
            stds[1].Courses = new Course[] { new Course("LS") };
            stds[2].Courses = new Course[] { new Course("POO") };
            object[] ps  = m.Map(stds);
            Person[] pso = new Person[ps.Length];

            for (int i = 0; i < stds.Length; i++)
            {
                pso[i] = (Person)ps[i];
                Assert.AreEqual(stds[i].Nr, pso[i].Id);
            }
        }
コード例 #4
0
        public void CacheTest()
        {
            IMapper mapper1 = AutoMapper.Build(typeof(Student), typeof(Person));
            IMapper mapper2 = AutoMapper.Build(typeof(Student), typeof(Person));

            Assert.AreEqual(mapper1.GetHashCode(), mapper2.GetHashCode());
        }
コード例 #5
0
        public void ValueTypeTest()
        {
            IMapper m = AutoMapper.Build(typeof(int), typeof(int));
            int     x = 1;
            int     i = (int)m.Map(x);

            Assert.AreEqual(x, i);
        }
コード例 #6
0
        public void TestObjectNotMapped()
        {
            IMapper m       = AutoMapper.Build(typeof(Student), typeof(Person)).Match("Nr", "Id");
            Teacher teacher = new Teacher(123, "t");
            Person  p       = (Person)m.Map(teacher);

            Assert.IsNull(p);
        }
コード例 #7
0
        public void TestPerformance()
        {
            Action reflectionAction = () => AutoMapper.Build(typeof(Student), typeof(Person));
            Action emitAction       = () => AutoMapperEmit.Build(typeof(Student), typeof(Person));
            long   reflection       = Benchmark.Bench(reflectionAction, "Reflection - Person");
            long   emit             = Benchmark.Bench(emitAction, "Emit - Person");

            Assert.IsTrue(reflection < emit);
        }
コード例 #8
0
        public void TestPrivateFields()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person));
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel", field = 200
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.getPrivateField(), p.getPrivateField());
        }
コード例 #9
0
        public void TestPublicProperties()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person));
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel", field = 200
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Name, p.Name);
        }
コード例 #10
0
        public void MapTest()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person));
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel"
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Name, p.Name);
        }
コード例 #11
0
        public void MapParametersTest()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Course));
            Student s = new Student {
                ImAField = "ImAField", Nickname = "Zezito", Nr = 27721, Name = "Ze Manel", Address = "Rua de Cima"
            };
            Course c = (Course)m.Map(s);

            Assert.AreEqual(s.Address, c.Address);
        }
コード例 #12
0
        public void TestMatch()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person)).Match("Nr", "Id");
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel", field = 200
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Nr, p.Id);
        }
コード例 #13
0
        public void testAttributesBuildWithBind()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person)).Bind(Mapping.Fields);
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel"
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Name, p.Name);
        }
コード例 #14
0
        public void testMappingCustomAttr()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person))
                        .Bind(new MappingCustomAttributes(typeof(ToMapAttribute)));
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel"
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Name, p.Name);
        }
コード例 #15
0
        public void MapByBindTest()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person))
                        .Bind(Mapping.Fields);
            Student s = new Student {
                ImAField = "ImAField", Nickname = "Zezito", Nr = 27721, Name = "Ze Manel"
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.ImAField, p.ImAField);
        }
コード例 #16
0
        public void MapCustomAttributeTest()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person))
                        .Bind(Mapping.CustomAttributes);
            Student s = new Student {
                ImAField = "ImAField", Nickname = "Zezito", Nr = 27721, Name = "Ze Manel"
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Nickname, p.Nickname);
        }
コード例 #17
0
ファイル: UnitTest1.cs プロジェクト: Rubben/AVE201617VeraoG6
        public void TestMethodStudentToPersonFieldsReflex()
        {
            Student​ s = new Student();

            s.NrF   = 27721;
            s.NameF = "ze Manel";

            IMapper <Student​, Person> m = AutoMapper.Build <Student​, Person>().Bind(Mapping.FieldsReflex);
            Person p = (Person)m.Map(s);

            Assert.IsTrue(s.NameF.Equals(p.NameF));
        }
コード例 #18
0
        public void ReferenceField()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person)).Match("Nr", "Id");
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel", field = 200, teacher = new Teacher(123, "t")
            };
            Person p = (Person)m.Map(s);

            Object.ReferenceEquals(s.teacher, p.teacher);
            Assert.IsFalse(Object.ReferenceEquals(s.teacher, p.teacher));
            Assert.AreEqual(s.teacher.name, p.teacher.name);
        }
コード例 #19
0
ファイル: UnitTest1.cs プロジェクト: Rubben/AVE201617VeraoG6
        public void TestMethodFor()
        {
            Student​ s = new Student();

            s.NrF   = 27721;
            s.NameF = "ze Manel";

            IMapper <Student​, Person> m = AutoMapper.Build <Student​, Person>().Bind(Mapping.FieldsIl).For("IdF", () => 11111);

            Person p = (Person)m.Map(s);

            Assert.IsTrue(s.NameF.Equals(p.NameF) && p.IdF == 11111);
        }
コード例 #20
0
ファイル: UnitTest1.cs プロジェクト: Rubben/AVE201617VeraoG6
        public void TestMethodMapperAutoMapperILUsingProprieties()
        {
            Student​ s = new Student();

            s.NrP   = 27721;
            s.NameP = "ze Manel";


            IMapper <Student​, Person> m = AutoMapper.Build <Student​, Person>().Bind(Mapping.PropertiesIl);

            Person p = (Person)m.Map(s);

            Assert.IsTrue(s.NameP.Equals(p.NameP));
        }
コード例 #21
0
        public void TestStructProperties()
        {
            AutoMapper.mappers.Clear();
            IMapper m = AutoMapper
                        .Build(typeof(Nurse), typeof(Student));

            Nurse nrs = new Nurse {
                Name = "D. Cristina", Nr = 1
            };
            Student s = (Student)m.Map(nrs);

            Assert.AreEqual(nrs.Name, s.Name);
            Assert.AreEqual(nrs.Nr, s.Nr);
        }
コード例 #22
0
    static void Main()
    {
        Mapper <Student, Person> mapper = AutoMapper
                                          .Build <Student, Person>()
                                          // .IgnoreMember("Nr")
                                          // .IgnoreMember("Name");
                                          .To("Org", "IBM");
        Student s = new Student {
            Nr = 27721, Name = "Ze Manel", School = "Isel"
        };
        Person p = mapper.Map(s);

        Console.WriteLine(p);
    }
コード例 #23
0
        public void TestMapObject()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person)).Match("Nr", "Id");
            Student s = new Student {
                Nr = 27721, Name = "Ze Manel", field = 200
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Nr, p.Id);
            Assert.AreEqual(s.Name, p.Name);
            Assert.AreEqual(s.field, p.field);
            Assert.AreEqual(s.getPrivateProperty(), p.getPrivateProperty());
            Assert.AreEqual(s.getPrivateField(), p.getPrivateField());
        }
コード例 #24
0
        public void testAttributesBuildWithMatchAndEmit()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person))
                        .Bind(new MappingEmit())
                        .Match("Nr", "Id");

            Student s = new Student {
                Nr = 27721, Name = "Ze Manel"
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Nr, p.Id);
            Assert.AreEqual(s.Name, p.Name);
        }
コード例 #25
0
        public void TestMapField_Default()
        {
            AutoMapper.mappers.Clear();
            IMapper m = AutoMapper
                        .Build(typeof(Doctor), typeof(Patient))
                        .Bind(Mapping.Fields);

            Doctor d = new Doctor {
                doctorNr = 213, name = "Zé Manel", localization = "Chelas"
            };
            Patient pt = (Patient)m.Map(d);

            Assert.AreEqual(pt.name, d.name);
            Assert.AreEqual(pt.localization, d.localization);
        }
コード例 #26
0
        public void TestMatchComplexFields()
        {
            AutoMapper.mappers.Clear();
            IMapper m = AutoMapper
                        .Build(typeof(Doctor), typeof(Person))
                        .Bind(Mapping.Fields)
                        .Match("doctorNr", "Id");

            Doctor d = new Doctor {
                doctorNr = 213, name = "Zé Manel"
            };
            Person p = (Person)m.Map(d);

            Assert.AreEqual(p.Id, d.doctorNr);
        }
コード例 #27
0
        public void MapByMatchTest()
        {
            int[]   array = { 27999, 27898, 27162 };
            IMapper m     = AutoMapper.Build(typeof(Student), typeof(Person))
                            .Match("Nr", "Id")
                            .Match("Org", "Org");
            Student s = new Student {
                ImAField = "ImAField", Nickname = "Zezito", Nr = 27721, Name = "Ze Manel",
                Org      = new School("Lisbon", array, "ISEL")
            };
            Person p = (Person)m.Map(s);

            Assert.AreEqual(s.Nr, p.Id);
            Assert.AreEqual(s.Org.Name, p.Org.Name);
        }
コード例 #28
0
        public void TestCache()
        {
            AutoMapper.mappers.Clear();
            IMapper msp1 = AutoMapper
                           .Build(typeof(Student), typeof(Person));
            IMapper mst1 = AutoMapper
                           .Build(typeof(Student), typeof(Teacher));
            IMapper msp2 = AutoMapper
                           .Build(typeof(Student), typeof(Person));
            IMapper mst2 = AutoMapper
                           .Build(typeof(Student), typeof(Teacher));

            Assert.AreSame(msp1, msp2);
            Assert.AreSame(mst1, mst2);
        }
コード例 #29
0
        public void MapArrayTest()
        {
            IMapper m = AutoMapper.Build(typeof(Student), typeof(Person));

            Student [] s = { new Student {
                                 ImAField = "ImAField", Nickname = "Zezito", Nr = 27721, Name = "Ze Manel"
                             },
                             new Student {
                                 ImAField = "ImAField", Nickname = "Xico", Nr = 27722, Name = "Francisco"
                             } };
            Person []  p = (Person[])m.Map(s);
            for (int i = 0; i < p.Length; ++i)
            {
                Assert.AreEqual(s[i].Name, p[i].Name);
            }
        }
コード例 #30
0
        public void TestMapPropertiesToConstructor_Default()
        {
            AutoMapper.mappers.Clear();
            IMapper m = AutoMapper
                        .Build(typeof(Student), typeof(Teacher));
            Student s = new Student
            {
                Nr     = 27721,
                Name   = "Ze Manel",
                Height = 170
            };
            Teacher t = (Teacher)m.Map(s);

            Assert.AreEqual(s.Name, t.Name);
            Assert.AreEqual(s.Nr, t.Id);
        }