public void Test_constraint_expression()
        {
            var person = new Person();
            person.Name = "Mars";
            person.Age = 10;
            person.Id = 2;

            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);
            var results = validator.Validate(person);

            Assert.IsTrue(results.IsSuccess);
        }
        public void Test_default_value()
        {
            var person = new Person();

            person.Minimum = -1;
            person.Maximum = 10;
            person.Age = 10;
            person.Name = "Mars";

            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);
            var validator2 = Validator<Person>.NewValidator(ValidatorOptions.Empty);
            var validator3 = Validator.NewValidator(typeof(Person), ValidatorOptions.Empty);

            Assert.AreSame(validator, validator2);
            Assert.AreSame(validator, validator3);

            var result = validator.Validate(person);

            Assert.That(result.IsSuccess, Is.True);

            Assert.AreEqual(11, person.Minimum);
        }
        public void Test_ValueRequired_2()
        {
            var person = new Person();
            person.Name = "";

            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);

            var results = validator.Validate(person);

            Assert.AreEqual(3, results.ValidationExceptions.Count);
            Assert.AreEqual("Name", results.ValidationExceptions[0].PropertyValidationContext.PropertyInfo.Name);
            Assert.AreEqual("Name", results.ValidationExceptions[1].PropertyValidationContext.PropertyInfo.Name);
            Assert.AreEqual("Age", results.ValidationExceptions[2].PropertyValidationContext.PropertyInfo.Name);

            person.Name = "Mars";
            person.Age = 10;

            results = validator.Validate(person);

            Assert.IsTrue(results.IsSuccess);
        }
        public void Test_ValueRequired1()
        {
            var person = new Person();

            person.Minimum = -1;
            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);

            var results = validator.Validate(person);

            Assert.AreEqual(3, results.ValidationExceptions.Count);
            // Defaults should not be set if there is a failure
            Assert.AreEqual(-1, person.Minimum);
            Assert.AreEqual("Name", results.ValidationExceptions[0].PropertyValidationContext.PropertyInfo.Name);
            Assert.AreEqual("Name", results.ValidationExceptions[1].PropertyValidationContext.PropertyInfo.Name);
            Assert.AreEqual("Age", results.ValidationExceptions[2].PropertyValidationContext.PropertyInfo.Name);
        }
        public void Test_ValueConstraint()
        {
            var person = new Person();
            person.Name = "Mars";
            person.Age = 10;

            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);
            var results = validator.Validate(person);
            Assert.IsTrue(results.IsSuccess);

            person.Age = 11;
            results = validator.Validate(person);
            Assert.IsFalse(results.IsSuccess);

            person.Age = 0;
            results = validator.Validate(person);
            // Fails cause Age has value required set
            Assert.IsFalse(results.IsSuccess);

            person.Age = 1;
            results = validator.Validate(person);
            Assert.IsTrue(results.IsSuccess);

            person.Age = -1;
            results = validator.Validate(person);

            Assert.IsFalse(results.IsSuccess);
        }
        public void Test_SizeConstraint()
        {
            var person = new Person();
            person.Minimum = -1;
            person.Maximum = 10;
            person.Age = 10;
            person.Name = "Mars";

            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);

            var result = validator.Validate(person);

            Assert.IsTrue(result.IsSuccess);

            person.Name = "0123456789";
            result = validator.Validate(person);
            Assert.IsTrue(result.IsSuccess);

            person.Name = "01234567891";
            result = validator.Validate(person);
            Assert.IsFalse(result.IsSuccess);
            WriteValidationExceptions(result);

            person.Name = "";
            result = validator.Validate(person);
            Assert.IsFalse(result.IsSuccess);
            WriteValidationExceptions(result);

            person.Name = null;
            result = validator.Validate(person);
            Assert.IsFalse(result.IsSuccess);
            WriteValidationExceptions(result);

            person.Name = "1";
            result = validator.Validate(person);
            Assert.IsTrue(result.IsSuccess);
        }
        public void Test_nullable_default_value_when_valid()
        {
            var person = new Person();
            person.Name = "Mars";
            person.Age = 10;
            person.LibraryAccountNo = 0;

            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);

            var result = validator.Validate(person);
            Assert.That(result.IsSuccess, Is.True);

            Assert.AreEqual(0, person.LibraryAccountNo);
        }
        public void Test_nullable_default_value_when_invalid()
        {
            var person = new Person();
            person.Name = "Mars";
            person.Age = 10;
            person.LibraryAccountNo = -5;

            var validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);

            var result = validator.Validate(person);
            Assert.That(result.IsSuccess, Is.False);

            person.LibraryAccountNo = 1000002354;
            validator = Validator<Person>.NewValidator(ValidatorOptions.Empty);

            result = validator.Validate(person);
            Assert.That(result.IsSuccess, Is.False);
        }