public void ShouldReturnFalseForDoubleValue()
        {
            testValueDouble = 10.1F;
            Validator lengthValidator = new LengthValidator(testValueDouble, controlLength);

            Assert.IsFalse(lengthValidator.Validate());
        }
        public void ShouldReturnFalseForIntValue()
        {
            testValueInt = 11;
            Validator lengthValidator = new LengthValidator(testValueInt, controlLength);

            Assert.IsFalse(lengthValidator.Validate());
        }
 public void When_the_text_is_exactly_the_size_of_the_upper_bound_then_the_validator_should_pass()
 {
     string text = "Test";
     var validator = new LengthValidator(1, 4);
     var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => text));
     result.IsValid().ShouldBeTrue();
 }
        public void Should_be_invalid_when_length_is_lower_than_min_value_using_instance_property()
        {
            var validator = new LengthValidator<Person>(p => p.Name.Length, p => p.Age);

            var person = new Person { Name = "Bob", Age = 8 };
            Assert.That(validator.Validate(person, "AB"), Is.False);
        }
        public void ShouldReturnFalseForDecimalValue()
        {
            testValueDecimal = 1.0M;
            Validator lengthValidator = new LengthValidator(testValueDecimal, controlLength);

            Assert.IsFalse(lengthValidator.Validate());
        }
        public void ShouldReturnFalseForObject()
        {
            object    testObject      = new object();
            Validator lengthValidator = new LengthValidator(testObject, 5);

            Assert.IsFalse(lengthValidator.Validate());
        }
        public void ShouldReturnFalseForValidatableObject()
        {
            LengthValidatableObjectFalse testValidatableObject = new LengthValidatableObjectFalse();
            Validator lengthValidator = new LengthValidator(testValidatableObject, 5);

            Assert.IsFalse(lengthValidator.Validate());
        }
        public void ShouldReturnTrueForObject()
        {
            LengthValidatableObjectTrue testValidatableObject = new LengthValidatableObjectTrue();
            Validator lengthValidator = new LengthValidator(testValidatableObject, 25);

            Assert.IsTrue(lengthValidator.Validate());
        }
        public void ShouldReturnFalseForStringValue()
        {
            testValueString = "Length less than 20!!!!";
            Validator lengthValidator = new LengthValidator(testValueString, controlLengthString);

            Assert.IsFalse(lengthValidator.Validate());
        }
        public void Should_be_valid_when_length_is_between_values_using_instance_property()
        {
            var validator = new LengthValidator<Person>(p => p.Name.Length, p => p.Age);

            var person = new Person {Name = "Bob", Age = 8};
            Assert.That(validator.Validate(person, "ABCDE"), Is.True);
        }
 public void When_the_text_is_between_the_range_specified_then_the_validator_should_pass()
 {
     string text = "Test";
     var validator = new LengthValidator(1, 10);
     var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => text));
     result.IsValid().ShouldBeTrue();
 }
Example #12
0
        public void Should_be_valid_when_length_is_equal_to_min_value_using_instance_property()
        {
            var validator = new LengthValidator <Person>(p => p.Name.Length, p => p.Age);

            var person = new Person {
                Name = "Bob", Age = 8
            };

            Assert.That(validator.Validate(person, "ABC"), Is.True);
        }
Example #13
0
        public void Should_be_invalid_when_length_is_higher_than_max_value_using_instance_property()
        {
            var validator = new LengthValidator <Person>(p => p.Name.Length, p => p.Age);

            var person = new Person {
                Name = "Bob", Age = 8
            };

            Assert.That(validator.Validate(person, "ABCDEFGHIJ"), Is.False);
        }
Example #14
0
        public void TestValidate_NotExistingProperty()
        {
            var presenceValidator = new PresenceValidator();
            var lengthValidator = new LengthValidator(2, 10);
            var account = new Account();
            var ageProperty = typeof(Customer).GetProperty("Age");

            string error;
            Assert.Throws<MissingMemberException>(() => presenceValidator.Validate(account, ageProperty, out error));
            Assert.Throws<MissingMemberException>(() => lengthValidator.Validate(account, ageProperty, out error));
        }
Example #15
0
        public void TestValidate_Indexer()
        {
            var presenceValidator = new PresenceValidator();
            var lengthValidator = new LengthValidator(2, 10);
            var account = new Account();

            string error;
            var indexerProperty = typeof(Account).GetProperty("Item");

            Assert.Throws<NotSupportedException>(() => presenceValidator.Validate(account, indexerProperty, out error));
            Assert.Throws<NotSupportedException>(() => lengthValidator.Validate(account, indexerProperty, out error));
        }
Example #16
0
        public void LengthValidator()
        {
            var o = new SimpleObject();
            var v = new LengthValidator(PropertyName, Description, 2, 5);

            AssertValidatorProperties(v);

            Assert.IsTrue(v.Validate(o));

            o.SimpleProperty = string.Empty;
            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = "12";
            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = "123";
            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = "12345";
            Assert.IsTrue(v.Validate(o));

            o.SimpleProperty = "1";
            Assert.IsFalse(v.Validate(o));
            o.SimpleProperty = "123456";
            Assert.IsFalse(v.Validate(o));
        }
Example #17
0
        public void TestValidate_StaticProperty()
        {
            var presenceValidator = new PresenceValidator();
            var lengthValidator = new LengthValidator(2, 10);
            var customer = new Customer();
            var ordersProperty = typeof(Customer).GetProperty("TotalOrders", BindingFlags.Static | BindingFlags.Public);
            Customer.Group = "GroupName";
            var groupProperty = typeof(Customer).GetProperty("Group", BindingFlags.Static | BindingFlags.Public);

            string error;
            Assert.Throws<NotSupportedException>(() => presenceValidator.Validate(customer, ordersProperty, out error));
            Assert.Throws<NotSupportedException>(() => lengthValidator.Validate(customer, groupProperty, out error));
        }
        public void Should_be_valid_when_length_is_equal_to_max_value()
        {
            var validator = new LengthValidator<Person>(2, 5);

            Assert.That(validator.Validate(null, "ABCDE"), Is.True);
        }
        public void ShouldReturnTrueForStringValue()
        {
            Validator lengthValidator = new LengthValidator(testValueString, controlLengthString);

            Assert.IsTrue(lengthValidator.Validate());
        }
Example #20
0
        public void Should_be_valid_when_length_is_between_values()
        {
            var validator = new LengthValidator <Person>(2, 5);

            Assert.That(validator.Validate(null, "ABC"), Is.True);
        }
Example #21
0
        public void Should_be_valid_when_length_is_equal_to_max_value()
        {
            var validator = new LengthValidator <Person>(2, 5);

            Assert.That(validator.Validate(null, "ABCDE"), Is.True);
        }
Example #22
0
        public void Should_be_invalid_when_length_is_greater_than_min_value()
        {
            var validator = new LengthValidator <Person>(2, 5);

            Assert.That(validator.Validate(null, "ABCDEF"), Is.False);
        }
Example #23
0
        public void TestValidate_NullDomainObjectArg()
        {
            var presenceValidator = new PresenceValidator();
            var lengthValidator = new LengthValidator(2, 10);
            var nameProperty = typeof(Customer).GetProperty("Name");

            string error;
            Assert.Throws<ArgumentNullException>(() => presenceValidator.Validate<Customer>(null, nameProperty, out error));
            Assert.Throws<ArgumentNullException>(() => lengthValidator.Validate<Customer>(null, nameProperty, out error));
        }
Example #24
0
        public void TestValidate_NullPropertyArg()
        {
            var presenceValidator = new PresenceValidator();
            var lengthValidator = new LengthValidator(2, 10);
            var customer = new Customer();

            string error;
            Assert.Throws<ArgumentNullException>(() => presenceValidator.Validate(customer, null, out error));
            Assert.Throws<ArgumentNullException>(() => lengthValidator.Validate(customer, null, out error));
        }
 public void When_the_text_is_smaller_than_the_range_then_the_validator_should_fail()
 {
     string text = "Test";
     var validator = new LengthValidator(5, 10);
     var result = validator.Validate(new PropertyValidatorContext(null, new object(), x => text));
     result.IsValid().ShouldBeFalse();
 }
 public void When_the_validator_fails_the_error_message_should_be_set()
 {
     var validator = new LengthValidator(1, 2);
     var result =
         validator.Validate(new PropertyValidatorContext("Forename", null, x => "Gire and gimble in the wabe"));
     result.Single().ErrorMessage.ShouldEqual("'Forename' must be between 1 and 2 characters. You entered 27 characters.");
 }
Example #27
0
        public void TestValidate_NotPublicProperty()
        {
            string error;
            var validator = new LengthValidator(4, 6);
            var customer = new Customer();
            var internalProperty = typeof(Customer).GetProperty("Gender", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.IsFalse(validator.Validate(customer, internalProperty, out error));
            Assert.AreEqual("Gender is too short (minimum is 4).", error);

            customer.Gender = "Male";
            Assert.IsTrue(validator.Validate(customer, internalProperty, out error));
            Assert.IsNull(error);
        }
Example #28
0
        public void TestValidate_String_OutOfInterval()
        {
            const string TOO_SHORT = "Number is too short (minimum is 6).";
            const string TOO_LONG = "Number is too long (maximum is 10).";
            string error;
            var accountNumberProperty = typeof(Account).GetProperty("Number");
            var validator = new LengthValidator(6, 10);

            var account = new Account();
            Assert.IsNull(account.Number);
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = String.Empty;
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = "\n\r\t ";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = " abc ";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = null;
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = "A60000";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "A6000";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_SHORT, error);

            account.Number = "12345678901";
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(TOO_LONG, error);

            var customerNameProperty = typeof(Customer).GetProperty("Name");
            validator = new LengthValidator(4, 6);
            var customer = new Customer { Name = "Joe" };
            Assert.IsFalse(validator.Validate(customer, customerNameProperty, out error));
            Assert.AreEqual("Name is too short (minimum is 4).", error);

            customer.Name = "Jillian";
            validator.TooLongMessage = "It is too long name to remember, '{{count}}' is maximum";
            Assert.IsFalse(validator.Validate(customer, customerNameProperty, out error));
            Assert.AreEqual("It is too long name to remember, '6' is maximum", error);

            customer.Name = "Bob";
            validator.TooShortMessage = "Must be {{count}} symbols.";
            Assert.IsFalse(validator.Validate(customer, customerNameProperty, out error));
            Assert.AreEqual("Must be 4 symbols.", error);
        }
        public void ShouldReturnTrueForDecimalValue()
        {
            Validator lengthValidator = new LengthValidator(testValueDecimal, controlLength);

            Assert.IsTrue(lengthValidator.Validate());
        }
Example #30
0
        public void TestValidate_NonStringType()
        {
            string error;

            // Value type property.
            var validator = new LengthValidator(4, 10);
            var valueTypeProperty = typeof(Customer).GetProperty("ActivationMode");
            var customer = new Customer { ActivationMode = ActivationMode.Client };
            Assert.Throws<NotSupportedException>(() => validator.Validate(customer, valueTypeProperty, out error));

            // Nullable value type property.
            validator = new LengthValidator(2);
            var countryProperty = typeof(Address).GetProperty("Country");
            var address = new Address { Country = Country.RussianFederation };
            Assert.Throws<NotSupportedException>(() => validator.Validate(address, countryProperty, out error));

            // Domain object ref property.
            var addressProperty = typeof(Customer).GetProperty("MailingAddress");
            Assert.Throws<NotSupportedException>(() => validator.Validate(customer, addressProperty, out error));

            // Collection property.
            var collectionProperty = typeof(Account).GetProperty("Owners");
            var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER };
            Assert.Throws<NotSupportedException>(() => validator.Validate(account, collectionProperty, out error));
        }
        public void Should_be_invalid_when_length_is_lower_than_min_value()
        {
            var validator = new LengthValidator<Person>(2, 5);

            Assert.That(validator.Validate(null, "A"), Is.False);
        }
Example #32
0
        public void TestValidate_String()
        {
            string error;
            var accountNumberProperty = typeof(Account).GetProperty("Number");

            var validator = new LengthValidator(6, 20);
            var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER };

            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "A12345";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "A0000000000";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            account.Number = "12345678901234567890";
            Assert.IsTrue(validator.Validate(account, accountNumberProperty, out error));
            Assert.IsNull(error);

            var customerNameProperty = typeof(Customer).GetProperty("Name");
            validator = new LengthValidator(4);
            var customer = new Customer { Name = "John" };
            Assert.IsTrue(validator.Validate(customer, customerNameProperty, out error));
            Assert.IsNull(error);

            customer.Name = "    ";
            Assert.IsTrue(validator.Validate(customer, customerNameProperty, out error));
            Assert.IsNull(error);

            var addressStateCodeProperty = typeof(Address).GetProperty("StateCode");
            var stateCodeValidator = new LengthValidator(2);

            var address = new Address { StateCode = "NJ" };
            Assert.IsTrue(stateCodeValidator.Validate(address, addressStateCodeProperty, out error));
            Assert.IsNull(error);

            address.StateCode = "  ";
            Assert.IsTrue(stateCodeValidator.Validate(address, addressStateCodeProperty, out error));
            Assert.IsNull(error);
        }
        public void Should_be_valid_when_length_is_between_values()
        {
            var validator = new LengthValidator<Person>(2, 5);

            Assert.That(validator.Validate(null, "ABC"), Is.True);
        }
Example #34
0
        public void TestValidate_String_WrongLength()
        {
            const string WRONG_STATE_CODE_LENGTH = "StateCode should be 2 characters.";
            string error;
            var stateCodeProperty = typeof(Address).GetProperty("StateCode");
            var address = new Address { StateCode = "NY1" };
            var stateValidator = new LengthValidator(2);

            Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error));
            Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error);

            address.StateCode = null;
            Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error));
            Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error);

            address.StateCode = String.Empty;
            Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error));
            Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error);

            address.StateCode = "   ";
            Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error));
            Assert.AreEqual(WRONG_STATE_CODE_LENGTH, error);

            address.StateCode = "NY";
            Assert.IsTrue(stateValidator.Validate(address, stateCodeProperty, out error));
            Assert.IsNull(error);

            stateValidator.WrongLengthMessage = "Only {{count}} symbols length allowed";
            address.StateCode = "N";
            Assert.IsFalse(stateValidator.Validate(address, stateCodeProperty, out error));
            Assert.AreEqual("Only 2 symbols length allowed", error);
        }
Example #35
0
        /// <summary>
        /// The method validates whether a supplied object has a length not greater than required.
        /// </summary>
        /// <param name="objectToValidate">An object to be valdiated.</param>
        /// <param name="controlLength">A maximum valid length value.</param>
        /// <returns>True - if object is valid, false - if object is invalid.</returns>
        public static bool IsWithinLength(object objectToValidate, int controlLength)
        {
            Validator validator = new LengthValidator(objectToValidate, controlLength);

            return(validator.Validate());
        }