Exemplo n.º 1
0
        public void TestGroupByExceptions()
        {
            List <ProductsList.Product> products1 = null;

            Func <ProductsList.Product, string> elementSelector = x => x.Name;

            Func <ProductsList.Product, int> keySelector = x => x.Ingredients.Count;

            Func <int, IEnumerable <string>, KeyValuePair <int, IEnumerable <string> > > resultSelector = (IngredientsCount, ProductNames) =>
            {
                return(new KeyValuePair <int, IEnumerable <string> >(IngredientsCount, ProductNames));
            };

            var result = LinqFunctions.GroupBy(products1,
                                               x => keySelector(x),
                                               y => elementSelector(y),
                                               (IngredientCount, ProductNames) => resultSelector(IngredientCount, ProductNames),
                                               EqualityComparer <int> .Default
                                               );
            var numerator = result.GetEnumerator();

            var exception = Assert.Throws <ArgumentNullException>(() => numerator.MoveNext());

            Assert.Equal("source", exception.ParamName);
        }
Exemplo n.º 2
0
        public void TestGroupBy()
        {
            var products = new List <ProductsList.Product>()
            {
                new ProductsList.Product//2 ingredients
                {
                    ID          = 2,
                    Name        = "Dero",
                    Price       = 10,
                    Ingredients = new List <ProductsList.Ingredient> {
                        new ProductsList.Ingredient {
                            Name = "Lamaie"
                        }, new ProductsList.Ingredient {
                            Name = "Parfum1"
                        }
                    }
                },

                new ProductsList.Product//2 ingredients
                {
                    ID          = 2,
                    Name        = "Dero",
                    Price       = 10,
                    Ingredients = new List <ProductsList.Ingredient> {
                        new ProductsList.Ingredient {
                            Name = "Lamaie"
                        }, new ProductsList.Ingredient {
                            Name = "Parfum5"
                        }
                    }
                },

                new ProductsList.Product//1 ingredient
                {
                    ID          = 6,
                    Name        = "Sampon",
                    Price       = 10,
                    Ingredients = new List <ProductsList.Ingredient> {
                        new ProductsList.Ingredient {
                            Name = "Menta"
                        }
                    }
                },

                new ProductsList.Product//3 ingredients
                {
                    ID          = 2,
                    Name        = "Detergent",
                    Price       = 11,
                    Ingredients = new List <ProductsList.Ingredient> {
                        new ProductsList.Ingredient {
                            Name = "Lamaie"
                        }, new ProductsList.Ingredient {
                            Name = "Parfum1"
                        }, new ProductsList.Ingredient {
                            Name = "Parfum5"
                        }
                    }
                },
            };

            Func <ProductsList.Product, string> elementSelector = x => x.Name;

            Func <ProductsList.Product, int> keySelector = x => x.Ingredients.Count;

            Func <int, IEnumerable <string>, KeyValuePair <int, IEnumerable <string> > > resultSelector = (IngredientsCount, ProductNames) =>
            {
                return(new KeyValuePair <int, IEnumerable <string> >(IngredientsCount, ProductNames));
            };

            var result = LinqFunctions.GroupBy(products,
                                               x => keySelector(x),
                                               y => elementSelector(y),
                                               (IngredientCount, ProductNames) => resultSelector(IngredientCount, ProductNames),
                                               EqualityComparer <int> .Default
                                               );

            var numerator = result.GetEnumerator();

            numerator.MoveNext();

            Assert.Equal(new string[] { "Dero", "Dero" }, numerator.Current.Value.ToArray());
            Assert.Equal("2", numerator.Current.Key.ToString());
            Assert.True(numerator.MoveNext());
            Assert.Equal(new string[] { "Sampon" }, numerator.Current.Value.ToArray());
            Assert.Equal("1", numerator.Current.Key.ToString());
            Assert.True(numerator.MoveNext());
            Assert.Equal(new string[] { "Detergent" }, numerator.Current.Value.ToArray());
            Assert.Equal("3", numerator.Current.Key.ToString());
            Assert.False(numerator.MoveNext());
        }