public void EmptyListIsValidProtectedIsValid() { RegExpDemoObject obj = new RegExpDemoObject(); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "DemoStrings"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.DemoStrings, ctx, validationResults).Should().BeTrue(); }
public void EmptyPatternShouldThrow() { RegExpDemoObject obj = new RegExpDemoObject(); obj.EmptyRegExPattern.Add("Something"); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "EmptyRegExPattern"; Action act = () => Validator.ValidateProperty(obj.EmptyRegExPattern, ctx); act.ShouldThrow <InvalidOperationException>("RegEx pattern cannot be empty string."); }
public void InvalidPatternShouldThrow() { RegExpDemoObject obj = new RegExpDemoObject(); obj.InvalidRegExPattern.Add("Something"); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "InvalidRegExPattern"; Action act = () => Validator.ValidateProperty(obj.InvalidRegExPattern, ctx); act.ShouldThrowExactly <ArgumentException>("Because regex pattern is invalid."); }
public void ShouldThrowInvalidPropertyTypeUsage() { RegExpDemoObject obj = new RegExpDemoObject(); // this needs to be different from null so that the TryValidateProperty doesn't throw ArgumentNullException obj.InvalidPropertyUsage = "some string"; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "InvalidPropertyUsage"; Action act = () => Validator.ValidateProperty(obj.InvalidPropertyUsage, ctx); act.ShouldThrow <InvalidOperationException>("Attribute used on a property that doesn't implement IList."); }
public void PublicIsValidAndProtectedIsValidShouldUseSameImplementationEmptyList() { ListRegularExpressionAttribute attr = new ListRegularExpressionAttribute(@"^\d+$"); // actually currently what happens is that list is converted to string and it doesn't match the numeric string pattern bool publicIsValid = attr.IsValid(new List <string>()); RegExpDemoObject obj = new RegExpDemoObject(); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "DemoStrings"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.DemoStrings, ctx, validationResults).Should().Be(publicIsValid); }
public void ListContainsValidNumericStrings() { RegExpDemoObject obj = new RegExpDemoObject(); obj.DemoStrings.Add("555"); obj.DemoStrings.Add("1"); obj.DemoStrings.Add("2017"); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "DemoStrings"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.DemoStrings, ctx, validationResults).Should().BeTrue(); }
public void ListContainsValidThreeLetterStrings() { RegExpDemoObject obj = new RegExpDemoObject(); obj.ThreeLetterItems = new List <string>() { "ana", "ptv", "vrk" }; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ThreeLetterItems"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.ThreeLetterItems, ctx, validationResults).Should().BeTrue(); }
public void ListContainsThreeLetterStringsAndNullShouldntCauseInternalCrash() { RegExpDemoObject obj = new RegExpDemoObject(); obj.ThreeLetterItems = new List <string>() { "ana", "ptv", "vrk", null }; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ThreeLetterItems"; Action act = () => Validator.ValidateProperty(obj.ThreeLetterItems, ctx); act.ShouldNotThrow <NullReferenceException>("Code shouldn't crash to the null value but check for nulls."); }
public void ListContainsInvalidThreeLetterStrings() { RegExpDemoObject obj = new RegExpDemoObject(); obj.ThreeLetterItems = new List <string>() { "ana", "ptv", "vrk", "IS" }; ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ThreeLetterItems"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(obj.ThreeLetterItems, ctx, validationResults).Should().BeFalse("Because 'IS' is not a three letter string."); }