public void can_find_arguments_in_parameterless_method_call() { var arguments = ArgumentsVisitor.FindArgumentsWithin(GetExpression(() => Console.WriteLine())); Assert.NotNull(arguments); Assert.Equal(0, arguments.Length); }
private static T InvokeAndLog <T>(Expression <Func <T> > expr) { var visitor = new ArgumentsVisitor(); visitor.Visit(expr); Console.WriteLine("Inputs: {0}", string.Join(", ", visitor.Arguments)); return(expr.Compile()()); }
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); }
protected override void ProcessRecord() { using (var reader = new StringReader(Script)) { var parser = new TSql90Parser(false); IList<ParseError> errors; var result = parser.Parse(reader, out errors); if (errors.Count == 0) { var visitor = new ArgumentsVisitor(); result.Accept(visitor); WriteObject(new { ProcedureName = visitor.ProcedureName, Parameters = visitor.Parameters }); } else { foreach (var error in errors) { var errorRecord = new ErrorRecord( new FormatException($"{error.Message} Line:{error.Line}:{error.Column}"), null, ErrorCategory.InvalidArgument, Script ); WriteError(errorRecord); } } } }
protected override void ProcessRecord() { using (var reader = new StringReader(Script)) { var parser = new TSql90Parser(false); IList <ParseError> errors; var result = parser.Parse(reader, out errors); if (errors.Count == 0) { var visitor = new ArgumentsVisitor(); result.Accept(visitor); WriteObject(new { ProcedureName = visitor.ProcedureName, Parameters = visitor.Parameters }); } else { foreach (var error in errors) { var errorRecord = new ErrorRecord( new FormatException($"{error.Message} Line:{error.Line}:{error.Column}"), null, ErrorCategory.InvalidArgument, Script ); WriteError(errorRecord); } } } }
public void cannot_find_arguments_if_there_is_a_method_call_but_it_is_not_at_root_level() { object[] arguments; Assert.False(ArgumentsVisitor.TryFindArgumentsWithin(GetExpression(() => new[] { Tuple.Create(1, 2) }), out arguments)); }
public void cannot_find_arguments_in_non_method_call() { object[] arguments; Assert.False(ArgumentsVisitor.TryFindArgumentsWithin(GetExpression(() => 13), out arguments)); Assert.False(ArgumentsVisitor.TryFindArgumentsWithin(GetExpression(() => "foo"), out arguments)); }