public void TryCatchWhenTest()
        {
            var expected = new InvalidOperationException("aaa");

            _mock.MethodWithReturn().Throws(expected);
            _mock.Condition.Returns(true);

            var parameter = ExpressionShortcuts.Parameter <IMock>();
            var action    = ExpressionShortcuts.Block()
                            .Parameter(parameter)
                            .Line(parameter.Assign(_mock))
                            .Line(ExpressionShortcuts.Try()
                                  .Body(parameter.Call(o => o.MethodWithReturn()))
                                  .Catch <InvalidOperationException>(
                                      e => ExpressionShortcuts.Null <string>(),
                                      e => e.Call(o => !parameter.Property(x => x.Condition) || o.Message == null)
                                      )
                                  )
                            .Lambda <Func <string> >()
                            .Compile();

            var actual = Assert.Throws <InvalidOperationException>(() => action());

            Assert.Equal(expected, actual);

            _mock.Received(1).MethodWithReturn();
            _ = _mock.Received(1).Condition;
        }
Пример #2
0
        public static Expression <Action <BindingContext, TextWriter, object> > Bind(CompilationContext context, Expression body, string templatePath)
        {
            var configuration = ExpressionShortcuts.Arg(context.Configuration);

            var writerParameter = ExpressionShortcuts.Parameter <TextWriter>("buffer");
            var objectParameter = ExpressionShortcuts.Parameter <object>("data");

            var bindingContext          = ExpressionShortcuts.Arg <BindingContext>(context.BindingContext);
            var inlinePartialsParameter = ExpressionShortcuts.Null <IDictionary <string, Action <TextWriter, object> > >();
            var textEncoder             = configuration.Property(o => o.TextEncoder);
            var encodedWriterExpression = ExpressionShortcuts.Call(() => EncodedTextWriter.From(writerParameter, (ITextEncoder)textEncoder));
            var parentContextArg        = ExpressionShortcuts.Var <BindingContext>("parentContext");

            var newBindingContext = ExpressionShortcuts.Call(
                () => BindingContext.Create(configuration, objectParameter, encodedWriterExpression, parentContextArg, templatePath, (IDictionary <string, Action <TextWriter, object> >)inlinePartialsParameter)
                );

            var shouldDispose = ExpressionShortcuts.Var <bool>("shouldDispose");

            Expression blockBuilder = ExpressionShortcuts.Block()
                                      .Parameter(bindingContext)
                                      .Parameter(shouldDispose)
                                      .Line(ExpressionShortcuts.Condition()
                                            .If(objectParameter.Is <BindingContext>(),
                                                bindingContext.Assign(objectParameter.As <BindingContext>())
                                                )
                                            .Else(block =>
            {
                block.Line(shouldDispose.Assign(true));
                block.Line(bindingContext.Assign(newBindingContext));
            })
                                            )
                                      .Line(ExpressionShortcuts.Try()
                                            .Body(block => block.Lines(((BlockExpression)body).Expressions))
                                            .Finally(ExpressionShortcuts.Condition()
                                                     .If(shouldDispose, bindingContext.Call(o => o.Dispose()))
                                                     )
                                            );

            return(Expression.Lambda <Action <BindingContext, TextWriter, object> >(blockBuilder, (ParameterExpression)parentContextArg.Expression, (ParameterExpression)writerParameter.Expression, (ParameterExpression)objectParameter.Expression));
        }
Пример #3
0
        private Expression HandleInvocationExpression(InvocationExpression invocation)
        {
            if (invocation.Type != typeof(void))
            {
                return(invocation.Update(invocation.Expression,
                                         ReplaceValuesOf <TextWriter>(invocation.Arguments, ExpressionShortcuts.Null <TextWriter>()).Select(Visit)
                                         ));
            }

            var context = ExpressionShortcuts.Var <BindingContext>();
            var writer  = ExpressionShortcuts.Var <TextWriter>();

            invocation = invocation.Update(ExpressionUtils.ReplaceParameters(invocation.Expression, context),
                                           ExpressionUtils.ReplaceParameters(
                                               ReplaceValuesOf <TextWriter>(invocation.Arguments, writer), new Expression[] { context }
                                               ).Select(Visit)
                                           );

            var formatProvider = ExpressionShortcuts.Arg(CompilationContext.Configuration.FormatProvider);
            var block          = ExpressionShortcuts.Block()
                                 .Parameter(writer, ExpressionShortcuts.New(() => new PolledStringWriter((IFormatProvider)formatProvider)))
                                 .Line(writer.Using((o, body) =>
                                                    body.Line(invocation)
                                                    .Line(o.Call(x => (object)x.ToString()))
                                                    ));

            var continuation = _expressionCompiler.Compile(Expression.Lambda <Func <BindingContext, object> >(block, (ParameterExpression)context));

            return(ExpressionShortcuts.Arg <object>(Expression.Invoke(Expression.Constant(continuation), CompilationContext.BindingContext)));
        }