public void GetValidationResult_BothNull_IsValid()
        {
            ComparisonEntity entity = CreateComparisonEntity();
            ValidationContext validationContext = new ValidationContext(entity)
            {
                MemberName = "StringType"
            };
            CompareValuesAttribute attribute = new CompareValuesAttribute(
                "DefaultToNull", ComparisonCriteria.LessThan);

            // Act
            ValidationResult result = attribute.GetValidationResult(entity.StringType, validationContext);

            Assert.That(result, Is.EqualTo(ValidationResult.Success));
        }
        public void CompareValues_UncomparableProperties_IsInvalid()
        {
            ComparisonEntity entity = CreateComparisonEntity();
            ValidationContext validationContext = new ValidationContext(entity)
            {
                MemberName = "Uncomparable"
            };
            CompareValuesAttribute attribute = new CompareValuesAttribute(
                "OtherUncomparable", ComparisonCriteria.LessThan);

            ValidationResult result = attribute.GetValidationResult(entity.Uncomparable, validationContext);

            Assert.That(result.ErrorMessage, Is.StringContaining("IComparable"));
        }
        public void GetValidationResult_OneNullValueWithNotEqualAsCriteria_IsValid()
        {
            ComparisonEntity entity = CreateComparisonEntity(min: 1);
            ValidationContext validationContext = new ValidationContext(entity) { MemberName = "Minimum" };
            CompareValuesAttribute attribute = new CompareValuesAttribute(
                "Maximum", ComparisonCriteria.NotEqualTo);

            // Act
            ValidationResult result = attribute.GetValidationResult(null, validationContext);

            Assert.That(result, Is.EqualTo(ValidationResult.Success));
        }
        public void GetValidationResult_InvalidValues_UsesDescriptionOfComparisonCriteria()
        {
            ComparisonEntity entity = CreateComparisonEntity(1, 0);
            ValidationContext validationContext = new ValidationContext(entity)
            {
                MemberName = "Minimum"
            };
            CompareValuesAttribute attribute = new CompareValuesAttribute(
                "Maximum", ComparisonCriteria.LessThan);

            // Act
            ValidationResult result = attribute.GetValidationResult(entity.Minimum, validationContext);

            Assert.That(result.ErrorMessage, Is.StringContaining("<"));
        }
        public void GetValidationResult_InvalidValuesWithoutDisplayName_UsesNameOfOtherProperty()
        {
            ComparisonEntity entity = CreateComparisonEntity();
            entity.DefaultToNull = "";
            entity.StringType = "";
            ValidationContext validationContext = new ValidationContext(entity)
            {
                MemberName = "StringType"
            };
            CompareValuesAttribute attribute = new CompareValuesAttribute(
                "DefaultToNull", ComparisonCriteria.LessThan);

            // Act
            ValidationResult result = attribute.GetValidationResult(entity.StringType, validationContext);

            Assert.That(result.ErrorMessage, Is.StringContaining("DefaultToNull"));
        }
        public void GetValidationResult_InvalidValues_UsesDisplayAttributeNameValueFromOtherProperty()
        {
            ComparisonEntity entity = CreateComparisonEntity(1, 0);
            ValidationContext validationContext = new ValidationContext(entity)
            {
                MemberName = "MaxDisplay"
            };
            CompareValuesAttribute attribute = new CompareValuesAttribute(
                "Minimum", ComparisonCriteria.GreaterThan);

            // Act
            ValidationResult result = attribute.GetValidationResult(entity.Minimum, validationContext);

            Assert.That(result.ErrorMessage, Is.StringContaining("MinDisplayName"));
        }
        public void CompareValues_MinimumComparedToMaximumWithInvalidValues_IsInvalid(
            ComparisonCriteria criteria, int minimum, int maximum)
        {
            ComparisonEntity entity = CreateComparisonEntity(minimum, maximum);
            ValidationContext validationContext = new ValidationContext(entity) { MemberName = "Minimum" };
            CompareValuesAttribute attribute = new CompareValuesAttribute("Maximum", criteria);

            // Act
            ValidationResult result = attribute.GetValidationResult(entity.Minimum, validationContext);

            Assert.That(result, Is.Not.EqualTo(ValidationResult.Success));
        }