Пример #1
0
        public void When()
        {
            Expression <Func <Customer, bool> >             expression  = x => x.Age.HasValue;
            IEntityValidationRuleBuilder <Customer, string> ruleBuilder = Substitute.For <IEntityValidationRuleBuilder <Customer, string> >();

            ruleBuilder.WhenForAnyArgs(x => x.SetSpecification(null)).Do(x => Assert.AreEqual(expression, x.Arg <ISpecification <Customer> >().Predicate));

            EntityPropertyValidationRuleExtensions.When(ruleBuilder, expression);

            ruleBuilder.ReceivedWithAnyArgs(1).SetSpecification(null);
        }
Пример #2
0
        public void Unless()
        {
            Expression <Func <Customer, bool> >             expression  = x => x.Age.HasValue;
            IEntityValidationRuleBuilder <Customer, string> ruleBuilder = Substitute.For <IEntityValidationRuleBuilder <Customer, string> >();

            ruleBuilder.WhenForAnyArgs(x => x.SetSpecification(null)).Do(
                x =>
            {
                Expression <Func <Customer, bool> > expected = Expression.Lambda <Func <Customer, bool> >(Expression.Not(Expression.Invoke(expression, expression.Parameters)), expression.Parameters);
                Func <Customer, bool> expectedFunc           = expected.Compile();
                Func <Customer, bool> parameterFunc          = x.Arg <ISpecification <Customer> >().Predicate.Compile();
                Customer customer = new Customer {
                    Age = 35
                };
                Assert.AreEqual(expectedFunc(customer), parameterFunc(customer));
                Assert.AreEqual(parameterFunc(customer), !expression.Compile()(customer));
            });

            ruleBuilder.Unless(expression);

            ruleBuilder.ReceivedWithAnyArgs(1).SetSpecification(null);
        }
Пример #3
0
        /// <summary>
        /// Sets a predicate to the rule builder that specifies when the validator should not run.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Rule builder.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// ruleBuilder
        /// or
        /// predicate
        /// </exception>
        public static IEntityValidationRuleBuilder <TEntity, TProperty> Unless <TEntity, TProperty>(this IEntityValidationRuleBuilder <TEntity, TProperty> ruleBuilder, Expression <Func <TEntity, bool> > predicate)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }

            UnaryExpression newExpression = Expression.Not(Expression.Invoke(predicate, predicate.Parameters));

            return(ruleBuilder.When(Expression.Lambda <Func <TEntity, bool> >(newExpression, predicate.Parameters)));
        }
Пример #4
0
        /// <summary>
        /// Sets a predicate to the rule builder that specifies when the validator should run.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Rule builder.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// ruleBuilder
        /// or
        /// predicate
        /// </exception>
        public static IEntityValidationRuleBuilder <TEntity, TProperty> When <TEntity, TProperty>(this IEntityValidationRuleBuilder <TEntity, TProperty> ruleBuilder, Expression <Func <TEntity, bool> > predicate)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }

            return(ruleBuilder.SetSpecification(new Specification <TEntity>(predicate)));
        }