Ejemplo n.º 1
0
        public virtual void testMinValidator()
        {
            MinValidator validator = new MinValidator();

            assertTrue(validator.validate(null, null));

            assertTrue(validator.validate(4, new TestValidatorContext("4")));
            assertFalse(validator.validate(4, new TestValidatorContext("5")));

            try
            {
                validator.validate(4, new TestValidatorContext("4.4"));
                fail("exception expected");
            }
            catch (FormException e)
            {
                assertTrue(e.Message.contains("Cannot validate Integer value 4: configuration 4.4 cannot be parsed as Integer."));
            }

            assertFalse(validator.validate(4d, new TestValidatorContext("4.1")));
            assertTrue(validator.validate(4.1d, new TestValidatorContext("4.1")));

            assertFalse(validator.validate(4f, new TestValidatorContext("4.1")));
            assertTrue(validator.validate(4.1f, new TestValidatorContext("4.1")));
        }
Ejemplo n.º 2
0
    public void Validate_Ok()
    {
        var foo       = new Foo();
        var validator = new MinValidator()
        {
            Value = 2
        };
        var context = new ValidationContext(foo);
        var results = new List <ValidationResult>();

        validator.Validate("v1", context, results);
        Assert.Equal($"Select at least {validator.Value} items", results[0].ErrorMessage);
    }
 public void IsValid()
 {
     MinValidator v = new MinValidator();
     v.Initialize(new MinAttribute());
     Assert.IsTrue(v.IsValid(0, null));
     v.Initialize(new MinAttribute(2));
     Assert.IsTrue(v.IsValid(3, null));
     Assert.IsTrue(v.IsValid(2, null));
     Assert.IsTrue(v.IsValid((decimal) 200.0, null));
     Assert.IsTrue(v.IsValid(null, null));
     Assert.IsTrue(v.IsValid("5", null));
     Assert.IsTrue(v.IsValid((long) int.MaxValue + 1, null));
     Assert.IsFalse(v.IsValid(1, null));
     Assert.IsFalse(v.IsValid("aaa", null));
     Assert.IsFalse(v.IsValid(new object(), null));
 }
        public void Extreme()
        {
            MinValidator v = new MinValidator();
            v.Initialize(new MinAttribute(-10000));
            Assert.IsTrue(v.IsValid(-10000, null));
            Assert.IsTrue(v.IsValid(-10000L, null));
            Assert.IsTrue(v.IsValid(123UL, null));
            Assert.IsTrue(v.IsValid(123U, null));
            Assert.IsTrue(v.IsValid((ushort) 5, null));
            Assert.IsTrue(v.IsValid((short) 5, null));
            Assert.IsTrue(v.IsValid(true, null));
            Assert.IsTrue(v.IsValid((byte) 100, null));
            Assert.IsTrue(v.IsValid((sbyte) 100, null));
            Assert.IsTrue(v.IsValid(AEnum.A, null));
            Assert.IsTrue(v.IsValid(CarOptions.Spoiler | CarOptions.FogLights, null));
            Assert.IsTrue(v.IsValid('A', null));
            Assert.IsTrue(v.IsValid(-9999.99999f, null));
            Assert.IsTrue(v.IsValid(-9999.9999999999999999999999999d, null));

            Assert.IsFalse(v.IsValid(decimal.MinValue, null));
            Assert.IsFalse(v.IsValid(decimal.MinValue.ToString(), null));
            Assert.IsFalse(v.IsValid(double.MinValue, null));
            Assert.IsFalse(v.IsValid(double.MinValue + "9", null));
        }