コード例 #1
0
        public void Should_AddRule_SingleRule()
        {
            var memberValidator = new MemberValidator();

            var rule = new ValidRule<int>(c => true);
            memberValidator.AddRule(rule);

            Assert.Single(memberValidator.Rules);
            Assert.Same(rule, memberValidator.Rules.Single());
        }
コード例 #2
0
            public void Should_ThrowException_When_NullContext()
            {
                var rule = new ValidRule <object>(c => true, new Error("message"));

                Assert.Throws <ArgumentNullException>(() =>
                {
                    rule.TryGetErrors(new object(),
                                      null,
                                      ValidationStrategy.Complete,
                                      out _);
                });
            }
コード例 #3
0
            public void Should_AddError_When_Valid_And_Force()
            {
                Predicate <object> isValid = m => true;

                var error = new Error("message");

                var rule = new ValidRule <object>(isValid, error);

                var getErrorsResult = rule.TryGetErrors(new object(),
                                                        new ExecutionContextStub(),
                                                        ValidationStrategy.Force,
                                                        out var errorsCollection);

                Assert.True(getErrorsResult);
                Assert.Same(error, errorsCollection.Errors.Single());
            }
コード例 #4
0
        public void Should_AddRule_MultipleRules()
        {
            var memberValidator = new MemberValidator();

            var rule1 = new ValidRule<int>(c => true);
            var rule2 = new AsRelativeRule<object>(c => true);
            var rule3 = new AsModelRule<object>();

            memberValidator.AddRule(rule1);
            memberValidator.AddRule(rule2);
            memberValidator.AddRule(rule3);

            Assert.Equal(3, memberValidator.Rules.Count);
            Assert.Same(rule1, memberValidator.Rules.ElementAt(0));
            Assert.Same(rule2, memberValidator.Rules.ElementAt(1));
            Assert.Same(rule3, memberValidator.Rules.ElementAt(2));
        }
コード例 #5
0
            public void Should_NotAddError_When_Valid(ValidationStrategy validationStrategy)
            {
                Predicate <object> isValid = m => true;

                var error = new Error("message");

                var rule = new ValidRule <object>(isValid, error);

                var getErrorsResult = rule.TryGetErrors(new object(),
                                                        new ExecutionContextStub(),
                                                        validationStrategy,
                                                        out var errorsCollection);

                Assert.False(getErrorsResult);
                Assert.Same(ErrorsCollection.Empty, errorsCollection);
                Assert.True(errorsCollection.IsEmpty);
            }
コード例 #6
0
            public void Should_AddError_When_Invalid(ValidationStrategy validationStrategy)
            {
                Predicate <object> isValid = m => false;

                var error = new Error("message");

                var rule = new ValidRule <object>(isValid, error);

                rule.RuleSingleError = new Error("ruleSingleError");

                var getErrorsResult = rule.TryGetErrors(new object(),
                                                        new ExecutionContextStub(),
                                                        validationStrategy,
                                                        out var errorsCollection);

                Assert.True(getErrorsResult);
                Assert.Equal("ruleSingleError", errorsCollection.Errors.Single().Message);
            }
コード例 #7
0
            public void Should_AddDefaultError_When_Invalid_And_NoError(ValidationStrategy validationStrategy)
            {
                Predicate <object> isValid = m => false;

                var error = new Error("default error {arg}", new[] { Arg.Text("key", "value") });

                var rule = new ValidRule <object>(isValid);

                var getErrorsResult = rule.TryGetErrors(new object(),
                                                        new ExecutionContextStub
                {
                    DefaultError = error
                },
                                                        validationStrategy,
                                                        out var errorsCollection);

                Assert.True(getErrorsResult);

                Assert.Same(error, errorsCollection.Errors.Single());
            }
コード例 #8
0
            public void Should_NotExecutePredicate_When_Force()
            {
                var executed = 0;

                Predicate <object> isValid = m =>
                {
                    executed++;

                    return(true);
                };

                var rule = new ValidRule <object>(isValid);

                rule.TryGetErrors(new object(),
                                  new ExecutionContextStub(),
                                  ValidationStrategy.Force,
                                  out _);

                Assert.Equal(0, executed);
            }
コード例 #9
0
            public void Should_NotExecutePredicate_When_NullMember(ValidationStrategy validationStrategy)
            {
                var executed = 0;

                Predicate <object> isValid = m =>
                {
                    executed++;

                    return(true);
                };

                var rule = new ValidRule <object>(isValid);

                rule.TryGetErrors(null,
                                  new ExecutionContextStub(),
                                  validationStrategy,
                                  out _);

                Assert.Equal(0, executed);
            }
コード例 #10
0
ファイル: RulesLoadTest.cs プロジェクト: zhjrate/NRules
        public void Add_RuleInstanceInRuleSet_AddedToRepository()
        {
            //Arrange
            var rule = new ValidRule();

            var factory        = new RuleDefinitionFactory();
            var ruleDefinition = factory.Create(rule);

            var ruleSet = new RuleSet("MyRuleSet");

            ruleSet.Add(ruleDefinition);

            RuleRepository target = CreateTarget();

            //Act
            target.Add(ruleSet);

            //Assert
            Assert.Single(ruleSet.Rules);
            Assert.Equal(typeof(ValidRule).FullName, ruleSet.Rules.First().Name);
        }
コード例 #11
0
            public void Should_PassEqualValueToPredicate(ValidationStrategy validationStrategy)
            {
                var executed = false;
                var member   = 123.123;

                Predicate <object> isValid = m =>
                {
                    executed = true;
                    Assert.Equal(member, m);

                    return(true);
                };

                var rule = new ValidRule <object>(isValid);

                rule.TryGetErrors(member,
                                  new ExecutionContextStub(),
                                  validationStrategy,
                                  out _);

                Assert.True(executed);
            }
コード例 #12
0
ファイル: FormGroup.cs プロジェクト: fenglinz/Sparrow
        /// <summary>
        /// 追加文本框。
        /// </summary>
        /// <param name="fullName">属性名称</param>
        /// <param name="labelCols">标签宽度</param>
        /// <param name="formCols">表单宽度</param>
        /// <param name="rule">验证规则</param>
        /// <param name="callback">表单设置回调</param>
        /// <returns>表单组</returns>
        public FormGroup AppendTextBox(string fullName,
            uint labelCols, uint formCols, ValidRule rule, Action<TextBoxControl> callback = null)
        {
            var metadata = this._html.Resolve(fullName);
            var control = new TextBoxControl(this._screen, metadata).Valid(rule);

            control.Layout(labelCols, formCols);

            callback?.Invoke(control);

            this._formControls.Add(control.Render());

            return this;
        }
コード例 #13
0
ファイル: FormGroup.cs プロジェクト: fenglinz/Sparrow
 /// <summary>
 /// 生成只包含密码框的表单组。
 /// </summary>
 /// <param name="fullName">元素名称</param>
 /// <param name="labelCols">标签宽度</param>
 /// <param name="formCols">表单宽度</param>
 /// <param name="rule">验证规则</param>
 /// <param name="callback">表单设置回调</param>
 /// <returns>Html片段</returns>
 public IHtmlString Password(string fullName,
     uint labelCols, uint formCols, ValidRule rule, Action<PasswordControl> callback = null)
 {
     return this.AppendPassword(fullName, labelCols, formCols, rule, callback).Render();
 }
コード例 #14
0
ファイル: FormGroup.cs プロジェクト: fenglinz/Sparrow
 /// <summary>
 /// 生成只包含文本框的表单组。
 /// </summary>
 /// <param name="fullName">属性名称</param>
 /// <param name="labelCols">标签宽度</param>
 /// <param name="formCols">表单宽度</param>
 /// <param name="rule">验证规则</param>
 /// <param name="callback">表单设置回调</param>
 /// <returns>Html片段</returns>
 public IHtmlString TextBox(string fullName,
     uint labelCols, uint formCols, ValidRule rule, Action<TextBoxControl> callback = null)
 {
     return this.AppendTextBox(fullName, labelCols, formCols, rule, callback).Render();
 }
コード例 #15
0
ファイル: FormGroup.cs プロジェクト: fenglinz/Sparrow
        /// <summary>
        /// 追加下拉列表框。
        /// </summary>
        /// <param name="fullName">属性名称</param>
        /// <param name="labelCols">标签宽度</param>
        /// <param name="formCols">表单宽度</param>
        /// <param name="key">字典键</param>
        /// <param name="rule">验证规则</param>
        /// <param name="callback">表单设置回调</param>
        /// <returns>表单组</returns>
        public FormGroup AppendMultipleList(string fullName,
            uint labelCols, uint formCols, string key, ValidRule rule, Action<MultipleListControl> callback = null)
        {
            var metadata = this._html.Resolve(fullName);
            var control = new MultipleListControl(this._screen, metadata);

            control.Layout(labelCols, formCols).Key(key).Valid(rule);

            callback?.Invoke(control);

            this._formControls.Add(control.Render());

            return this;
        }
コード例 #16
0
ファイル: FormBase.cs プロジェクト: fenglinz/Sparrow
        /// <summary>
        /// 初始化验证规则。
        /// </summary>
        private void InitializeRule()
        {
            if (this.Rule == ValidRule.Default)
            {
                var type = this._metadata.Type;
                var isRequired = this._metadata.IsRequired;

                if (type == typeof(string))
                {
                    this.Rule = isRequired ? ValidRule.NotNull : ValidRule.Default;
                }
                else if (type == typeof(int))
                {
                    this.Rule = ValidRule.Int;
                }
                else if (type == typeof(int?))
                {
                    this.Rule = ValidRule.IntOrNull;
                }
                else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
                {
                    this.Rule = ValidRule.Double;
                }
                else if (type == typeof(float?) || type == typeof(double?) || type == typeof(decimal?))
                {
                    this.Rule = ValidRule.DoubleOrNull;
                }
                else if (type == typeof(DateTime))
                {
                    this.Rule = ValidRule.Date;
                }
                else if (type == typeof(DateTime?))
                {
                    this.Rule = ValidRule.DateOrNull;
                }
                else if (isRequired)
                {
                    this.Rule = ValidRule.NotNull;
                }
            }
        }