Пример #1
0
        public void TestToDictionaryWhenDuplicateKeysException()
        {
            var productList = new ProductsList().GetProducts();

            var prodToAdd = new ProductsList.Product
            {
                ID          = 6,
                Name        = "Deodorant",
                Price       = 6,
                Ingredients = new List <ProductsList.Ingredient> {
                    new ProductsList.Ingredient {
                        Name = "Fresh"
                    }, new ProductsList.Ingredient {
                        Name = "Parfum4"
                    }
                }
            };

            productList.Add(prodToAdd);

            Func <ProductsList.Product, int>    myKeyFunc     = (x) => x.ID;
            Func <ProductsList.Product, string> myElementFunc = (x) => x.Name;

            var dictionary = Assert.Throws <ArgumentException>(() => LinqFunctions.ToDictionary(productList, p => myKeyFunc(p), z => myElementFunc(z)));

            Assert.Equal("An item with the same key has already been added. Key: 6", dictionary.Message);
        }
Пример #2
0
        public void TestToDictionaryWhenExists()
        {
            var productList = new ProductsList().GetProducts();

            Func <ProductsList.Product, int>    myKeyFunc     = (x) => x.ID;
            Func <ProductsList.Product, string> myElementFunc = (x) => x.Name;

            var dictionary = LinqFunctions.ToDictionary(productList, p => myKeyFunc(p), z => myElementFunc(z));

            var test1 = new KeyValuePair <int, string>(2, "Detergent");
            var test2 = new KeyValuePair <int, string>(3, "Sapun");
            var test3 = new KeyValuePair <int, string>(6, "Sampon");
            var test4 = new KeyValuePair <int, string>(10, "Biocid");


            Assert.Contains(test1, dictionary);
            Assert.Contains(test2, dictionary);
            Assert.Contains(test3, dictionary);
            Assert.DoesNotContain(test4, dictionary);
        }