Exemplo n.º 1
0
        /// <summary>
        /// Checks the attribute value.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="action">The action.</param>
        /// <param name="failureMessage">The failure message.</param>
        /// <returns></returns>
        public CheckElementWrapper CssClass(string name, Action <PresenceValidator> action, string failureMessage = null)
        {
            var comparator = new PresenceValidator(ElementWrapper.HasCssClass(name))
            {
                FailureMessage = failureMessage
            };

            action.Invoke(comparator);
            return(this);
        }
        public int TestValidation(string value)
        {
            // Arrange.
            var validator = new PresenceValidator <TestEntity>();
            var instance  = new TestEntity();
            var context   = DocumentValidationContext <TestEntity> .Create(instance, SaveType.Any);

            // Act.
            return(validator.Validate(value, context).Count());
        }
Exemplo n.º 3
0
		public int TestValidation(string value)
		{
			// Arrange.
			var validator = new PresenceValidator<TestEntity>();
			var instance = new TestEntity();
			var context = DocumentValidationContext<TestEntity>.Create(instance, SaveType.Any); 

			// Act.
			return validator.Validate(value, context).Count();
		}
Exemplo n.º 4
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));
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
        public void TestValidate_Collection()
        {
            var validator = new PresenceValidator();
            var account = new Account();

            var ownersProperty = typeof(Account).GetProperty("Owners");

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

            account.Owners = null;
            Assert.IsFalse(validator.Validate(account, ownersProperty, out error));
            Assert.AreEqual("Owners can't be empty.", error);
        }
Exemplo n.º 8
0
        public void TestValidate_DomainObjectRef()
        {
            const string OWNER_CANT_BE_EMPRTY = "PrimaryOwner can't be empty.";
            string error;
            var validator = new PresenceValidator();
            var customer = new Customer();
            var account = new Account();

            // Reference to other domain object.
            var primaryOwnerProperty = typeof(Account).GetProperty("PrimaryOwner");
            var addressProperty = typeof(Customer).GetProperty("MailingAddress");

            Assert.IsFalse(validator.Validate(account, primaryOwnerProperty, out error));
            Assert.AreEqual(OWNER_CANT_BE_EMPRTY, error);

            customer.MailingAddress.Country = Country.UnitedStates;
            account.Owners.Add(customer);
            Assert.IsTrue(validator.Validate(account, primaryOwnerProperty, out error));
            Assert.IsNull(error);

            account.Owners.Clear();
            Assert.IsFalse(validator.Validate(account, primaryOwnerProperty, out error));
            Assert.AreEqual(OWNER_CANT_BE_EMPRTY, error);

            Assert.IsTrue(validator.Validate(customer, addressProperty, out error));
            Assert.IsNull(error);
        }
Exemplo n.º 9
0
 public void TestDefaultErrorMessage()
 {
     var validator = new PresenceValidator();
     Assert.AreEqual("can't be empty", validator.DefaultErrorMessage);
 }
Exemplo n.º 10
0
        public void TestValidate_ValueType()
        {
            string error;
            var validator = new PresenceValidator();
            var customer = new Customer();

            var activationModeProperty = typeof(Customer).GetProperty("ActivationMode");
            var ageProperty = typeof(Customer).GetProperty("Age");
            var countryProperty = typeof(ForeignPostalAddress).GetProperty("Country");

            Assert.IsTrue(validator.Validate(customer, activationModeProperty, out error));
            Assert.IsNull(error);

            Assert.IsTrue(validator.Validate(customer, ageProperty, out error));
            Assert.IsNull(error);

            customer.ActivationMode = ActivationMode.Singleton;
            Assert.IsTrue(validator.Validate(customer, activationModeProperty, out error));
            Assert.IsNull(error);

            customer.Age = 42;
            Assert.IsTrue(validator.Validate(customer, ageProperty, out error));
            Assert.IsNull(error);

            customer.Age = 0;
            Assert.IsTrue(validator.Validate(customer, ageProperty, out error));
            Assert.IsNull(error);

            customer.Age = -300;
            Assert.IsTrue(validator.Validate(customer, ageProperty, out error));
            Assert.IsNull(error);

            Assert.IsFalse(validator.Validate(customer.MailingAddress, countryProperty, out error));
            Assert.AreEqual("Country can't be empty.", error);

            customer.MailingAddress.Country = Country.UnitedStates;
            Assert.IsTrue(validator.Validate(customer.MailingAddress, countryProperty, out error));
            Assert.IsNull(error);
        }
Exemplo n.º 11
0
        public void TestValidate_String()
        {
            const string NAME_CANT_BE_EMPTY = "Name can't be empty.";
            const string NUMBER_CANT_BE_EMPTY = "Number can't be empty.";
            string error;
            var validator = new PresenceValidator();
            var customer = new Customer();
            var account = new Account { Number = UnitTestHelper.ACCOUNT_NUMBER };

            var nameProperty = typeof(Customer).GetProperty("Name");
            var accountNumberProperty = typeof(Account).GetProperty("Number");

            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

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

            customer.Name = UnitTestHelper.BLANK_STRING;
            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

            customer.Name = String.Empty;
            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

            customer.Name = UnitTestHelper.SAMPLE_NAME;
            Assert.IsTrue(validator.Validate(customer, nameProperty, out error));
            Assert.IsNull(error);

            customer.Name = null;
            Assert.IsFalse(validator.Validate(customer, nameProperty, out error));
            Assert.AreEqual(NAME_CANT_BE_EMPTY, error);

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

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

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

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

            account.Number = null;
            Assert.IsFalse(validator.Validate(account, accountNumberProperty, out error));
            Assert.AreEqual(NUMBER_CANT_BE_EMPTY, error);
        }
Exemplo n.º 12
0
        public void TestValidate_NullableValueType()
        {
            const string FINRA_CANT_BE_EMPTY = "IsFinraMember can't be empty.";
            string error;
            var validator = new PresenceValidator();
            var customer = new Customer();
            var isFinraProperty = typeof(Customer).GetProperty("IsFinraMember");

            Assert.IsFalse(validator.Validate(customer, isFinraProperty, out error));
            Assert.AreEqual(FINRA_CANT_BE_EMPTY, error);

            customer.IsFinraMember = false;
            Assert.IsTrue(validator.Validate(customer, isFinraProperty, out error));
            Assert.IsNull(error);

            customer.IsFinraMember = null;
            Assert.IsFalse(validator.Validate(customer, isFinraProperty, out error));
            Assert.AreEqual(FINRA_CANT_BE_EMPTY, error);

            customer.IsFinraMember = true;
            Assert.IsTrue(validator.Validate(customer, isFinraProperty, out error));
            Assert.IsNull(error);
        }
Exemplo n.º 13
0
        public void TestValidate_NotPublicProperty()
        {
            var validator = new PresenceValidator();
            var customer = new Customer();
            var internalProperty = typeof(Customer).GetProperty("Gender", BindingFlags.Instance | BindingFlags.NonPublic);

            string error;
            Assert.IsFalse(validator.Validate(customer, internalProperty, out error));
            Assert.AreEqual("Gender can't be empty.", error);

            customer.Gender = "Male";
            Assert.IsTrue(validator.Validate(customer, internalProperty, out error));
            Assert.IsNull(error);
        }
Exemplo n.º 14
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));
        }
Exemplo n.º 15
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));
        }