public void CompareEqualTest()
        {
            Date           date1      = new Date("2019-10-11");
            Date           date2      = new Date("2019-10-11");
            DateComparator comparator = new DateComparator();

            Assert.AreEqual(comparator.Compare(date1, date2), 0);
        }
Exemple #2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo propertyInfo = validationContext.ObjectType.GetProperty(_comparePropertyName);

            if (propertyInfo == null)
            {
                return(null);
            }

            if (value == null)
            {
                return(null);
            }

            if (!propertyInfo.PropertyType.Equals(value.GetType()))
            {
                return(null);
            }

            bool result = false;

            switch (_compareType)
            {
            case CompareType.NUMBER:
                result = new NumberComparator().Compare((float)value, (float)propertyInfo.GetValue(validationContext.ObjectInstance), _compareOperator);
                break;

            case CompareType.TIME:
                result = new TimeComparator().Compare((string)value, (string)propertyInfo.GetValue(validationContext.ObjectInstance), _compareOperator);
                break;

            case CompareType.DATE:
                result = new DateComparator().Compare((DateTime)value, (DateTime)propertyInfo.GetValue(validationContext.ObjectInstance), _compareOperator);
                break;

            case CompareType.STRING:
                result = new StringComparator().Compare((string)value, (string)propertyInfo.GetValue(validationContext.ObjectInstance), _compareOperator);
                break;
            }

            if (!result)
            {
                return(new ValidationResult(string.Format(ErrorMessageString, validationContext.DisplayName, _comparePropertyName)));
            }

            return(ValidationResult.Success);
        }