예제 #1
0
 public void HasMessage()
 {
     var notification = new Notification();
     Assert.IsFalse(notification.HasMessage("field", "message"), "message should not exist yet");
     notification.Register("field", "message");
     Assert.IsTrue(notification.HasMessage("field", "message"), "message should exist");
 }
예제 #2
0
 public void GetJoinedMessage()
 {
     var notification = new Notification();
     notification.Register("field1", "message1");
     notification.Register("field1", "message2");
     Assert.AreEqual("message1, message2", notification.GetMessageFor("field1", ", "));
 }
예제 #3
0
 public static Notification GetNotification(ValidationAttribute attribute, object caller, string propertyName)
 {
     var notification = new Notification();
     attribute.Property = caller.GetType().GetProperty(propertyName);
     attribute.Validate(caller, notification);
     return notification;
 }
예제 #4
0
 public void FieldsWithErrors()
 {
     var notification = new Notification();
     notification.Register("field1", "message1");
     notification.Register("field2", "message2");
     Assert.AreEqual(new[] { "field1", "field2" }, notification.GetFieldsWithMessages());
 }
예제 #5
0
        public void GetMessages()
        {
            var notification = new Notification();
            notification.Register("field1", "message1");
            notification.Register("field2", "message2");
            notification.Register("field1", "message3");

            var messages = notification.GetMessagesFor("field1");
            Assert.AreEqual(new[] { "message1", "message3" }, messages);
        }
예제 #6
0
        public static Notification ValidateProperty(object obj, string propertyName)
        {
            var notification = new Notification();
            var property = obj.GetType().GetProperty(propertyName, Bindings);

            if (property == null)
                throw new ArgumentException("Invalid property name:" + propertyName);

            ValidateProperty(obj, property, notification);
            return notification;
        }
예제 #7
0
        public static Notification Validate(object obj)
        {
            var notification = new Notification();

            var properties = obj.GetType().GetProperties(Bindings);
            foreach (PropertyInfo property in properties)
            {
                ValidateProperty(obj, property, notification);
            }

            return notification;
        }
예제 #8
0
 public void IsNotValidWhenNotEmpty()
 {
     var notification = new Notification();
     notification.Register("field", "message");
     Assert.IsFalse(notification.IsValid);
 }
예제 #9
0
 public void EmptyGetMessages()
 {
     var notification = new Notification();
     Assert.AreEqual(0, notification.GetMessagesFor("field").Length);
 }
예제 #10
0
        private static void ValidateProperty(object obj, PropertyInfo property, Notification notification)
        {
            if (!DependencySatisfied(obj, property))
                return;

            foreach (ValidationAttribute validationRule in GetValidationAttributes(obj, property))
            {
                validationRule.Property = property;
                validationRule.Validate(obj, notification);
            }
        }
예제 #11
0
 public void Validate(object target, Notification notification)
 {
     var rawValue = Property.GetValue(target, null);
     if (!IsValid(rawValue))
         notification.Register(Property.Name, Message);
 }