Exemple #1
0
 public void OnPost()
 {
     Products = ProductSorter.GetProductsWithTags(_products.ReadAll(), Tags);
     if (Products.Count == 0)
     {
         Products = _products.ReadAll();
     }
 }
Exemple #2
0
        public void Can_Sort_Products_Descendingly_By_Price()
        {
            var products = CreateProducts();

            var sortedProducts = new ProductSorter(products).Sort(SortOption.High).ToList();

            Assert.Equal(50, sortedProducts[0].Price);
            Assert.Equal(20, sortedProducts[1].Price);
            Assert.Equal(10, sortedProducts[2].Price);
        }
Exemple #3
0
        public void Can_Sort_Products_Descendingly_By_Name()
        {
            var products = CreateProducts();

            var sortedProducts = new ProductSorter(products).Sort(SortOption.Descending).ToList();

            Assert.Equal("ProductC", sortedProducts[0].Name);
            Assert.Equal("ProductB", sortedProducts[1].Name);
            Assert.Equal("ProductA", sortedProducts[2].Name);
        }
Exemple #4
0
        public async Task ProductSorter_Should_Correctly_Sort_Product_Based_On_Popularity(PopularitySortTestData testData)
        {
            var apiCaller = new Mock <IApiCaller>();

            apiCaller.Setup(x => x.GetProducts()).ReturnsAsync(testData.AllProducts);
            apiCaller.Setup(x => x.GetShopperHistory()).ReturnsAsync(testData.ShopperHistoryReponse);

            var sut = new ProductSorter(apiCaller.Object);

            var result = await sut.SortProducts(SortOptions.Recommended.ToString());

            Assert.Equal(testData.ExpectedResult.Select(x => x.Name), result.Select(x => x.Name));
        }
        public void EmptyListShouldSortToEmpty()
        {
            // arrange
            //   any necessary setup before the "act". everything in this section is assumed to work correctly.
            //   (if possible/relevant, it should itself get other unit tests.)
            var sorter    = new ProductSorter();
            var emptyList = new List <Product>();

            // act
            //    the specific thing (usually 1 method call) that this method is responsible for testing.
            sorter.Sort(emptyList);

            // assert
            //   run checks as much as possible to verify the correct behavior.
            // in xUnit, use Assert static class.
            Assert.Empty(emptyList);

            // in case of an unhandled exception, the test is considered failed.
        }
        [Fact] // marks it as a test (otherwise, it would be a helper method)
        public void SortListWithItemsShouldSortByName()
        {
            // arrange
            var sorter   = new ProductSorter();
            var product1 = new Product {
                Name = "A"
            };
            var product2 = new Product {
                Name = "B"
            };
            var list = new List <Product> {
                product2, product1
            };

            // act
            sorter.Sort(list);

            // assert
            Assert.Equal(2, list.Count);     // unchanged length (expected first, actual second)
            Assert.Equal(list[0], product1); // in C#, the indexing operator [] isn't just for arrays
            Assert.Equal(list[1], product2);
        }
Exemple #7
0
        public async Task <IEnumerable <Product> > SortProducts([FromQuery] SortOption sortOption)
        {
            var products = await _resourceClient.GetProducts();

            return(await ProductSorter.Sort(sortOption, products, _resourceClient));
        }
Exemple #8
0
        static void Main(string[] args)
        {
            var c = new Circle(4);

            Console.WriteLine(c); // (WriteLine uses ToString to convert objects to strings (as does the + operator))
            Console.WriteLine(c.GetArea());

            LocatedCircle circle2 = new LocatedCircle(3, 0, 0);

            Console.WriteLine(circle2.GetArea()); // prints: 0

            //                                  (implicit) upcast
            DisplayCircles(new List <Circle> {
                c, circle2
            });

            var product = new Product();

            product.Color = "red";
            product.Name  = "bicycle";
            product.Price = 200;

            product.ApplyDiscount(8); // 8% off

            Console.WriteLine($"price after discount: ${product.Price}");

            Console.WriteLine("Hello World!");

            ProductSorter        sorter1 = new ProductSorter();
            BuiltinProductSorter sorter2 = new BuiltinProductSorter();

            ISortingAlgorithm <List <Product> > sorterToUse;

            // based on some logic, choose which sorter to use
            if (true)
            {
                // the BuiltinProductSorter value
                // is being implicitly casted to ISortingAlgorithm<List<Product>> type.
                sorterToUse = sorter2;

                // this is allowed by the compiler specifically because that class
                // does indeed implement that interface.
                // this is also called "upcasting"

                // downcasting example:
                //ProductSorter sorter3 = (ProductSorter)sorterToUse; // this might fail: what if it's really a BuiltinProductSorter value?
                // so it must be explicit. this line will throw an error at runtime because it really is not a ProductSorter.

                // in VS:   comment with Ctrl+K, Ctrl+C
                // in VS: uncomment with Ctrl+K, Ctrl+U
            }

            // ask the user if it's reverse or not
            // if (true)
            // {
            //     sorterToUse.SortInReverse = true;
            // }

            DisplayProducts(new List <Product> {
                product
            }, sorterToUse);
        }