Exemplo n.º 1
0
        public virtual void testMaxValidator()
        {
            var validator = new MaxValidator();

            Assert.True(validator.Validate(null, null));

            Assert.True(validator.Validate(3, new TestValidatorContext("4")));
            Assert.IsFalse(validator.Validate(4, new TestValidatorContext("3")));

            try
            {
                validator.Validate(4, new TestValidatorContext("4.4"));
                Assert.Fail("exception expected");
            }
            catch (FormException e)
            {
                Assert.True(
                    e.Message.Contains("Cannot validate Integer value 4: configuration 4.4 cannot be parsed as Integer."));
            }

            Assert.IsFalse(validator.Validate(4.1d, new TestValidatorContext("4")));
            Assert.True(validator.Validate(4.1d, new TestValidatorContext("4.2")));

            Assert.IsFalse(validator.Validate(4.1f, new TestValidatorContext("4")));
            Assert.True(validator.Validate(4.1f, new TestValidatorContext("4.2")));
        }
    public void SplitCallback_Ok()
    {
        var foo       = new Foo();
        var validator = new MaxValidator()
        {
            Value        = 1,
            ErrorMessage = "最多可以选择 {0} 项"
        };
        var context = new ValidationContext(foo)
        {
            MemberName = "Name"
        };
        var results = new List <ValidationResult>();

        validator.Validate("v1, v2", context, results);
        Assert.Equal($"最多可以选择 {validator.Value} 项", results[0].ErrorMessage);

        validator.SplitCallback = value => value.Split(',', StringSplitOptions.RemoveEmptyEntries).Length;
        validator.Validate("v1, v2", context, results);
        Assert.Equal($"最多可以选择 {validator.Value} 项", results[0].ErrorMessage);
    }
    public void ErrorMessage_Ok()
    {
        var foo       = new Foo();
        var validator = new MaxValidator()
        {
            Value = 1
        };
        var context = new ValidationContext(foo);
        var results = new List <ValidationResult>();

        validator.Validate("v1, v2", context, results);
        Assert.Equal($"At most {validator.Value} items can be selected", results[0].ErrorMessage);
    }
    public void GenericType_Ok()
    {
        var foo       = new Foo();
        var validator = new MaxValidator()
        {
            Value        = 1,
            ErrorMessage = "最多可以选择 {0} 项"
        };
        var context = new ValidationContext(foo)
        {
            MemberName = "Name"
        };
        var results = new List <ValidationResult>();

        validator.Validate(new List <string> {
            "v1", "v2"
        }, context, results);
        Assert.Equal($"最多可以选择 {validator.Value} 项", results[0].ErrorMessage);

        results.Clear();
        validator.Validate("v1", context, results);
        Assert.Empty(results);
    }
    public void Null_Ok()
    {
        var foo       = new Foo();
        var validator = new MaxValidator()
        {
            Value        = 1,
            ErrorMessage = "最多可以选择 {0} 项"
        };
        var context = new ValidationContext(foo)
        {
            MemberName = "Name"
        };
        var results = new List <ValidationResult>();

        validator.Validate(null, context, results);
        Assert.Equal($"最多可以选择 {validator.Value} 项", results[0].ErrorMessage);
    }