/// <summary> /// Initializes a new instance of the <see cref="ConstraintsExpectation"/> class. /// Creates a new <see cref="ConstraintsExpectation"/> instance. /// </summary> /// <param name="expectation"> /// Expectation. /// </param> /// <param name="constraints"> /// Constraints. /// </param> public ConstraintsExpectation(IExpectation expectation, AbstractConstraint[] constraints) : base(expectation) { Validate.IsNotNull(() => constraints); this.constraints = constraints; ConstraintsMatchMethod(); }
/// <summary> /// Initializes a new instance of the <see cref="ConstraintsExpectation"/> class. /// Creates a new <see cref="ConstraintsExpectation"/> instance. /// </summary> /// <param name="invocation"> /// Invocation for this expectation /// </param> /// <param name="constraints"> /// Constraints. /// </param> /// <param name="expectedRange"> /// Number of method calls for this expectations /// </param> public ConstraintsExpectation(IInvocation invocation, AbstractConstraint[] constraints, Range expectedRange) : base(invocation, expectedRange) { Validate.IsNotNull(() => constraints); this.constraints = constraints; ConstraintsMatchMethod(); }
/// <summary> /// Builds the default expectation. /// </summary> /// <param name="invocation"> /// The invocation. /// </param> /// <param name="method"> /// The method. /// </param> /// <param name="args"> /// The args. /// </param> /// <param name="callCallRangeExpectation"> /// The call call range expectation. /// </param> /// <returns> /// </returns> public IExpectation BuildDefaultExpectation(IInvocation invocation, MethodInfo method, object[] args, Func<Range> callCallRangeExpectation) { ParameterInfo[] parameters = method.GetParameters(); if (!Array.Exists(parameters, p => p.IsOut)) { return new ArgsEqualExpectation(invocation, args, callCallRangeExpectation()); } // The value of an incoming out parameter variable is ignored AbstractConstraint[] constraints = new AbstractConstraint[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { constraints[i] = parameters[i].IsOut ? Is.Anything() : Is.Equal(args[i]); } return new ConstraintsExpectation(invocation, constraints, callCallRangeExpectation()); }
/// <summary> /// Constrains the parameter to have a public field satisfying a specified constraint. /// </summary> /// <param name="declaringType"> /// The type that declares the public field, used to disambiguate between public fields. /// </param> /// <param name="publicFieldName"> /// Name of the public field. /// </param> /// <param name="publicFieldConstraint"> /// Constraint for the public field. /// </param> public static AbstractConstraint ValueConstraint(Type declaringType, string publicFieldName, AbstractConstraint publicFieldConstraint) { return new PublicFieldConstraint(declaringType, publicFieldName, publicFieldConstraint); }
/// <summary> /// Constrains the parameter to have a public field satisfying a specified constraint. /// </summary> /// <param name="publicFieldName"> /// Name of the public field. /// </param> /// <param name="publicFieldConstraint"> /// Constraint for the public field. /// </param> public static AbstractConstraint ValueConstraint(string publicFieldName, AbstractConstraint publicFieldConstraint) { return new PublicFieldConstraint(publicFieldName, publicFieldConstraint); }
/// <summary> /// Initializes a new instance of the <see cref="ArgumentDefinition"/> struct. /// </summary> /// <param name="returnValue"> /// The return value. /// </param> public ArgumentDefinition(object returnValue) { this.InOutRef = InOutRefArgument.OutArg; this.returnValue = returnValue; this.constraint = Is.Anything(); }
/// <summary> /// Initializes a new instance of the <see cref="ArgumentDefinition"/> struct. /// </summary> /// <param name="constraint"> /// The constraint. /// </param> /// <param name="returnValue"> /// The return value. /// </param> public ArgumentDefinition(AbstractConstraint constraint, object returnValue) { this.InOutRef = InOutRefArgument.RefArg; this.constraint = constraint; this.returnValue = returnValue; }
/// <summary> /// Initializes a new instance of the <see cref="ArgumentDefinition"/> struct. /// </summary> /// <param name="constraint"> /// The constraint. /// </param> public ArgumentDefinition(AbstractConstraint constraint) { this.InOutRef = InOutRefArgument.InArg; this.constraint = constraint; this.returnValue = null; }
/// <summary> /// The check method signature. /// </summary> /// <param name="method"> /// The method. /// </param> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> /// <exception cref="InvalidOperationException"> /// </exception> internal static void CheckMethodSignature(MethodInfo method) { InitializeThreadStatic(); ParameterInfo[] parameters = method.GetParameters(); AbstractConstraint[] constraints = new AbstractConstraint[parameters.Length]; if (args.Count < parameters.Length) { throw new InvalidOperationException( string.Format( "When using Arg<T>, all arguments must be defined using Arg<T>.Is, Arg<T>.Text, Arg<T>.List, Arg<T>.Ref or Arg<T>.Out. {0} arguments expected, {1} have been defined.", parameters.Length, args.Count)); } if (args.Count > parameters.Length) { throw new InvalidOperationException( string.Format( "Use Arg<T> ONLY within a mock method call while recording. {0} arguments expected, {1} have been defined.", parameters.Length, args.Count)); } for (int i = 0; i < parameters.Length; i++) { if (parameters[i].IsOut) { if (args[i].InOutRef != InOutRefArgument.OutArg) { throw new InvalidOperationException( string.Format("Argument {0} must be defined as: out Arg<T>.Out(returnvalue).Dummy", i)); } } else if (parameters[i].ParameterType.IsByRef) { if (args[i].InOutRef != InOutRefArgument.RefArg) { throw new InvalidOperationException( string.Format( "Argument {0} must be defined as: ref Arg<T>.Ref(constraint, returnvalue).Dummy", i)); } } else if (args[i].InOutRef != InOutRefArgument.InArg) { throw new InvalidOperationException( string.Format("Argument {0} must be defined using: Arg<T>.Is, Arg<T>.Text or Arg<T>.List", i)); } } }
/// <summary> /// The add ref argument. /// </summary> /// <param name="constraint"> /// The constraint. /// </param> /// <param name="returnValue"> /// The return value. /// </param> internal static void AddRefArgument(AbstractConstraint constraint, object returnValue) { InitializeThreadStatic(); args.Add(new ArgumentDefinition(constraint, returnValue)); }
/// <summary> /// The add in argument. /// </summary> /// <param name="constraint"> /// The constraint. /// </param> internal static void AddInArgument(AbstractConstraint constraint) { InitializeThreadStatic(); args.Add(new ArgumentDefinition(constraint)); }
/// <summary> /// Constrains the parameter to have a property satisfying a specified constraint. /// </summary> /// <param name="declaringType"> /// The type that declares the property, used to disambiguate between properties. /// </param> /// <param name="propertyName"> /// Name of the property. /// </param> /// <param name="propertyConstraint"> /// Constraint for the property. /// </param> public static AbstractConstraint ValueConstraint(Type declaringType, string propertyName, AbstractConstraint propertyConstraint) { return new PropertyConstraint(declaringType, propertyName, propertyConstraint); }
/// <summary> /// Constrains the parameter to have a property satisfying a specified constraint. /// </summary> /// <param name="propertyName"> /// Name of the property. /// </param> /// <param name="propertyConstraint"> /// Constraint for the property. /// </param> public static AbstractConstraint ValueConstraint(string propertyName, AbstractConstraint propertyConstraint) { return new PropertyConstraint(propertyName, propertyConstraint); }