예제 #1
0
        public void VisitUnknownNonExtension()
        {
            var expressionNode = new SpecialExpressionNode((ExpressionType)(-1), typeof(int));

            Expect.Call(InvokeVisitMethod("VisitUnknownNonExtension", expressionNode)).CallOriginalMethod(OriginalCallOptions.CreateExpectation);
            MockRepository.ReplayAll();

            try
            {
                InvokeVisitMethod("VisitUnknownNonExtension", expressionNode);
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
예제 #2
0
        private static Dictionary <ExpressionType, Expression> InitializeExpressionTypeMap()
        {
            var map = new Dictionary <ExpressionType, Expression> ();

            Expression         zero             = Expression.Constant(0);
            Expression         dateTimeValue    = Expression.Constant(DateTime.MinValue);
            Expression         zeroDouble       = Expression.Constant(0.0);
            NewArrayExpression arrayExpression  = Expression.NewArrayInit(typeof(int), zero);
            Expression         trueExpression   = Expression.Constant(true);
            LambdaExpression   lambdaExpression = Expression.Lambda <Func <int> > (zero);
            NewExpression      newExpression    = Expression.New(typeof(List <int>).GetConstructor(new[] { typeof(int) }), zero);

            map[ExpressionType.Add]                = Expression.Add(zero, zero);
            map[ExpressionType.AddChecked]         = Expression.AddChecked(zero, zero);
            map[ExpressionType.And]                = Expression.And(zero, zero);
            map[ExpressionType.AndAlso]            = Expression.AndAlso(trueExpression, trueExpression);
            map[ExpressionType.ArrayLength]        = Expression.ArrayLength(arrayExpression);
            map[ExpressionType.ArrayIndex]         = Expression.ArrayIndex(arrayExpression, zero);
            map[ExpressionType.Call]               = Expression.Call(zero, typeof(int).GetMethod("Equals", new[] { typeof(int) }), zero);
            map[ExpressionType.Coalesce]           = Expression.Coalesce(arrayExpression, arrayExpression);
            map[ExpressionType.Conditional]        = Expression.Condition(trueExpression, zero, zero);
            map[ExpressionType.Constant]           = Expression.Constant(zero);
            map[ExpressionType.Convert]            = Expression.Convert(zero, typeof(object));
            map[ExpressionType.ConvertChecked]     = Expression.ConvertChecked(zero, typeof(object));
            map[ExpressionType.Divide]             = Expression.Divide(zero, zero);
            map[ExpressionType.Equal]              = Expression.Equal(zero, zero);
            map[ExpressionType.ExclusiveOr]        = Expression.ExclusiveOr(trueExpression, trueExpression);
            map[ExpressionType.GreaterThan]        = Expression.GreaterThan(zero, zero);
            map[ExpressionType.GreaterThanOrEqual] = Expression.GreaterThanOrEqual(zero, zero);
            map[ExpressionType.Invoke]             = Expression.Invoke(lambdaExpression);
            map[ExpressionType.Lambda]             = lambdaExpression;
            map[ExpressionType.LeftShift]          = Expression.LeftShift(zero, zero);
            map[ExpressionType.LessThan]           = Expression.LessThan(zero, zero);
            map[ExpressionType.LessThanOrEqual]    = Expression.LessThanOrEqual(zero, zero);
            map[ExpressionType.ListInit]           = Expression.ListInit(newExpression, zero);
            map[ExpressionType.MemberAccess]       = Expression.MakeMemberAccess(dateTimeValue, typeof(DateTime).GetProperty("Date"));
            map[ExpressionType.MemberInit]         = Expression.MemberInit(newExpression, Expression.Bind(typeof(List <int>).GetProperty("Capacity"), zero));
            map[ExpressionType.Modulo]             = Expression.Modulo(zero, zero);
            map[ExpressionType.Multiply]           = Expression.Multiply(zero, zero);
            map[ExpressionType.MultiplyChecked]    = Expression.MultiplyChecked(zero, zero);
            map[ExpressionType.Negate]             = Expression.Negate(zero);
            map[ExpressionType.UnaryPlus]          = Expression.UnaryPlus(zero);
            map[ExpressionType.NegateChecked]      = Expression.NegateChecked(zero);
            map[ExpressionType.New]                = newExpression;
            map[ExpressionType.NewArrayInit]       = arrayExpression;
            map[ExpressionType.NewArrayBounds]     = Expression.NewArrayBounds(typeof(int), zero);
            map[ExpressionType.Not]                = Expression.Not(trueExpression);
            map[ExpressionType.NotEqual]           = Expression.NotEqual(zero, zero);
            map[ExpressionType.Or]              = Expression.Or(trueExpression, trueExpression);
            map[ExpressionType.OrElse]          = Expression.OrElse(trueExpression, trueExpression);
            map[ExpressionType.Parameter]       = Expression.Parameter(typeof(object), "bla");
            map[ExpressionType.Power]           = Expression.Power(zeroDouble, zeroDouble);
            map[ExpressionType.Quote]           = Expression.Quote(lambdaExpression);
            map[ExpressionType.RightShift]      = Expression.RightShift(zero, zero);
            map[ExpressionType.Subtract]        = Expression.Subtract(zero, zero);
            map[ExpressionType.SubtractChecked] = Expression.SubtractChecked(zero, zero);
            map[ExpressionType.TypeAs]          = Expression.TypeAs(zero, typeof(object));
            map[ExpressionType.TypeIs]          = Expression.TypeIs(zero, typeof(object));
            map[(ExpressionType)(-1)]           = new SpecialExpressionNode((ExpressionType)(-1), typeof(int));
#if !NET_3_5
            map[ExpressionType.Block]            = Expression.Block(zero);
            map[ExpressionType.DebugInfo]        = Expression.DebugInfo(Expression.SymbolDocument("test.cs"), 1, 1, 1, 1);
            map[ExpressionType.Default]          = Expression.Default(typeof(int));
            map[ExpressionType.Goto]             = Expression.Goto(Expression.Label(), zero);
            map[ExpressionType.Index]            = Expression.MakeIndex(arrayExpression, typeof(int[]).GetProperty("Item"), new[] { zero });
            map[ExpressionType.Label]            = Expression.Label(Expression.Label(), zero);
            map[ExpressionType.Loop]             = Expression.Loop(zero);
            map[ExpressionType.RuntimeVariables] = Expression.RuntimeVariables(Expression.Parameter(typeof(string)));
            map[ExpressionType.Switch]           = Expression.Switch(zero, Expression.SwitchCase(Expression.Default(typeof(void)), Expression.Constant(1)));
            map[ExpressionType.Try] = Expression.TryFinally(zero, Expression.Constant(1));
#endif
            return(map);
        }
    public void VisitUnknownNonExtension ()
    {
      var expressionNode = new SpecialExpressionNode ((ExpressionType) (-1), typeof (int));
      Expect.Call (InvokeVisitMethod ("VisitUnknownNonExtension", expressionNode)).CallOriginalMethod (OriginalCallOptions.CreateExpectation);
      MockRepository.ReplayAll();

      try
      {
        InvokeVisitMethod ("VisitUnknownNonExtension", expressionNode);
      }
      catch (TargetInvocationException ex)
      {
        throw ex.InnerException;
      }
    }
    private static Dictionary<ExpressionType, Expression> InitializeExpressionTypeMap ()
    {
      var map = new Dictionary<ExpressionType, Expression> ();

      Expression zero = Expression.Constant (0);
      Expression dateTimeValue = Expression.Constant (DateTime.MinValue);
      Expression zeroDouble = Expression.Constant (0.0);
      NewArrayExpression arrayExpression = Expression.NewArrayInit (typeof (int), zero);
      Expression trueExpression = Expression.Constant (true);
      LambdaExpression lambdaExpression = Expression.Lambda<Func<int>> (zero);
      NewExpression newExpression = Expression.New (typeof (List<int>).GetConstructor (new[] { typeof (int) }), zero);

      map[ExpressionType.Add] = Expression.Add (zero, zero);
      map[ExpressionType.AddChecked] = Expression.AddChecked (zero, zero);
      map[ExpressionType.And] = Expression.And (zero, zero);
      map[ExpressionType.AndAlso] = Expression.AndAlso (trueExpression, trueExpression);
      map[ExpressionType.ArrayLength] = Expression.ArrayLength (arrayExpression);
      map[ExpressionType.ArrayIndex] = Expression.ArrayIndex (arrayExpression, zero);
      map[ExpressionType.Call] = Expression.Call (zero, typeof (int).GetMethod ("Equals", new[] { typeof (int) }), zero);
      map[ExpressionType.Coalesce] = Expression.Coalesce (arrayExpression, arrayExpression);
      map[ExpressionType.Conditional] = Expression.Condition (trueExpression, zero, zero);
      map[ExpressionType.Constant] = Expression.Constant (zero);
      map[ExpressionType.Convert] = Expression.Convert (zero, typeof (object));
      map[ExpressionType.ConvertChecked] = Expression.ConvertChecked (zero, typeof (object));
      map[ExpressionType.Divide] = Expression.Divide (zero, zero);
      map[ExpressionType.Equal] = Expression.Equal (zero, zero);
      map[ExpressionType.ExclusiveOr] = Expression.ExclusiveOr (trueExpression, trueExpression);
      map[ExpressionType.GreaterThan] = Expression.GreaterThan (zero, zero);
      map[ExpressionType.GreaterThanOrEqual] = Expression.GreaterThanOrEqual (zero, zero);
      map[ExpressionType.Invoke] = Expression.Invoke (lambdaExpression);
      map[ExpressionType.Lambda] = lambdaExpression;
      map[ExpressionType.LeftShift] = Expression.LeftShift (zero, zero);
      map[ExpressionType.LessThan] = Expression.LessThan (zero, zero);
      map[ExpressionType.LessThanOrEqual] = Expression.LessThanOrEqual (zero, zero);
      map[ExpressionType.ListInit] = Expression.ListInit (newExpression, zero);
      map[ExpressionType.MemberAccess] = Expression.MakeMemberAccess (dateTimeValue, typeof (DateTime).GetProperty ("Date"));
      map[ExpressionType.MemberInit] = Expression.MemberInit (newExpression, Expression.Bind (typeof (List<int>).GetProperty ("Capacity"), zero));
      map[ExpressionType.Modulo] = Expression.Modulo (zero, zero);
      map[ExpressionType.Multiply] = Expression.Multiply (zero, zero);
      map[ExpressionType.MultiplyChecked] = Expression.MultiplyChecked (zero, zero);
      map[ExpressionType.Negate] = Expression.Negate (zero);
      map[ExpressionType.UnaryPlus] = Expression.UnaryPlus (zero);
      map[ExpressionType.NegateChecked] = Expression.NegateChecked (zero);
      map[ExpressionType.New] = newExpression;
      map[ExpressionType.NewArrayInit] = arrayExpression;
      map[ExpressionType.NewArrayBounds] = Expression.NewArrayBounds (typeof (int), zero);
      map[ExpressionType.Not] = Expression.Not (trueExpression);
      map[ExpressionType.NotEqual] = Expression.NotEqual (zero, zero);
      map[ExpressionType.Or] = Expression.Or (trueExpression, trueExpression);
      map[ExpressionType.OrElse] = Expression.OrElse (trueExpression, trueExpression);
      map[ExpressionType.Parameter] = Expression.Parameter (typeof (object), "bla");
      map[ExpressionType.Power] = Expression.Power (zeroDouble, zeroDouble);
      map[ExpressionType.Quote] = Expression.Quote (lambdaExpression);
      map[ExpressionType.RightShift] = Expression.RightShift (zero, zero);
      map[ExpressionType.Subtract] = Expression.Subtract (zero, zero);
      map[ExpressionType.SubtractChecked] = Expression.SubtractChecked (zero, zero);
      map[ExpressionType.TypeAs] = Expression.TypeAs (zero, typeof (object));
      map[ExpressionType.TypeIs] = Expression.TypeIs (zero, typeof (object));
      map[(ExpressionType)(-1)] = new SpecialExpressionNode ((ExpressionType)(-1), typeof (int));
#if !NET_3_5
      map[ExpressionType.Block] = Expression.Block (zero);
      map[ExpressionType.DebugInfo] = Expression.DebugInfo (Expression.SymbolDocument ("test.cs"), 1, 1, 1, 1);
      map[ExpressionType.Default] = Expression.Default (typeof (int));
      map[ExpressionType.Goto] = Expression.Goto (Expression.Label(), zero);
      map[ExpressionType.Index] = Expression.MakeIndex (arrayExpression, typeof (int[]).GetProperty ("Item"), new[] { zero });
      map[ExpressionType.Label] = Expression.Label (Expression.Label(), zero);
      map[ExpressionType.Loop] = Expression.Loop (zero);
      map[ExpressionType.RuntimeVariables] = Expression.RuntimeVariables (Expression.Parameter (typeof (string)));
      map[ExpressionType.Switch] = Expression.Switch (zero, Expression.SwitchCase (Expression.Default (typeof (void)), Expression.Constant (1)));
      map[ExpressionType.Try] = Expression.TryFinally (zero, Expression.Constant (1));
#endif
      return map;
    }