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 static void InitializeEntitiesAndDTOs(TestContext testContext) { animalDTOBase = new PigeonDTO() { Name = "Pidgey", MaxAltitude = 100, IsReal = true, Skill = SpecialSkill.FLY }; animalBase = new Pigeon("Pidgey", 100, true, 0, SpecialSkill.FLY); foreach (int value in Enumerable.Range(1, 10)) { Animal pigeon = new Pigeon($"PidgeyNo.{value}", value * 100, false, value - 1, SpecialSkill.FLY); Animal hawk = new Hawk($"FearowNo.{value}", value * 200, false, value - 1, SpecialSkill.FLY, (Bird)pigeon); Animal rattlesnake = new Rattlesnake($"RattlesnakeNo.{value}", value * 10, true, SpecialSkill.BITE); animalsBase.Add(pigeon); animalsBase.Add(hawk); animalsBase.Add(rattlesnake); AnimalDTO pigeonDTO = new PigeonDTO() { Name = $"PidgeyNo.{value}", MaxAltitude = value * 100, IsReal = false, Skill = SpecialSkill.FLY }; AnimalDTO hawkDTO = new HawkDTO() { Name = $"FearowNo.{value}", MaxAltitude = value * 200, IsReal = false, Skill = SpecialSkill.FLY, FlyingPartner = (Bird)pigeon }; AnimalDTO rattlesnakeDTO = new RattlesnakeDTO() { Name = $"RattlesnakeNo.{value}", Length = value * 10, IsPoisonous = true, Skill = SpecialSkill.BITE }; animalDTOsBase.Add(pigeonDTO); animalDTOsBase.Add(hawkDTO); animalDTOsBase.Add(rattlesnakeDTO); } }
public void GenerateConstructorArguments_ConstructorHasArgs_ReturnsArray() { // Arrange Mapper <AnimalDTO, Animal> mapper = new Mapper <AnimalDTO, Animal>(); AnimalDTO rattlenakeDTO = new RattlesnakeDTO() { Name = "", Length = 0.0, IsPoisonous = false, Skill = SpecialSkill.BITE }; object[] expectedArgs = new object[] { "", 0.0, false, SpecialSkill.BITE }; object[] actualArgs; // Act actualArgs = mapper.GenerateConstructorArguments(rattlenakeDTO); // Assert CollectionAssert.AreEqual(expectedArgs, actualArgs, "Constructor args were not generated as expected"); }
public void Map_HasExcludedProperties_ReturnsUpdatedObject() { // Arrange Mapper <Animal, AnimalDTO> mapper = new Mapper <Animal, AnimalDTO>(); AnimalDTO expectedRattlesnakeDTO = new RattlesnakeDTO() { Name = "Name", Length = 10.0, IsPoisonous = true, Skill = SpecialSkill.BITE }; Animal rattlesnake = new Rattlesnake("Name", 10.0, true, SpecialSkill.BITE); AnimalDTO actualRattlesnakeDTO; // Act actualRattlesnakeDTO = (RattlesnakeDTO)mapper.Map(rattlesnake, expectedRattlesnakeDTO); expectedRattlesnakeDTO = new RattlesnakeDTO() { Name = "Name", Length = 10.0, IsPoisonous = true, Skill = SpecialSkill.BITE }; // Assert foreach (PropertyInfo property in actualRattlesnakeDTO.GetType().GetProperties()) { object expectedValue = expectedRattlesnakeDTO .GetType() .GetProperty(property.Name) .GetValue(expectedRattlesnakeDTO); object actualValue = property .GetValue(actualRattlesnakeDTO); Assert.AreEqual(expectedValue, actualValue, $"{property.Name} is not as expected"); } }
public void ConvertCollection_CollectionOfEntities_ReturnsCollectionOfDTOs() { // Arrange Producer <Animal, AnimalDTO> producer = new Producer <Animal, AnimalDTO>(); IList <Animal> animals = animalsBase; IList <AnimalDTO> expectedAnimalDTOs = animalDTOsBase; IList <AnimalDTO> animalDTOs; // Act animalDTOs = producer.ConvertCollection(animals); // Assert foreach (AnimalDTO animalDTO in animalDTOs) { int index = animalDTOs.IndexOf(animalDTO); AnimalDTO expected = expectedAnimalDTOs[index]; switch (animalDTO) { case PigeonDTO _: { PigeonDTO pigeon = animalDTO as PigeonDTO; PigeonDTO expectedPigeon = expected as PigeonDTO; 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 HawkDTO _: { HawkDTO hawk = animalDTO as HawkDTO; HawkDTO expectedHawk = expected as HawkDTO; 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 RattlesnakeDTO _: { RattlesnakeDTO rattlesnake = animalDTO as RattlesnakeDTO; RattlesnakeDTO expectedRattleSnake = expected as RattlesnakeDTO; 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; } } }