public void ShouldSerializeWhenAbstractionRefersToAnotherAbstraction() { // Given KnownTypesContainer knownTypes = new KnownTypesContainer().Add <ProductCategory>("Abstraction1").Add <Milk>("milk").Add <Bread>("bread"); var milk = new Milk { Caption = "Первый вкус", Protein = 2.9f }; var bread = new Bread { Caption = "Бородинский", Richness = 365 }; var target = new ProductCategory { Products = new IProduct[] { milk, bread } }; // When ProductCategory result = SerializeAndDeserialize(target, knownTypes); // Then Assert.IsNotNull(result); Assert.IsNotNull(result.Products); Assert.AreEqual(2, result.Products.Count()); Assert.IsInstanceOf <Milk>(result.Products.ElementAt(0)); Assert.AreEqual(milk.Caption, ((Milk)result.Products.ElementAt(0)).Caption); Assert.AreEqual(milk.Protein, ((Milk)result.Products.ElementAt(0)).Protein); Assert.IsInstanceOf <Bread>(result.Products.ElementAt(1)); Assert.AreEqual(bread.Caption, ((Bread)result.Products.ElementAt(1)).Caption); Assert.AreEqual(bread.Richness, ((Bread)result.Products.ElementAt(1)).Richness); }
public void ShouldSerializeInterfaceArray() { // Given KnownTypesContainer knownTypes = new KnownTypesContainer().Add <Milk>("milk").Add <Bread>("bread"); var milk = new Milk { Caption = "Первый вкус", Protein = 2.9f }; var bread = new Bread { Caption = "Бородинский", Richness = 365 }; var target = new IProduct[] { milk, bread }; // When IProduct[] result = SerializeAndDeserialize(target, knownTypes); // Then Assert.IsNotNull(result); Assert.AreEqual(2, result.Length); Assert.IsInstanceOf <Milk>(result[0]); Assert.AreEqual(milk.Caption, ((Milk)result[0]).Caption); Assert.AreEqual(milk.Protein, ((Milk)result[0]).Protein); Assert.IsInstanceOf <Bread>(result[1]); Assert.AreEqual(bread.Caption, ((Bread)result[1]).Caption); Assert.AreEqual(bread.Richness, ((Bread)result[1]).Richness); }
public void ShouldSerializeCustomCollectionReference() { // Given KnownTypesContainer knownTypes = new KnownTypesContainer().Add <Milk>("milk").Add <Bread>("bread").Add <Employee>("employee"); var milk = new Milk { Caption = "Первый вкус", Protein = 2.9f }; var bread = new Bread { Caption = "Бородинский", Richness = 365 }; var item1 = new OrderItem { Product = milk, Count = 1, Price = 45.5f }; var item2 = new OrderItem { Product = bread, Count = 1, Price = 20.3f }; var client = new Employee { FirstName = "Вася", JobTitle = "Аналитик" }; var order1 = new Order { Client = client, Items = new[] { item1 } }; var order2 = new Order { Client = client, Items = new[] { item2 } }; var history = new OrderHistory(); history.AddOrder(order1); history.AddOrder(order2); var target = new Account { OrderHistory = history }; // When Account result = SerializeAndDeserialize(target, knownTypes); // Then Assert.IsNotNull(result); Assert.IsNotNull(result.OrderHistory); Assert.AreEqual(2, result.OrderHistory.Count()); Assert.IsInstanceOf <Employee>(result.OrderHistory.ElementAt(0).Client); Assert.AreEqual(client.FirstName, result.OrderHistory.ElementAt(0).Client.FirstName); Assert.AreEqual(client.JobTitle, ((Employee)result.OrderHistory.ElementAt(0).Client).JobTitle); Assert.IsNotNull(result.OrderHistory.ElementAt(0).Items); Assert.AreEqual(1, result.OrderHistory.ElementAt(0).Items.Count()); Assert.IsInstanceOf <Milk>(result.OrderHistory.ElementAt(0).Items.ElementAt(0).Product); Assert.AreEqual(milk.Caption, ((Milk)result.OrderHistory.ElementAt(0).Items.ElementAt(0).Product).Caption); Assert.AreEqual(milk.Protein, ((Milk)result.OrderHistory.ElementAt(0).Items.ElementAt(0).Product).Protein); Assert.AreEqual(item1.Count, result.OrderHistory.ElementAt(0).Items.ElementAt(0).Count); Assert.AreEqual(item1.Price, result.OrderHistory.ElementAt(0).Items.ElementAt(0).Price); Assert.IsInstanceOf <Bread>(result.OrderHistory.ElementAt(1).Items.ElementAt(0).Product); Assert.AreEqual(bread.Caption, ((Bread)result.OrderHistory.ElementAt(1).Items.ElementAt(0).Product).Caption); Assert.AreEqual(bread.Richness, ((Bread)result.OrderHistory.ElementAt(1).Items.ElementAt(0).Product).Richness); Assert.AreEqual(item2.Count, result.OrderHistory.ElementAt(1).Items.ElementAt(0).Count); Assert.AreEqual(item2.Price, result.OrderHistory.ElementAt(1).Items.ElementAt(0).Price); }
public void ShouldSerializeInterfaceReference() { // Given KnownTypesContainer knownTypes = new KnownTypesContainer().Add <Milk>("milk"); var milk = new Milk { Caption = "Первый вкус", Protein = 2.9f }; var target = new OrderItem { Product = milk, Count = 1, Price = 45.5f }; // When OrderItem result = SerializeAndDeserialize(target, knownTypes); // Then Assert.IsNotNull(result); Assert.IsInstanceOf <Milk>(result.Product); Assert.AreEqual(milk.Caption, ((Milk)result.Product).Caption); Assert.AreEqual(milk.Protein, ((Milk)result.Product).Protein); Assert.AreEqual(target.Count, result.Count); Assert.AreEqual(target.Price, result.Price); }