public void can_find_arguments_in_parameterless_method_call() { var arguments = ArgumentsVisitor.FindArgumentsWithin(GetExpression(() => Console.WriteLine())); Assert.NotNull(arguments); Assert.Equal(0, arguments.Length); }
public void can_find_arguments_within_method_call_taking_parameters() { var arguments = ArgumentsVisitor.FindArgumentsWithin(GetExpression(() => Console.WriteLine("something {0}, {1}, {2}", 13, DateTime.MinValue, Tuple.Create(1, "one")))); Assert.NotNull(arguments); Assert.Equal(4, arguments.Length); Assert.Equal("something {0}, {1}, {2}", arguments[0]); Assert.Equal(13, arguments[1]); Assert.Equal(DateTime.MinValue, arguments[2]); Assert.Equal(Tuple.Create(1, "one"), arguments[3]); }
/// <summary> /// Applies any specifications configured via <see cref="When"/> for a given member. /// </summary> /// <param name="selector"> /// An expression that resolves to the member. /// </param> protected TMember Apply <TMember>(Expression <Func <TMock, TMember> > selector) { var args = ArgumentsVisitor.FindArgumentsWithin(selector.Body) ?? emptyArgs; WhenContinuationCollection whenContinuationCollection; var continuation = this.GetWhenContinuation(selector, args, out whenContinuationCollection); whenContinuationCollection.AddInvocation(new Invocation(args)); if (continuation == null) { return(default);
/// <summary> /// Applies any specifications configured via <see cref="When"/> for a given member. /// </summary> /// <param name="selector"> /// An expression that resolves to the member. /// </param> protected void Apply(Expression <Action <TMock> > selector) { var args = ArgumentsVisitor.FindArgumentsWithin(selector.Body) ?? emptyArgs; WhenContinuationCollection whenContinuationCollection; var continuation = this.GetWhenContinuation(selector, args, out whenContinuationCollection); whenContinuationCollection.AddInvocation(new Invocation(args)); if (continuation == null) { return; } continuation.Apply(this.MockedObject, args); }
/// <summary> /// Applies any specifications configured via <see cref="WhenPropertySet{TMember}"/> for a given property setter. /// </summary> /// <param name="selector"> /// An expression that resolves to the property being set. /// </param> /// <param name="value"> /// The value being assigned to the property. /// </param> protected void ApplyPropertySet <TMember>(Expression <Func <TMock, TMember> > selector, object value) { // base arguments would be any indexers to the property var indexerArgs = ArgumentsVisitor.FindArgumentsWithin(selector.Body) ?? emptyArgs; var args = new object[indexerArgs.Length + 1]; Array.Copy(indexerArgs, args, indexerArgs.Length); args[args.Length - 1] = value; WhenContinuationCollection whenContinuationCollection; var continuation = this.GetWhenContinuation(selector, args, out whenContinuationCollection); whenContinuationCollection.AddInvocation(new Invocation(args)); if (continuation == null) { return; } continuation.Apply(this.MockedObject, args); }