Ejemplo n.º 1
0
 public void Validation_Cities_Fail_Set_Bad_Range()
 {
     ExceptionHelper.ExpectValidationException(delegate() {
         Zip zip = new Zip()
         {
             Code = 999990
         };
     }, "The field Code must be between 0 and 99999.", typeof(RangeAttribute), 999990);
 }
Ejemplo n.º 2
0
 public void Validation_Cities_Fail_Set_Bad_MustStartWith()
 {
     ExceptionHelper.ExpectValidationException(delegate() {
         Zip zip = new Zip()
         {
             Code = 8
         };
     }, "Code must start with the prefix 9", typeof(MustStartWithAttribute), 8);
 }
        public void Validator_Property_Invalid_Throws()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            context.MemberName = "RequiredProperty";

            ExceptionHelper.ExpectValidationException(delegate() {
                Validator.ValidateProperty(null, context);
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), typeof(RequiredAttribute), null);
        }
Ejemplo n.º 4
0
        public void Validation_Cities_Fail_Set_Long_StringLength()
        {
            // Must first set non-null since setter is NOP if incoming value == existing value
            State state = new State()
            {
                Name = "WA", FullName = "Washington"
            };

            ExceptionHelper.ExpectValidationException(delegate() {
                state.Name = "WASH";   // must be 2 letters -- [StringLength] validates that
            }, "The field Name must be a string with a maximum length of 2.", typeof(StringLengthAttribute), "WASH");
        }
Ejemplo n.º 5
0
        public void Validation_Cities_Fail_Set_Short_StringLength()
        {
            // Must first set non-null since setter is NOP if incoming value == existing value
            State state = new State()
            {
                Name = "WA", FullName = "Washington"
            };

            ExceptionHelper.ExpectValidationException(delegate() {
                state.Name = "W";   // must be 2 letters -- custom validation exception checks that
            }, "The value for Name must have exactly 2 letters", typeof(CustomValidationAttribute), "W");
        }
Ejemplo n.º 6
0
        public void Validation_Cities_Fail_Set_Bad_RegExp()
        {
            // Must first set non-null since setter is NOP if incoming value == existing value
            State state = new State()
            {
                Name = "WA", FullName = "Washington"
            };

            ExceptionHelper.ExpectValidationException(delegate() {
                state.Name = "wa";
            }, "The field Name must match the regular expression '^[A-Z]*'.", typeof(RegularExpressionAttribute), "wa");
        }
Ejemplo n.º 7
0
        public void Validation_Cities_Fail_Set_Null_Required()
        {
            // Must first set non-null since setter is NOP if incoming value == existing value
            State state = new State()
            {
                Name = "WA", FullName = "Washington"
            };

            ExceptionHelper.ExpectValidationException(delegate() {
                state.Name = null;
            }, "The Name field is required.", typeof(RequiredAttribute), null);
        }
        public void Validator_Fails_With_IValidatableObject_Errors()
        {
            ICollection <ValidationResult> injectedResults = new Collection <ValidationResult>
            {
                new ValidationResult("Error"),
                new ValidationResult("Another Error")
            };

            ValTestClass_With_IValidatableObject entity = new ValTestClass_With_IValidatableObject(injectedResults);
            ValidationContext context = new ValidationContext(entity, null, null);

            ExceptionHelper.ExpectValidationException(() => { Validator.ValidateObject(entity, context); }, "Error", null, entity);
        }
        public void Validator_Object_Invalid_Throws()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = new ValidationContext(vtc, null, null);

            //vtc.RequiredProperty = null causes validation error for [Required]
            //vtc.StringLengthProperty = null causes validation error for object;
            vtc.NullableDoubleProperty = null;

            ExceptionHelper.ExpectValidationException(delegate() {
                Validator.ValidateObject(vtc, context);
            }, String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.RequiredAttribute_ValidationError, "RequiredProperty"), typeof(RequiredAttribute), null);
        }
Ejemplo n.º 10
0
        public void Validation_Cities_Fail_Object_Null_Required()
        {
            // construct object with null Name -- this is invalid
            State state = new State()
            {
                FullName = "Washington"
            };

            ExceptionHelper.ExpectValidationException(delegate() {
                ValidationContext context = new ValidationContext(state, null, null);
                Validator.ValidateObject(state, context);
                state.Name = null;
            }, "The Name field is required.", typeof(RequiredAttribute), null);
        }
Ejemplo n.º 11
0
        public void Validation_Cities_Fail_Invalid_Value_After_Deserialization()
        {
            State state = new State();

            state.OnDeserializing(new System.Runtime.Serialization.StreamingContext());
            state.Name = "bogus";                 // would fail RegExp, StrLen, etc
            Assert.AreEqual("bogus", state.Name); // setter should still have worked
            state.OnDeserialized(new System.Runtime.Serialization.StreamingContext());

            // Now, outside of serialization, the same property should throw
            ExceptionHelper.ExpectValidationException(delegate() {
                state.Name = "WASH";   // must be 2 letters -- [StringLength] validates that
            }, "The field Name must be a string with a maximum length of 2.", typeof(StringLengthAttribute), "WASH");
        }
Ejemplo n.º 12
0
        public void Validation_Cities_Fail_Object_Cross_Field_Validation()
        {
            // First verify the validation test passes for a legal construction
            Zip zip = new Zip()
            {
                Code = 90563, CityName = "Redmond", StateName = "WA"
            };
            ValidationContext context = new ValidationContext(zip, null, null);

            Validator.ValidateObject(zip, context);

            // now make a cross-field validation error
            zip.CityName = zip.StateName;

            ExceptionHelper.ExpectValidationException(delegate() {
                Validator.ValidateObject(zip, context);
            }, "Zip codes cannot have matching city and state names", typeof(CustomValidationAttribute), zip);
        }