public void ValidateThat_SomeValue_IsLessThan() { var handle = new ExampleClassType { Age = 25 }; var result = Validate.That<ExampleClassType>() .Property(x => x.Age).IsLessThan(100).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Age).IsLessThan(24).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Age).IsLessThan(25).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Age).IsLessThan(25, RangeComparison.Inclusive).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Age).IsLessThan(25, RangeComparison.Exclusive).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_DoesNotContainOneOf() { var handle = new ExampleClassType { Collection = new [] { 'a', 'b', 'c' }, }; var result = Validate.That<ExampleClassType>() .Property(x => x.Collection).DoesNotContainAnyOf('1', '2', 'b').And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).DoesNotContainAnyOf('d', '1', '2').And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).DoesNotContainAnyOf(new[] { '1', '2', 'b' }).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).DoesNotContainAnyOf(new[] { 'd', '1', '2' }).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_SomeValue_LengthValue_Inclusive() { var handle = new ExampleClassType { Collection = new [] { 'a', 'b', 'c' }, }; var result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(3).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(1, 3).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(minimum:4).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(1, 2).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_DoesNotEndWith() { var handle = new ExampleClassType { FirstName = "Mr. Bjangles" }; var result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).DoesNotEndWith("les").And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).DoesNotEndWith("Mrs.").And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).DoesNotEndWith("LES", false).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).DoesNotEndWith("Mrs.", false).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_SomeValue_LengthValidation_Exclusive() { var handle = new ExampleClassType { Collection = new[] { 'a', 'b', 'c' }, }; var result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(maximum: 4, comparison: RangeComparison.Exclusive).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(1, 4, RangeComparison.Exclusive).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(4, comparison:RangeComparison.Exclusive).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(1, 2, RangeComparison.Exclusive).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void Validation_IsFastEnough() { int iterations = 1000; var handle = new ExampleClassType { FirstName = "Mr. Person", Age = 24, Collection = new [] { 'a', 'b', 'c' }, Insured = true, Birthday = DateTime.Now.AddYears(-1), }; var validator = Validate.That<ExampleClassType>() .Property(x => x.FirstName).IsNotNullOrEmpty().Contains("Mr").And() .Property(x => x.Age).IsEven().IsLessThan(65).And() .Property(x => x.Collection).ContainsAnyOf('a', 'z', 'y').And() .Property(x => x.Insured).IsTrue().And() .Property(x => x.Birthday).IsPast().And() // very slow .Compile().Value; var watch = Stopwatch.StartNew(); for (var count = 0; count <= iterations; ++count) { var result = validator.Validate(handle); } watch.Stop(); Assert.IsTrue(watch.ElapsedMilliseconds / iterations <= 1); }
public void Validation_Against_Baseline() { int iterations = 1000; var handle = new ExampleClassType { FirstName = "Mr. Person", Age = 24, Collection = new[] { 'a', 'b', 'c' }, Insured = true, Birthday = DateTime.Now.AddYears(-1), }; var contains = new[] { 'a', 'z', 'y' }; var watch = Stopwatch.StartNew(); for (var count = 0; count <= iterations; ++count) { var result = (!string.IsNullOrEmpty(handle.FirstName) && handle.FirstName.Contains("Mr")) && ((handle.Age % 2 == 0) && (handle.Age < 65)) && (contains.Any(v => handle.Collection.Contains(v))) && (handle.Insured) && (handle.Birthday < DateTime.Now); // very slow } watch.Stop(); Assert.IsTrue(watch.ElapsedMilliseconds / iterations <= 1); }
public void ValidateThat_SomeType_LengthValue_WithNull() { var handle = new ExampleClassType { Collection = null, }; var result = Validate.That<ExampleClassType>() .Property(x => x.Collection).HasLength(0).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void PropertyCollectionContext_SucceedsWith_EachItemIn() { var handle = new ExampleClassType { Collection = new[] { 'a', 'b', 'c' }, }; var result = Validate.That<ExampleClassType>() .EachItem(x => x.Collection).IsLowercase().And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void PredicateContext_ErrorMessage_Overloads() { var message = "The user's age is not a valid value"; var validator = Validate.That<ExampleClassType>() .Property(x => x.Age).IsEqualTo(10).And(message) .Compile().Value; var handle = new ExampleClassType { Age = 20, }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); Assert.AreEqual(message, result.Failures.First().ToString()); }
public void PredicateContext_PropertyName_Overloads() { var name = "UserAge"; var validator = Validate.That<ExampleClassType>() .Property(x => x.Age, name).IsEqualTo(10).And() .Compile().Value; var handle = new ExampleClassType { Age = 20, }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); Assert.AreEqual(name, result.Failures.First().PropertyName); }
public void ValidateThat_SomeValue_IsNull() { var validator = Validate.That<ExampleClassType>() .Property(x => x.Collection).Contains('b').And() .Compile().Value; var handle = new ExampleClassType { Collection = null, }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsUppercase() { var validator = Validate.That<ExampleClassType>() .Property(x => x.Letter).IsUppercase() .And().Compile().Value; var handle = new ExampleClassType { Letter = 'A' }; var result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); handle = new ExampleClassType { Letter = 'a' }; result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValidationValue_IsNotNullOrEmpty() { var validator = Validate.That<ExampleClassType>() .Property(x => x.Collection).IsNotNullOrEmpty().And() .Compile().Value; var handle = new ExampleClassType { Collection = null, }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); handle.Collection = new List<char> { 'a', 'b', 'c' }; result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_SomeFunction_IsTrue() { var validator = Validate.That<ExampleClassType>() .Property(x => x.Function).IsTrue().And() .Compile().Value; var handle = new ExampleClassType { Function = () => false }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); handle = new ExampleClassType { Function = () => true }; result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void PropertyCollectionContext_FailsWith_EachItemIn() { var handle = new ExampleClassType { Collection = new[] { 'a', 'b', 'c' }, }; var result = Validate.That<ExampleClassType>() .EachItem(x => x.Collection).IsUppercase().And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); Assert.AreEqual(1, result.Failures.Count()); }
public void ValidateThat_SomeValue_IsTrue() { var validator = Validate.That<ExampleClassType>() .Property(x => x.Insured).IsTrue().And() .Compile().Value; var handle = new ExampleClassType { Insured = true }; var result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); handle = new ExampleClassType { Insured = false }; result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_Matches() { var validator = Validate.That<ExampleClassType>() .Property(x => x.FirstName).Matches(@"\w*\. \w*").And() .Compile().Value; var handle = new ExampleClassType { FirstName = "Mr. Bjangles" }; var result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); handle = new ExampleClassType { FirstName = "123456789" }; result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValidationValue_IsNotNull() { var validator = Validate.That<ExampleClassType>() .IsNotNull() .Compile().Value; var handle = new ExampleClassType { Prefix = null }; var result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); handle = null; result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsPast() { var validator = Validate.That<ExampleClassType>() .Property(x => x.Birthday).IsPast().And() .Compile().Value; var handle = new ExampleClassType { Birthday = DateTime.Now.AddDays(-1) }; var result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); handle = new ExampleClassType { Birthday = DateTime.Now.AddDays(1) }; result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_DoesNotContain() { var validator = Validate.That<ExampleClassType>() .Property(x => x.FirstName).DoesNotContain("Mr.").And() .Compile().Value; var handle = new ExampleClassType { FirstName = "Mr. Bjangles" }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); handle = new ExampleClassType { FirstName = "Mrs. Bjangles" }; result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsNotEqualTo() { var validator = Validate.That<ExampleClassType>() .Property(x => x.Age).IsNotEqualTo(25).And() .Compile().Value; var handle = new ExampleClassType { Age = 25 }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); handle = new ExampleClassType { Age = 24 }; result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_VariousTypes_WillWorkWith_IsLessThan() { var date = DateTime.Now; var handle = new ExampleClassType { Letter = 'B', Birthday = date, Insured = true, Prefix = "Mr", Time = TimeSpan.FromMinutes(1), Balance = 12.34, }; var result = Validate.That<ExampleClassType>() .Property(x => x.Letter).IsLessThan('Z').And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Birthday).IsLessThan(date.AddDays(1)).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Insured).IsLessThan(true).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Prefix).IsLessThan("Ms").And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Time).IsLessThan(TimeSpan.FromHours(1)).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Balance).IsLessThan(13.00).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsNotNear() { var handle = new ExampleClassType { Balance = 12.34 }; var result = Validate.That<ExampleClassType>() .Property(x => x.Balance).IsNotNear(12).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Balance).IsNotNear(12, 0.01).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Balance).IsNotNear(15).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); result = Validate.That<ExampleClassType>() .Property(x => x.Balance).IsNotNear(15, 3).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsValidForm() { var handle = new ExampleClassType { FirstName = "*****@*****.**" }; var result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).IsValid(FormValidationType.Email).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); handle = new ExampleClassType { FirstName = "name email com" }; result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).IsValid(FormValidationType.AlphaAndSpaces).And() .Compile().Value.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsInValidForm() { var handle = new ExampleClassType { FirstName = "!@#!@#%$" }; var result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).IsValid(FormValidationType.Alpha).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); handle = new ExampleClassType { FirstName = "Hello World" }; result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).IsValid(FormValidationType.IpAddressIPv4).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_WithNullRegexThrows() { var handle = new ExampleClassType { FirstName = "Mr. Bjangles" }; var result = Validate.That<ExampleClassType>() .Property(x => x.FirstName).DoesNotMatch((Regex)null).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsStructNotNull() { var validator = Validate.That<ExampleClassType>() .Property(x => x.NullableType).IsNotNull().And() .Compile().Value; var handle = new ExampleClassType { NullableType = null }; var result = validator.Validate(handle); Assert.IsFalse(result.IsSuccessful); handle = new ExampleClassType { NullableType = 1 }; result = validator.Validate(handle); Assert.IsTrue(result.IsSuccessful); }
public void ValidateThat_SomeValue_IsNull() { var elements = (IEnumerable<char>)null; var handle = new ExampleClassType { Collection = null, }; var result = Validate.That<ExampleClassType>() .Property(x => x.Collection).ContainsAnyOf('1', '2', 'b').And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); handle = new ExampleClassType { Collection = new[] { 'a', 'b', 'c' }, }; result = Validate.That<ExampleClassType>() .Property(x => x.Collection).ContainsAnyOf(elements).And() .Compile().Value.Validate(handle); Assert.IsFalse(result.IsSuccessful); }