public void InvalidLengthShouldThrowTestTwo() { List <SomeDemoObject> inValues = new List <SomeDemoObject>() { new SomeDemoObject() { Integer = 1, String = "a" }, new SomeDemoObject() { Integer = 2, String = "ab" }, new SomeDemoObject() { Integer = 3, String = "cb" } }; MaxLengthTestObject obj = new MaxLengthTestObject(); obj.InvalidLengthDefinedForList.AddRange(inValues); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "InvalidLengthDefinedForList"; Action act = () => Validator.ValidateProperty(obj.InvalidLengthDefinedForList, ctx); act.ShouldThrowExactly <InvalidOperationException>("MaxLength is a negative value."); }
public void InvalidItemPropertyName() { List <SomeDemoObject> inValues = new List <SomeDemoObject>() { new SomeDemoObject() { Integer = 1, String = "a" }, new SomeDemoObject() { Integer = 2, String = "ab" }, new SomeDemoObject() { Integer = 3, String = "cb" } }; MaxLengthTestObject obj = new MaxLengthTestObject(); obj.InvalidPropertyNameList.AddRange(inValues); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "InvalidPropertyNameList"; Action act = () => Validator.ValidateProperty(obj.InvalidPropertyNameList, ctx); act.ShouldThrowExactly <ValidationException>("Named property not found from item."); }
public void NullItemPropertyValueShouldntMakeTheWholeListValid() { // COMMENT: if there is a null value in the list item property, currently makes the whole list values valid List <SomeDemoObject> inValues = new List <SomeDemoObject>() { new SomeDemoObject() { Integer = 1, String = "a" }, new SomeDemoObject() { Integer = 2, String = "ab" }, new SomeDemoObject() { Integer = 3, String = "cb" }, new SomeDemoObject() { Integer = 3, String = null // this makes whole list valid }, new SomeDemoObject() { Integer = 4, String = "too long string" } }; MaxLengthTestObject obj = new MaxLengthTestObject(inValues); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ValuesList"; Action act = () => Validator.ValidateProperty(obj.ValuesList, ctx); act.ShouldThrowExactly <ValidationException>().WithMessage("Maximum length of property 'String' must be '2'.", "Null property value in a single item shouldn't make all list items valid."); }
public void PublicIsValidAndProtectedIsValidShouldWorkTheSame() { // COMMENT: background, for example .Net framework MaxLengthAttribute is implemneted in public IsValid method // out inherited attribute was implemented in protected IsValid so that means we have two different implementations that work differently // public IsValid(object) and protected IsValid(object, validationContext) should result in same // public isvalid validates string length or collection count, custom implementation validate list objects string length ListPropertyMaxLengthAttribute attr = new ListPropertyMaxLengthAttribute(2, "String"); List <SomeDemoObject> inValues = new List <SomeDemoObject>() { new SomeDemoObject() { Integer = 1, String = "a" }, new SomeDemoObject() { Integer = 2, String = "ab" }, new SomeDemoObject() { Integer = 3, String = "cb" } }; // this call actually currently checks that there are at maximum 2 items in the list and returns fale because there are 3 items in the list bool publicIsValid = attr.IsValid(inValues); MaxLengthTestObject obj = new MaxLengthTestObject(inValues); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ValuesList"; List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateProperty(inValues, ctx, validationResults).Should().Be(publicIsValid, "Public and protected IsValid methods should result to the same outcome."); }
public void ItemTypeValueNullShouldntThrow() { // validator tries to validate items which Type property value is: TypeValueTest // list items have null value, so these should be skipped by the loop // currently causes nullreferenceexception because code tries to do item.Type.ToString() and the Type value is null List <SomeDemoObject> inValues = new List <SomeDemoObject>() { new SomeDemoObject() { Integer = 1, String = "a" }, new SomeDemoObject() { Integer = 2, String = "ab" }, new SomeDemoObject() { Integer = 3, String = "cb" } }; MaxLengthTestObject obj = new MaxLengthTestObject(); obj.ItemTypeValueNullList.AddRange(inValues); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ItemTypeValueNullList"; Action act = () => Validator.ValidateProperty(obj.ItemTypeValueNullList, ctx); act.ShouldNotThrow(); }
public void InvalidValueInValuesList() { List <SomeDemoObject> inValues = new List <SomeDemoObject>() { new SomeDemoObject() { Integer = 1, String = "a" }, new SomeDemoObject() { Integer = 2, String = "ab" }, new SomeDemoObject() { Integer = 3, String = "cb" }, new SomeDemoObject() { Integer = 4, String = "too long string" } }; MaxLengthTestObject obj = new MaxLengthTestObject(inValues); ValidationContext ctx = new ValidationContext(obj); ctx.MemberName = "ValuesList"; Action act = () => Validator.ValidateProperty(obj.ValuesList, ctx); act.ShouldThrowExactly <ValidationException>().WithMessage("Maximum length of property 'String' must be '2'."); }