public void ShouldPassForIsTrueAndFailForIsFalse()
        {
            var person = new Person {Goals = 101, Fouls = 9};

            var validator = person.Validate().IsTrue(v => v.Goals > 100 && v.Fouls < 10, "Goals should be greater than 100 and fouls should be less than 10.");
            Assert.IsTrue(validator.IsValid);

            validator = person.Validate().IsFalse(v => v.Goals > 100 && v.Fouls < 10, "Goals should be greater than 100 and fouls should be less than 10.");
            Assert.IsFalse(validator.IsValid);
        }
 public void ShouldPassForLesserThanOtherMember()
 {
     var person = new Person { Name = "Some Name", Age = 12, Goals = 10 };
     var validator = person.Validate()
                     .IsLesserThan(p => p.Goals, p => p.Age, "Goals scored should be lesser than age.");
     Assert.IsTrue(validator.IsValid);
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
 {
     var person = new Person { HomeAddress = new Address{Country = "USA"}};
     var validator = person.Validate()
         .IsOneOf(v => v.HomeAddress.Country, null, "UK", "Canada", "France");
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Address.Country should be one of { UK | Canada | France }."));
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage2()
 {
     var person = new Person { Name = "Some Name", NickName = "Some Name" };
     var validator = person.Validate().IsNotEqualTo(v => v.Name, v => v.NickName);
     Assert.IsFalse(validator.IsValid);
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Name should not be equal to Person.NickName."));
 }
        public void ShouldPassForOneOf()
        {
            var person = new Person { Name = "Person2" };

            var validator = person.Validate().IsOneOf(p => p, "Person is not one of people.", new Person { Name = "Person1" }, new Person { Name = "Person2" }, new Person { Name = "Person3" });
            Assert.IsTrue(validator.IsValid);
        }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
 {
     var person = new Person { EmailAddresses = new List<string>{"Email1", "Email2", "Email3"}};
     var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
         .Contains(v => v.EmailAddresses, null, "Email3", "Email4", "Email5");
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.EmailAddresses should contain { Email3 | Email4 | Email5 }."));
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage2()
 {
     var person = new Person { Name = "Some Name", Age = 12, Goals = 20 };
     var validator = person.Validate()
                     .IsLesserThan(p => p.Goals, p => p.Age);
     Assert.IsFalse(validator.IsValid);
     Assert.AreEqual(validator.Errors[0].Message, "Person.Goals should be lesser than Person.Age.");
 }
        public void ShouldPassIfThen()
        {
            var obj = new Person { Name = "Ashish", Goals = 15, Fouls = 100, Age = 26 };
            var validator = obj.Validate()
                .IfThen(p => p.Goals < 10, "Failed validation", p => p.Fouls < 5, p => p.Age > 0);

            Assert.IsTrue(validator.IsValid);
        }
 public void ShouldFailForLesserThanOtherMember()
 {
     var person = new Person { Name = "Some Name", Age = 12, Goals = 20 };
     var validator = person.Validate()
                     .IsLesserThan(p => p.Goals, p => p.Age, "Goals scored should be lesser than age.");
     Assert.IsFalse(validator.IsValid);
     Assert.AreEqual(validator.Errors[0].Message, "Goals scored should be lesser than age.");
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
 {
     var person = new Person { Name = "Some Name", Age = 26, Goals = 100 };
     var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
         .IsNotBetween(v => v.Age, 25, 30)
         .IsNotBetween(v => v.Goals, 100, 150);
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Age should not be between \"25\" and \"30\"."));
     Assert.That(validator.Errors[1].Message, Is.EqualTo("Person.Goals should not be between \"100\" and \"150\"."));
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
 {
     var person = new Person { Name = "Some Name", Age = 18, HomeAddress = new Address { City = "Reading" } };
     var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
         .IsNotEqualTo(v => v.Age, 18)
         .IsNotEqualTo(v => v.HomeAddress.City, "Reading");
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Age should not be equal to 18."));
     Assert.That(validator.Errors[1].Message, Is.EqualTo("Address.City should not be equal to Reading."));
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage2()
 {
     var person = new Person();
     var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
                     .IsNotNull(p => p.Name)
                     .IsNotNull(p => p.HomeAddress.AddressLine1);
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Name should not be null."));
     Assert.That(validator.Errors[1].Message, Is.EqualTo("Address.AddressLine1 should not be null."));
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
 {
     var person = new Person { Name = "Some Name", Age = 18, Goals = 100 };
     var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
         .IsLesserThan(v => v.Age, 18)
         .IsLesserThan(v => v.Goals, 50);
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Age should be lesser than 18."));
     Assert.That(validator.Errors[1].Message, Is.EqualTo("Person.Goals should be lesser than 50."));
 }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
 {
     var person = new Person { HomeAddress = new Address{City = "Wrong City", Zipcode = "Wrong Zipcode"}};
     var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
         .MatchesRegex(v => v.HomeAddress.City, @"London\s\(South\)")
         .MatchesRegex(v => v.HomeAddress.Zipcode, @"\d{5,6}");
     Assert.That(validator.Errors[0].Message, Is.EqualTo(@"Address.City should match pattern ""London\s\(South\)""."));
     Assert.That(validator.Errors[1].Message, Is.EqualTo(@"Address.Zipcode should match pattern ""\d{5,6}""."));
 }
        public void ShouldFailOrValidation()
        {
            var obj = new Person { Name = "Ashish", Goals = 15, Fouls = 100 };
            var validator = obj.Validate()
                .Or("Some validation failed",
                    o => o.Validate().IsNull(v => v, "Object should not be null"),
                    o => o.Validate().IsNull(v => v.Name, "Name should be null")
                   );

            Assert.IsFalse(validator.IsValid);
        }
 public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage2()
 {
     var person = new Person{Name = "Some Name", HomeAddress = new Address{AddressLine1 = "Line 1"}, EmailAddresses = new List<string>{"Email1", "Email2"}};
     var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
                     .IsNullOrEmpty(p => p.Name)
                     .IsNullOrEmpty(p => p.HomeAddress.AddressLine1)
                     .IsNullOrEmpty(p => p.EmailAddresses);
     Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Name should be null or empty."));
     Assert.That(validator.Errors[1].Message, Is.EqualTo("Address.AddressLine1 should be null or empty."));
     Assert.That(validator.Errors[2].Message, Is.EqualTo("Person.EmailAddresses should be null or empty."));
 }
        public void ShouldPassForPassesSavedValidation()
        {
            var validation = new Validation<Person>("Person_Validation_Example")
                .Setup(v => v.IsNotNullOrEmpty(p => p.Name));
            var validationRepository = new ValidationRepositoryFactory().GetValidationRepository();
            validationRepository.Reset();
            validationRepository.Save(validation);

            var person = new Person {Name = "Some Name"};
            var validator = person.Validate().PassesSavedValidation(p => p, "Person_Validation_Example");
            Assert.IsTrue(validator.IsValid);
        }
        public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
        {
            var person = new Person { IsTrue = false};
            var validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
                .IsTrue(p => p.IsTrue);
            Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.IsTrue should be true."));

            person = new Person { IsTrue = true };
            validator = person.Validate(new ValidationOptions { StopOnFirstError = false })
                        .IsFalse(p => p.IsTrue);
            Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.IsTrue should be false."));
        }
        public void ShouldFailOrWithNestedAndValidation()
        {
            var obj = new Person { Name = "Ashish", Goals = 15, Fouls = 100 };
            var validator = obj.Validate()
                .Or("Or validation failed",
                    o => o.Validate().IsNull(v => v, "Object should be null"),
                    o => o.Validate().And("And validations failed.",
                                            x => x.Validate().IsNull(v => v.Name, "Name should be null"),
                                            x => x.Validate().IsGreaterThan(v => v.Age, 25,"Age should be greater than 25")),
                    o => o.Validate().IsNull(v => v.Name, "Name should be null")
                   );

            Assert.IsFalse(validator.IsValid);
        }
 public void ShouldFailForEqualTo()
 {
     var person = new Person { Name = "Some Name", Age = 18 };
     var validator = person.Validate().IsEqualTo(v => v.Age, 25, "Age should be 18");
     Assert.IsFalse(validator.IsValid);
 }
 public void ShouldPassForNotEqualTo()
 {
     var person = new Person { Name = "Some Name", Age = 25 };
     var validator = person.Validate().IsNotEqualTo(v => v.Age, 18, "Age should not be 18");
     Assert.IsTrue(validator.IsValid);
 }
 public void ShouldPassForNotEqualToOtherMember()
 {
     var person = new Person { Name = "Some Name", NickName = "NickName"};
     var validator = person.Validate().IsNotEqualTo(v => v.Name, v => v.NickName, "Name should not be same as NickName");
     Assert.IsTrue(validator.IsValid);
 }
 public void ShouldFailForEqualToOtherMember()
 {
     var person = new Person { Name = "Some Name", NickName = "NickName" };
     var validator = person.Validate().IsEqualTo(v => v.Name, v => v.NickName, "Name should be same as NickName");
     Assert.IsFalse(validator.IsValid);
 }
 public void ShouldPassForMatchesRegex()
 {
     var person = new Person {Name = "Some Person"};
     var validator = person.Validate().MatchesRegex(p => p.Name, "Some\\sPerson",message: "Regex match failed.");
     Assert.IsTrue(validator.IsValid);
 }