/// <summary> /// Creates a new specimen based on a requested range. /// </summary> /// <param name="request">The request that describes what to create.</param> /// <param name="context">A container that can be used to create other specimens.</param> /// <returns> /// A specimen created from a <see cref="RangedRequest"/> encapsulating the operand /// type, the minimum and the maximum of the requested value, if possible; otherwise, /// a <see cref="NoSpecimen"/> instance. /// </returns> public object Create(object request, ISpecimenContext context) { if (request == null) { return(new NoSpecimen()); } if (context == null) { throw new ArgumentNullException(nameof(context)); } var rangeAttribute = TypeEnvy.GetAttribute <RangeAttribute>(request); if (rangeAttribute == null) { return(new NoSpecimen()); } var memberType = GetMemberType(rangeAttribute, request); var rangedRequest = new RangedRequest( memberType, rangeAttribute.OperandType, rangeAttribute.Minimum, rangeAttribute.Maximum); return(context.Resolve(rangedRequest)); }
public static bool TryGetFromAttributes(object request, out Range range) { var minLengthAttribute = TypeEnvy.GetAttribute <MinLengthAttribute>(request); var maxLengthAttribute = TypeEnvy.GetAttribute <MaxLengthAttribute>(request); if (minLengthAttribute == null && maxLengthAttribute == null) { range = null; return(false); } range = GetRange(minLengthAttribute, maxLengthAttribute); return(true); }
/// <summary> /// Creates a new specimen based on a request. /// </summary> /// <param name="request">The request that describes what to create.</param> /// <param name="context">A context that can be used to create other specimens.</param> /// <returns> /// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance. /// </returns> public object Create(object request, ISpecimenContext context) { if (request == null) { return(new NoSpecimen()); } if (context == null) { throw new ArgumentNullException(nameof(context)); } var regularExpressionAttribute = TypeEnvy.GetAttribute <RegularExpressionAttribute>(request); if (regularExpressionAttribute == null) { return(new NoSpecimen()); } return(context.Resolve(new RegularExpressionRequest(regularExpressionAttribute.Pattern))); }
/// <summary> /// Creates a new specimen based on a specified length of characters that are allowed. /// </summary> /// <param name="request">The request that describes what to create.</param> /// <param name="context">A container that can be used to create other specimens.</param> /// <returns> /// A specimen created from a <see cref="RangedNumberRequest"/> encapsulating the operand /// type, the minimum and the maximum of the requested number, if possible; otherwise, /// a <see cref="NoSpecimen"/> instance. /// </returns> public object Create(object request, ISpecimenContext context) { if (request == null) { return(new NoSpecimen()); } if (context == null) { throw new ArgumentNullException(nameof(context)); } var stringLengthAttribute = TypeEnvy.GetAttribute <StringLengthAttribute>(request); if (stringLengthAttribute == null) { return(new NoSpecimen()); } return(context.Resolve(new ConstrainedStringRequest(stringLengthAttribute.MinimumLength, stringLengthAttribute.MaximumLength))); }