Exemplo n.º 1
0
        public void Map_NoExcludedProperties_ReturnsSameObject()
        {
            // Arrange
            Mapper <AnimalDTO, Animal> mapper = new Mapper <AnimalDTO, Animal>();
            AnimalDTO rattlenakeDTO           = new RattlesnakeDTO()
            {
                Name        = "Name",
                Length      = 10.0,
                IsPoisonous = true,
                Skill       = SpecialSkill.ROLLOVER
            };
            Animal expectedRattlesnake = new Rattlesnake("", 0.0, false, SpecialSkill.BITE);
            Animal actualRattlesnake;

            // Act
            actualRattlesnake   = (Rattlesnake)mapper.Map(rattlenakeDTO, expectedRattlesnake);
            expectedRattlesnake = new Rattlesnake("", 0.0, false, SpecialSkill.BITE);

            // Assert
            foreach (PropertyInfo property in actualRattlesnake.GetType().GetProperties())
            {
                object expectedValue = expectedRattlesnake
                                       .GetType()
                                       .GetProperty(property.Name)
                                       .GetValue(expectedRattlesnake);

                object actualValue = property
                                     .GetValue(actualRattlesnake);

                Assert.AreEqual(expectedValue, actualValue, $"{property.Name} is not as expected");
            }
        }
        public void ConvertCollection_CollectionOfDTOs_ReturnsCollectionOfEntities()
        {
            // Arrange
            Producer <AnimalDTO, Animal> producer   = new Producer <AnimalDTO, Animal>();
            IList <AnimalDTO>            animalDTOs = animalDTOsBase;
            IList <Animal> expectedAnimals          = animalsBase;
            IList <Animal> animals;

            // Act
            animals = producer.ConvertCollection(animalDTOs);

            // Assert
            foreach (Animal animal in animals)
            {
                int    index    = animals.IndexOf(animal);
                Animal expected = expectedAnimals[index];

                switch (animal)
                {
                case Pigeon _:
                {
                    Pigeon pigeon         = animal as Pigeon;
                    Pigeon expectedPigeon = expected as Pigeon;
                    foreach (PropertyInfo property in pigeon.GetType().GetProperties())
                    {
                        Assert.AreEqual(
                            expectedPigeon.GetType().GetProperty(property.Name),
                            pigeon.GetType().GetProperty(property.Name),
                            $"{property.Name} does not match.");
                    }
                }
                break;

                case Hawk _:
                {
                    Hawk hawk         = animal as Hawk;
                    Hawk expectedHawk = expected as Hawk;
                    foreach (PropertyInfo property in hawk.GetType().GetProperties())
                    {
                        Assert.AreEqual(
                            expectedHawk.GetType().GetProperty(property.Name),
                            hawk.GetType().GetProperty(property.Name),
                            $"{property.Name} does not match.");
                    }
                }
                break;

                case Rattlesnake _:
                {
                    Rattlesnake rattlesnake         = animal as Rattlesnake;
                    Rattlesnake expectedRattleSnake = expected as Rattlesnake;
                    foreach (PropertyInfo property in rattlesnake.GetType().GetProperties())
                    {
                        Assert.AreEqual(
                            expectedRattleSnake.GetType().GetProperty(property.Name),
                            rattlesnake.GetType().GetProperty(property.Name),
                            $"{property.Name} does not match.");
                    }
                }
                break;
                }
            }
        }