Exemple #1
0
        public void SwitchUpdateDoesntRepeatEnumeration()
        {
            SwitchExpression sw = Expression.Switch(
                Expression.Constant(0),
                Expression.Constant(0),
                Expression.SwitchCase(Expression.Constant(1), Expression.Constant(1)),
                Expression.SwitchCase(Expression.Constant(2), Expression.Constant(2))
                );

            IEnumerable <SwitchCase> newCases =
                new RunOnceEnumerable <SwitchCase>(
                    new SwitchCase[]
            {
                Expression.SwitchCase(Expression.Constant(1), Expression.Constant(1)),
                Expression.SwitchCase(Expression.Constant(2), Expression.Constant(2))
            });

            Assert.NotSame(sw, sw.Update(sw.SwitchValue, newCases, sw.DefaultBody));
        }
Exemple #2
0
        public void UpdateDoesntRepeatEnumeration()
        {
            MethodInfo meth = typeof(List <int>).GetMethod("Add");

            ElementInit[] inits = new[]
            {
                Expression.ElementInit(meth, Expression.Constant(1)),
                Expression.ElementInit(meth, Expression.Constant(2)),
                Expression.ElementInit(meth, Expression.Constant(3))
            };
            ListInitExpression        init     = Expression.ListInit(Expression.New(typeof(List <int>)), inits);
            IEnumerable <ElementInit> newInits = new RunOnceEnumerable <ElementInit>(
                new[]
            {
                Expression.ElementInit(meth, Expression.Constant(1)),
                Expression.ElementInit(meth, Expression.Constant(2)),
                Expression.ElementInit(meth, Expression.Constant(3))
            });

            Assert.NotSame(init, init.Update(init.NewExpression, newInits));
        }
Exemple #3
0
        private static void VerifyUpdateDifferentParamsReturnsDifferent <TDelegate>(Expression <TDelegate> lamda, ParameterExpression[] pars)
        {
            // Should try to create new lambda, but should fail as should have wrong number of arguments.
            Assert.Throws <ArgumentException>(() => lamda.Update(lamda.Body, pars.Append(Expression.Parameter(typeof(int)))));

            if (pars.Length != 0)
            {
                Assert.Throws <ArgumentException>(() => lamda.Update(lamda.Body, null));
                for (int i = 0; i != pars.Length; ++i)
                {
                    ParameterExpression[] newPars = new ParameterExpression[pars.Length];
                    pars.CopyTo(newPars, 0);
                    newPars[i] = Expression.Parameter(typeof(int));
                    Assert.NotSame(lamda, lamda.Update(lamda.Body, newPars));
                }

                IEnumerable <ParameterExpression> diffPars = new RunOnceEnumerable <ParameterExpression>(
                    Enumerable.Range(0, lamda.Parameters.Count) // Trigger Parameters collection build.
                    .Select(_ => Expression.Parameter(typeof(int))));

                Assert.NotSame(lamda, lamda.Update(lamda.Body, diffPars));
            }
        }