예제 #1
0
        public void ToArray()
        {
            NonEmptyList <int> x = NonEmptyList.Singleton(1);

            int[] a = x.ToArray();
            CollectionAssert.AreEqual(new[] { 1 }, a);
        }
        public void SingleScopeRegistrationTest()
        {
            const string scope       = "openid";
            var          requirement = new ScopeAuthorizationRequirement(NonEmptyList.Create(scope));

            Assert.Equal(scope, requirement.RequiredScopes.ToList().First());
        }
예제 #3
0
        public void Concat_FSharpList_extension()
        {
            var x = NonEmptyList.Singleton(1);

            x = x.Concat(FSharpList.Create(2, 3, 4));
            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 }, x);
        }
예제 #4
0
        public void Reverse()
        {
            var x = NonEmptyList.Create(1, FSharpList.Create(2, 3, 4));

            x = x.Reverse();
            CollectionAssert.AreEqual(new[] { 4, 3, 2, 1 }, x);
        }
예제 #5
0
        public void ToFSharpList()
        {
            NonEmptyList <int> x    = NonEmptyList.Singleton(1);
            FSharpList <int>   list = x.ToFSharpList();

            Assert.AreEqual(FSharpList.Create(1), list);
        }
예제 #6
0
 public static AuthorizationPolicyBuilder RequireScope(
     this AuthorizationPolicyBuilder authorizationPolicyBuilder,
     string requiredScope)
 {
     authorizationPolicyBuilder.RequireScopes(NonEmptyList.Create(requiredScope));
     return(authorizationPolicyBuilder);
 }
예제 #7
0
        public void HeadTail()
        {
            NonEmptyList <int> x = NonEmptyList.Singleton(1);

            Assert.AreEqual(1, x.Head);
            Assert.AreEqual(FSharpList <int> .Empty, x.Tail);
        }
예제 #8
0
 public static AuthorizationPolicyBuilder RequireScopes(
     this AuthorizationPolicyBuilder authorizationPolicyBuilder,
     NonEmptyList <string> requiredScopes)
 {
     authorizationPolicyBuilder.AddRequirements(new ScopeAuthorizationRequirement(requiredScopes));
     return(authorizationPolicyBuilder);
 }
        private void LoadDishesFromXml(string filename)
        {
            XmlDocument xml = new XmlDocument();

            xml.Load(filename);
            XmlNodeList dishesNodes = xml.SelectNodes("/dishes/dish");

            foreach (XmlElement dishNode in dishesNodes)
            {
                string name          = dishNode.Attributes["name"].Value;
                double carbohydrates = XmlConvert.ToDouble(dishNode.Attributes["carbohydrates"].Value);
                double calories      = XmlConvert.ToDouble(dishNode.Attributes["calories"].Value);
                double proteins      = XmlConvert.ToDouble(dishNode.Attributes["proteins"].Value);
                double fats          = XmlConvert.ToDouble(dishNode.Attributes["fats"].Value);

                XmlNodeList       ingredientNodes = dishNode.SelectNodes("/ingredient");
                List <Ingredient> ingredients     = new List <Ingredient>();
                foreach (XmlElement ingredientNode in ingredientNodes)
                {
                    Ingredient ingredient = ingredientNode.InnerText;
                    ingredients.Add(name);
                }
                NonEmptyList <Ingredient> nonEmpty = new NonEmptyList <Ingredient>(ingredients[0]);
                for (int i = 1; i < ingredients.Count; i++)
                {
                    nonEmpty.Add(ingredients[i]);
                }

                _dishes.Add(new Dish(name, calories, proteins, fats, carbohydrates, nonEmpty));
            }
        }
        public WeeklyMenu NewMenu(UserProfile userProfile)
        {
            WeeklyMenu result = new WeeklyMenu(this);
            Random     rnd    = new Random();

            foreach (DailyMenu dm in result)
            {
                int num = rnd.Next(1, 4);
                for (int i = 0; i < num; i++)
                {
                    List <Serving> servings = new List <Serving>();
                    int            num2     = rnd.Next(1, 3);
                    for (int j = 0; j < num2; j++)
                    {
                        servings.Add(new Serving(KitchenManager.Dishes.RandomElement(), new Random().Next(120)));
                    }
                    NonEmptyList <Serving> nonempty = new NonEmptyList <Serving>(servings[0]);
                    for (int j = 1; j < servings.Count; j++)
                    {
                        nonempty.Add(servings[j]);
                    }
                    dm.Meals.Add(new Meal("pasto " + i, new DateTime(2000, 1, 1, rnd.Next(24), rnd.Next(60), 0), nonempty));
                }
            }
            return(result);
        }
예제 #11
0
 public void CannotAssignEmptyListTest()
 {
     Assert.DoesNotThrow(() => _ = new NonEmptyList(new List <int> {
         0
     }));
     Assert.Throws <ArgumentException>(() => _ = new NonEmptyList(new List <int>()));
 }
예제 #12
0
        public void Concat_NonEmptyList_extension()
        {
            var x = NonEmptyList.Singleton(1);

            x = x.Concat(NonEmptyList.Singleton(2));
            CollectionAssert.AreEqual(new[] { 1, 2 }, x);
        }
예제 #13
0
        public void Create()
        {
            NonEmptyList <int> x = NonEmptyList.Create(1, FSharpList.Create(2, 3, 4));
            NonEmptyList <int> y = NonEmptyList.Create(1, 2, 3, 4);

            Assert.AreEqual(4, x.Length);
            Assert.AreEqual(x, y);
        }
        public async Task <IReadOnlyCollection <CategoryId> > GetNonExistentIdsAsync(NonEmptyList <CategoryId> categoryIds)
        {
            await Task.CompletedTask;

            return
                (categoryIds
                 .Except(_categories.Select(c => c.Id))
                 .ToList());
        }
        public async Task <IReadOnlyCollection <Product> > GetAsync(NonEmptyList <ProductId> productIds)
        {
            await Task.CompletedTask;

            return
                (_products
                 .Where(p => productIds.Contains(p.Id))
                 .ToList());
        }
        public void MultiScopeRegistrationTest()
        {
            const string openIdScope  = "openid";
            const string profileScope = "profile";
            var          requirement  = new ScopeAuthorizationRequirement(NonEmptyList.Create(openIdScope, profileScope));

            Assert.Equal(openIdScope, requirement.RequiredScopes.ToList().First());
            Assert.Equal(profileScope, requirement.RequiredScopes.ToList()[1]);
        }
예제 #17
0
        public void Select()
        {
            var list = NonEmptyList.Create(1, FSharpList.Create(2, 3, 4));

            list = list.Select(x => x + 2);
            CollectionAssert.AreEqual(new[] { 3, 4, 5, 6 }, list);
            list = from x in list select x + 1;
            CollectionAssert.AreEqual(new[] { 4, 5, 6, 7 }, list);
        }
예제 #18
0
        public ScopeAuthorizationRequirement(NonEmptyList <string> requiredScopes)
        {
            if (requiredScopes == null)
            {
                throw new ArgumentNullException(nameof(requiredScopes),
                                                $"{nameof(requiredScopes)} must be of type NonEmptyList<string>.");
            }

            RequiredScopes = requiredScopes.AsEnumerable();
        }
        public void SingleScopeRequirementWithMultiScopeTest()
        {
            const string openIdScope = "openid";
            var          requirement = new ScopeAuthorizationRequirement(NonEmptyList.Create(openIdScope));
            var          context     = new AuthorizationHandlerContext(new[] { requirement },
                                                                       new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("scope", "openid profile") })), null);

            requirement.HandleAsync(context);
            Assert.True(context.HasSucceeded);
        }
예제 #20
0
        public async Task <DiscountStrategy> GetWeightDiscountStrategyAsync(NonEmptyList <ProductId> productIds)
        {
            var weightByProductId = await _productWeightRepository.GetWeightByProductIdAsync(productIds);

            return
                (new DiscountStrategy(
                     new DiscountStrategyId(1),
                     new DiscountName("Price discount"),
                     new DiscountDescription("-5% si poids > 50Kg"),
                     new ProductGlobalWeightDiscountStrategy(weightByProductId)));
        }
예제 #21
0
        public async Task <DiscountStrategy> GetPriceDiscountStrategyAsync(NonEmptyList <ProductId> productIds)
        {
            var products = await _productPriceRepository.GetAsync(productIds);

            return
                (new DiscountStrategy(
                     new DiscountStrategyId(1),
                     new DiscountName("Price discount"),
                     new DiscountDescription("-25€ si montant > 500€"),
                     new ProductGlobalPriceDiscountStrategy(
                         products.ToDictionary(p => p.Id, p => p.Price))));
        }
        public void Ctor_Creates_An_Enumerable_Instance()
        {
            // ARRANGE
            var items = new[] { "foo", "bar" };

            // ACT
            var result = new NonEmptyList <string>(items);

            // ASSERT
            Assert.IsNotNull(result);
            CollectionAssert.AreEqual(new[] { "foo", "bar" }, result);
        }
        public void Count_Returns_Number_Of_Items_In_List()
        {
            // ARRANGE
            var items  = new[] { "foo", "bar" };
            var target = new NonEmptyList <string>(items);

            // ACT
            var result = target.Count;

            // ASSERT
            Assert.AreEqual(2, result);
        }
예제 #24
0
        public void MultiScopeRegistrationTest()
        {
            const string openIdScope  = "openid";
            const string profileScope = "profile";
            var          builder      = new AuthorizationPolicyBuilder();

            builder.RequireScopes(NonEmptyList.Create(openIdScope, profileScope));
            var requiredScopes = ((ScopeAuthorizationRequirement)builder.Requirements.ToList()[0]).RequiredScopes.ToList();

            Assert.Contains(openIdScope, requiredScopes);
            Assert.Contains(profileScope, requiredScopes);
        }
예제 #25
0
 public UncreatedProduct(
     ProductName name,
     ProductDescription description,
     Dimension dimension,
     Weight weight,
     NonEmptyList <CategoryId> categoryIds)
 {
     Name        = name;
     Description = description;
     Dimension   = dimension;
     Weight      = weight;
     CategoryIds = categoryIds;
 }
예제 #26
0
        public void Test2_either_LINQ()
        {
            var userID = FSharpOption.ParseInt(req_userID)
                         .ToFSharpChoice(NonEmptyList.Singleton("Invalid User ID"));
            var id = FSharpOption.ParseInt(req_otherID)
                     .ToFSharpChoice(NonEmptyList.Singleton("Invalid ID"));

            var result =
                from a in userID
                join b in id on 1 equals 1
                select doSomething(a, b);

            result.Match(Console.WriteLine, setErrors);
        }
        public void Indexer_Can_Be_Used_To_Access_List_Items()
        {
            // ARRANGE
            var items  = new[] { "foo", "bar" };
            var target = new NonEmptyList <string>(items);

            // ACT
            var firstItem  = target[0];
            var secondItem = target[1];

            // ASSERT
            Assert.AreEqual("foo", firstItem);
            Assert.AreEqual("bar", secondItem);
        }
예제 #28
0
 public Product(
     ProductId id,
     ProductName name,
     ProductDescription description,
     Dimension dimension,
     Weight weight,
     NonEmptyList <CategoryId> categoryIds)
     : base(id)
 {
     Name        = name;
     Description = description;
     Dimension   = dimension;
     Weight      = weight;
     CategoryIds = categoryIds;
 }
예제 #29
0
        public async Task ChangeCategoriesAsync(ProductId productId, NonEmptyList <CategoryId> categoryIds)
        {
            var product = await SafeGetProductAsync(productId);

            var nonexistentCategoryIds = await _categoriesRepository.GetNonExistentIdsAsync(categoryIds);

            if (nonexistentCategoryIds.Any())
            {
                throw new NonExistentCategoriesException(categoryIds);
            }

            product.CategoryIds = categoryIds;

            await _saveProduct.SaveAsync(product);
        }
예제 #30
0
        public void ThrowsWhenRemovingLastElement()
        {
            var list = new NonEmptyList <int>(1)
            {
                2, 3, 4
            };

            Assert.AreEqual(4, list.Count);
            _ = list.Remove(2);
            Assert.IsFalse(list.Contains(2));
            list.RemoveAt(0);
            Assert.AreEqual(3, list.Head);
            list.RemoveAt(1);
            Assert.AreEqual(0, list.IndexOf(3));

            _ = Assert.ThrowsException <NotSupportedException>(() => list.Clear());
            _ = Assert.ThrowsException <NotSupportedException>(() => list.RemoveAt(0));
        }