Context MaxAttributeContext(bool isDefined, int length = 0) { return (c => c.Given(string.Format("max attribute isDefined:{0} of length: {1}", isDefined, length), x => { A.CallTo(() => Member.IsDefined(typeof(MaxLengthAttribute))).Returns(isDefined); var maxLengthAttribute = isDefined ? new MaxLengthAttribute(length) : null; A.CallTo(() => Member.GetCustomAttribute <MaxLengthAttribute> ()).Returns(maxLengthAttribute); })); }
public static RangeContstraints <T> FromMember([CanBeNull] IFastMemberWithValues member) { if (member == null) { return(null); } if (!member.IsDefined(typeof(RangeAttribute))) { return(null); } var rangeAttribute = member.GetCustomAttribute <RangeAttribute>(); if (rangeAttribute == null) { return(null); } if (rangeAttribute.OperandType != typeof(T)) { return(null); } var minValue = (T)Convert.ChangeType(rangeAttribute.Minimum, typeof(T)); var maxValue = (T)Convert.ChangeType(rangeAttribute.Maximum, typeof(T)); if ((minValue).CompareTo(maxValue) > 0) { throw new ArgumentOutOfRangeException( string.Format("On the member {0} {1} the Range attribute has an invalid range", member.Type, member.Name)); } return(new RangeContstraints <T> (minValue, maxValue)); }
Context PropertyInfoContext(bool isDefined, Type rangeType = null, string minValue = null, string maxValue = null) { return (c => c.Given(string.Format("range attribute isDefined:{0} of type {1} between: {2} and {3}", isDefined, rangeType, minValue, maxValue), x => { A.CallTo(() => Member.IsDefined(typeof(RangeAttribute))).Returns(isDefined); RangeAttribute = isDefined ? new RangeAttribute(rangeType, minValue, maxValue) : null; A.CallTo(() => Member.GetCustomAttribute <RangeAttribute> ()).Returns(RangeAttribute); })); }
public static StringConstraints FromMember([CanBeNull] IFastMemberWithValues member) { if (member == null) { return(null); } var minLength = -1; var maxLength = -1; var minLengthDefined = false; var maxLengthDefined = false; if (member.IsDefined(typeof(MinLengthAttribute))) { var minLengthAttribute = member.GetCustomAttribute <MinLengthAttribute>(); minLength = minLengthAttribute?.Length ?? 0; minLengthDefined = true; } if (member.IsDefined(typeof(MaxLengthAttribute))) { var maxLengthAttribute = member.GetCustomAttribute <MaxLengthAttribute>(); maxLength = maxLengthAttribute?.Length ?? 0; maxLengthDefined = true; } if (minLengthDefined && maxLengthDefined && maxLength < minLength) { throw new ArgumentOutOfRangeException( string.Format( "On the member {0} {1} the MinLength attribute and MaxLength attribute result in an invalid range", member.Type, member.Name)); } if (member.IsDefined(typeof(StringLengthAttribute))) { var stringLengthAttribute = member.GetCustomAttribute <StringLengthAttribute>(); minLength = stringLengthAttribute?.MinimumLength ?? 0; maxLength = stringLengthAttribute?.MaximumLength ?? 0; if (maxLength < minLength) { throw new ArgumentOutOfRangeException( string.Format("On the member {0} {1} the StringLength attribute has an invalid range", member.Type, member.Name)); } } if (minLength < 0 && maxLength < 0) { return(null); } if (minLengthDefined && !maxLengthDefined) { maxLength = minLength + 100; } if (maxLengthDefined && !minLengthDefined) { minLength = maxLength - 100; } if (minLength < 0) { minLength = 0; } return(new StringConstraints(minLength, maxLength)); }