public void DeserializeNonExistentFile() { ShopUtils.CreateFiles(); Action action = delegate { ShopUtils.DeserializeProducts("null"); }; Assert.ThrowsException <FileNotFoundException>(action); }
public void InvalidJsonFormatting() { ShopUtils.CreateFiles(); List <Product> products = new List <Product>() { new Product(), new Product(), new Product() }; string path = ShopUtils.GetFilePath("TESTCART.json"); Action action = delegate { File.WriteAllText(path, "This is not json"); products = ShopUtils.DeserializeProducts(path); }; Assert.ThrowsException <JsonException>(action); //If the exception is thrown, the product count should still remain 3 Assert.AreEqual(3, products.Count); }
private void Start() { System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; ShopUtils.CreateFiles(); productList = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("Products.json")); couponList = Coupon.DeserializeCoupons(); #region Custom brushes // declare a brushconverter to convert a hex color code string to a Brush color BrushConverter brushConverter = new System.Windows.Media.BrushConverter(); Brush backgroundBrush = (Brush)brushConverter.ConvertFromString("#2F3136"); listBoxBrush = (Brush)brushConverter.ConvertFromString("#36393F"); textBoxBrush = (Brush)brushConverter.ConvertFromString("#40444B"); #endregion // Window options Title = "Sortimenthanteraren"; Width = 1100; Height = 600; WindowStartupLocation = WindowStartupLocation.CenterScreen; // Changes the Window icon Uri iconUri = new Uri("Images/Ica.png", UriKind.RelativeOrAbsolute); this.Icon = BitmapFrame.Create(iconUri); // Scrolling ScrollViewer root = new ScrollViewer(); root.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; Content = root; // Main grid Grid mainGrid = new Grid(); root.Content = mainGrid; mainGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); mainGrid.RowDefinitions.Add(new RowDefinition()); // first column contains a stackpanel with buttons which determines what grid to create, the second column contains a class field grid "buttonClickGrid" which is the parent grid for the eventhandlers nested grids mainGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); mainGrid.ColumnDefinitions.Add(new ColumnDefinition()); mainGrid.Background = backgroundBrush; // Window heading, always visible TextBlock headingTextBlock = ShopUtils.CreateTextBlock("Sortimenthanteraren", 18, TextAlignment.Center); mainGrid.Children.Add(headingTextBlock); Grid.SetRow(headingTextBlock, 0); Grid.SetColumnSpan(headingTextBlock, 2); StackPanel buttonPanel = new StackPanel { VerticalAlignment = VerticalAlignment.Top }; mainGrid.Children.Add(buttonPanel); Grid.SetColumn(buttonPanel, 0); Grid.SetRow(buttonPanel, 1); // using a empty class field grid to be able to add nested grids from the eventhandlers buttonClickGrid = new Grid(); mainGrid.Children.Add(buttonClickGrid); Grid.SetRow(buttonClickGrid, 1); Grid.SetColumn(buttonClickGrid, 1); // each button declares a new grid which is added as buttonClickGrid's only child to display a menu to add/edit/remove coupons and products Button editProductButton = ShopUtils.CreateButton("Ändra produkter"); editProductButton.Padding = new Thickness(10); buttonPanel.Children.Add(editProductButton); editProductButton.Click += CreateEditProductGrid; Button addProductButton = ShopUtils.CreateButton("Lägg till produkt"); addProductButton.Padding = new Thickness(10); buttonPanel.Children.Add(addProductButton); addProductButton.Click += CreateAddProductGrid; Button EditCouponsButton = ShopUtils.CreateButton("Lägg till/Ändra kuponger"); EditCouponsButton.Padding = new Thickness(10); buttonPanel.Children.Add(EditCouponsButton); EditCouponsButton.Click += CreateEditCouponsGrid; }
public void SaveCartTest() { ShopUtils.CreateFiles(); List <Product> preSaveCart = new List <Product>() { new Product() { Title = "Äpple", Category = "Mat", Count = 14, Price = 4.99M, Description = "Rött äpple", ProductImage = "" }, new Product() { Title = "Banan", Category = "Mat", Count = 4, Price = 9.99M, Description = "Guleböj", ProductImage = "" }, new Product() { Title = "Päron", Category = "Mat", Count = 4541354, Price = 0.99M, Description = "Lila Päron", ProductImage = "" } }; preSaveCart.Serialize(ShopUtils.GetFilePath("TESTCART.json")); List <Product> deserializedCart = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("TESTCART.json")); bool areEqual = deserializedCart.Count == preSaveCart.Count; if (areEqual) { for (int i = 0; i < preSaveCart.Count; i++) { foreach (PropertyInfo p in preSaveCart[i].GetType().GetProperties()) { if (p.GetValue(preSaveCart[i]).ToString() != p.GetValue(deserializedCart[i]).ToString()) { areEqual = false; //If they are not equal we should break out of the loop break; } } //If they are not equal, we break out of the loop and the test will fail. if (!areEqual) { break; } } } Assert.AreEqual(true, areEqual); }