public void UsingException()
        {
            var disposable = new Disposable();

            var d = Expression.Parameter(typeof(Disposable), "d");

            var disposer = Expression.Lambda <Action <Disposable> > (
                CustomExpression.Using(
                    d,
                    Expression.Block(
                        Expression.Throw(Expression.New(typeof(TestUsingException))),
                        Expression.Call(d, typeof(Disposable).GetMethod("Touch")))),
                d).Compile();

            Assert.IsFalse(disposable.Touched);
            Assert.IsFalse(disposable.Disposed);

            try {
                disposer(disposable);
                Assert.Fail();
            } catch (TestUsingException) {}

            Assert.IsFalse(disposable.Touched);
            Assert.IsTrue(disposable.Disposed);
        }
Пример #2
0
        public void For()
        {
            var l = Expression.Parameter(typeof(int), "l");

            var i = Expression.Variable(typeof(int), "i");

            var lambda = Expression.Lambda <Action <int> > (
                CustomExpression.For(
                    i,
                    Expression.Constant(0),
                    Expression.LessThan(i, l),
                    Expression.PreIncrementAssign(i),
                    Expression.Call(typeof(Console).GetMethod("WriteLine", new [] { typeof(int) }), i)),
                l);

            AssertExpression(@"
void (int l)
{
	for (int i = 0; i < l; ++i)
	{
		Console.WriteLine(i);
	}
}
", lambda);
        }
		protected Expression Visit (CustomExpression node)
		{
			if (node == null)
				return null;

			return node.Accept (this);
		}
Пример #4
0
        public void ForVisit()
        {
            var i            = Expression.Variable(typeof(int), "i");
            var for_break    = Expression.Label("for_break");
            var for_continue = Expression.Label("for_continue");

            var forExpression = CustomExpression.For(
                i,
                Expression.Constant(0),
                Expression.LessThan(i, Expression.Constant(1)),
                Expression.PreIncrementAssign(i),
                Expression.Constant(null),
                for_break,
                for_continue);
            var visitedForExpression = (ForExpression)(new ReplaceNullByDefaultVisitor().Visit(forExpression));

            Assert.AreNotEqual(forExpression, visitedForExpression);

            Assert.AreEqual(forExpression.Variable, visitedForExpression.Variable);
            Assert.AreEqual(forExpression.Initializer, visitedForExpression.Initializer);
            Assert.AreEqual(forExpression.Test, visitedForExpression.Test);
            Assert.AreEqual(forExpression.Step, visitedForExpression.Step);
            Assert.AreEqual(ExpressionType.Default, visitedForExpression.Body.NodeType);
            Assert.AreEqual(forExpression.BreakTarget, visitedForExpression.BreakTarget);
            Assert.AreEqual(forExpression.ContinueTarget, visitedForExpression.ContinueTarget);
        }
Пример #5
0
        public void ForEachException()
        {
            var enumerable_counter = new EnumerableCounter(0, 100);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item = Expression.Variable(typeof(int), "i");

            var hitcounter = Expression.Lambda <Action <EnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Block(
                        Expression.Condition(
                            Expression.LessThanOrEqual(item, Expression.Constant(10)),
                            Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes)),
                            Expression.Throw(Expression.New(typeof(TestException)))))),
                ec).Compile();

            try {
                hitcounter(enumerable_counter);
                Assert.Fail();
            } catch (TestException) {}

            Assert.AreEqual(10, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
        }
Пример #6
0
        public void WhileBreakContinue()
        {
            var counter = new Counter();

            var c = Expression.Parameter(typeof(Counter), "c");
            var i = Expression.Parameter(typeof(int), "i");
            var l = Expression.Parameter(typeof(int), "l");

            var loop_break    = Expression.Label("for_break");
            var loop_continue = Expression.Label("for_continue");

            var hitcounter = Expression.Lambda <Action <Counter, int, int> > (
                CustomExpression.While(
                    Expression.LessThan(i, l),
                    Expression.Block(
                        Expression.Condition(
                            Expression.LessThan(i, Expression.Constant(10)),
                            Expression.Block(
                                Expression.Call(c, typeof(Counter).GetMethod("Hit", Type.EmptyTypes)),
                                Expression.PostIncrementAssign(i),
                                Expression.Goto(loop_continue)),
                            Expression.Goto(loop_break))),
                    loop_break,
                    loop_continue),
                c, i, l).Compile();

            hitcounter(counter, 0, 100);

            Assert.AreEqual(10, counter.Count);
        }
Пример #7
0
        public void ForEachContinue()
        {
            var enumerable_counter = new EnumerableCounter(0, 10);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item             = Expression.Variable(typeof(int), "i");
            var foreach_break    = Expression.Label("foreach_break");
            var foreach_continue = Expression.Label("foreach_continue");

            var hitcounter = Expression.Lambda <Action <EnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Block(
                        Expression.Condition(
                            Expression.Equal(Expression.Modulo(item, Expression.Constant(2)), Expression.Constant(0)),
                            Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes)),
                            Expression.Goto(foreach_continue))),
                    foreach_break,
                    foreach_continue),
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.AreEqual(5, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
        }
Пример #8
0
        public void ForEachBreakAfterUpdate()
        {
            var enumerable_counter = new EnumerableCounter(0, 100);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item             = Expression.Variable(typeof(int), "i");
            var foreach_break    = Expression.Label("foreach_break");
            var foreach_continue = Expression.Label("foreach_continue");
            var foreach_body     = Expression.Block(
                Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes)),
                Expression.IfThen(
                    Expression.GreaterThanOrEqual(item, Expression.Constant(10)),
                    Expression.Goto(foreach_break)
                    ));

            var foreach_expr = CustomExpression.ForEach(
                item,
                ec,
                foreach_body,
                foreach_break,
                foreach_continue);

            foreach_expr = foreach_expr.Update(item, ec, foreach_body, foreach_break, null);
            var hitcounter = Expression.Lambda <Action <EnumerableCounter> >(
                foreach_expr,
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.AreEqual(10, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
        }
        public void ForBreak()
        {
            var counter = new Counter();

            var c = Expression.Parameter(typeof(Counter), "c");
            var l = Expression.Parameter(typeof(int), "l");

            var i         = Expression.Variable(typeof(int), "i");
            var for_break = Expression.Label("for_break");

            var hitcounter = Expression.Lambda <Action <Counter, int> > (
                CustomExpression.For(
                    i,
                    Expression.Constant(0),
                    Expression.LessThan(i, l),
                    Expression.PreIncrementAssign(i),
                    Expression.Block(
                        Expression.Condition(
                            Expression.LessThan(i, Expression.Constant(10)),
                            Expression.Call(c, typeof(Counter).GetMethod("Hit", Type.EmptyTypes)),
                            Expression.Goto(for_break))),
                    for_break),
                c, l).Compile();

            hitcounter(counter, 100);

            Assert.AreEqual(10, counter.Count);
        }
Пример #10
0
        protected Expression Visit(CustomExpression node)
        {
            if (node == null)
            {
                return(null);
            }

            return(node.Accept(this));
        }
Пример #11
0
        public DoWhileExpression Update(Expression test, Expression body, LabelTarget breakTarget, LabelTarget continueTarget)
        {
            if (this.test == test && this.body == body && this.break_target == breakTarget && this.continue_target == continueTarget)
            {
                return(this);
            }

            return(CustomExpression.DoWhile(test, body, breakTarget, continueTarget));
        }
Пример #12
0
        public ForExpression Update(ParameterExpression variable, Expression initializer, Expression test, Expression step, Expression body, LabelTarget breakTarget, LabelTarget continueTarget)
        {
            if (this.variable == variable && this.initializer == initializer && this.test == test && this.step == step && this.body == body && this.break_target == breakTarget && this.continue_target == continueTarget)
            {
                return(this);
            }

            return(CustomExpression.For(variable, initializer, test, step, body, breakTarget, continueTarget));
        }
Пример #13
0
        public static bool Is(this CustomExpression self, CustomExpressionType type)
        {
            if (self == null)
            {
                throw new ArgumentNullException("self");
            }

            return(self.CustomNodeType == type);
        }
        public ForEachExpression Update(ParameterExpression variable, Expression enumerable, Expression body, LabelTarget breakTarget, LabelTarget continueTarget)
        {
            if (this.variable == variable && this.enumerable == enumerable && this.body == body && break_target == breakTarget && continue_target == continueTarget)
            {
                return(this);
            }

            return(CustomExpression.ForEach(variable, enumerable, body, continueTarget, breakTarget));
        }
        public UsingExpression Update(ParameterExpression variable, Expression disposable, Expression body)
        {
            if (this.variable == variable && this.disposable == disposable && this.body == body)
            {
                return(this);
            }

            return(CustomExpression.Using(variable, disposable, body));
        }
Пример #16
0
        static bool IsStatement(CustomExpression expression)
        {
            switch (expression.CustomNodeType)
            {
            case CustomExpressionType.DoWhileExpression:
            case CustomExpressionType.ForExpression:
            case CustomExpressionType.ForEachExpression:
            case CustomExpressionType.UsingExpression:
            case CustomExpressionType.WhileExpression:
                return(true);

            default:
                return(false);
            }
        }
Пример #17
0
        public void ForEachNonGeneric()
        {
            var enumerable_counter = new NonGenericEnumerableCounter(0, 10);

            var ec = Expression.Parameter(typeof(NonGenericEnumerableCounter), "ec");

            var item = Expression.Variable(typeof(int), "i");

            var hitcounter = Expression.Lambda <Action <NonGenericEnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Call(ec, typeof(NonGenericEnumerableCounter).GetMethod("Hit", Type.EmptyTypes))),
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.AreEqual(10, enumerable_counter.Count);
        }
Пример #18
0
        public void Using()
        {
            var arg = Expression.Parameter(typeof(IDisposable), "arg");

            var lambda = Expression.Lambda <Action <IDisposable> > (
                CustomExpression.Using(
                    arg,
                    Expression.Call(typeof(Console).GetMethod("WriteLine", new [] { typeof(object) }), arg)),
                arg);

            AssertExpression(@"
void (IDisposable arg)
{
	using (arg)
	{
		Console.WriteLine(arg);
	}
}
", lambda);
        }
Пример #19
0
        public void ForEachEnumerable()
        {
            var enumerable_counter = new EnumerableCounter(0, 10);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item = Expression.Variable(typeof(int), "i");

            var hitcounter = Expression.Lambda <Action <EnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes))),
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.AreEqual(10, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
            Assert.IsFalse(enumerable_counter.DisposedIncorrectly);
        }
        public void Using()
        {
            var disposable = new Disposable();

            var d = Expression.Parameter(typeof(Disposable), "d");

            var disposer = Expression.Lambda <Action <Disposable> > (
                CustomExpression.Using(
                    d,
                    Expression.Block(
                        Expression.Call(d, typeof(Disposable).GetMethod("Touch")))),
                d).Compile();

            Assert.IsFalse(disposable.Touched);
            Assert.IsFalse(disposable.Disposed);

            disposer(disposable);

            Assert.IsTrue(disposable.Touched);
            Assert.IsTrue(disposable.Disposed);
        }
Пример #21
0
        public void WhileFalse()
        {
            var counter = new Counter();

            var c = Expression.Parameter(typeof(Counter), "c");
            var i = Expression.Parameter(typeof(int), "i");
            var l = Expression.Parameter(typeof(int), "l");

            var hitcounter = Expression.Lambda <Action <Counter, int, int> > (
                CustomExpression.While(
                    Expression.LessThan(i, l),
                    Expression.Block(
                        Expression.Call(c, typeof(Counter).GetMethod("Hit", Type.EmptyTypes)),
                        Expression.PreIncrementAssign(i))
                    ),
                c, i, l).Compile();

            hitcounter(counter, 100, 10);

            Assert.AreEqual(0, counter.Count);
        }
Пример #22
0
        public void Never()
        {
            var counter = new Counter();

            var c = Expression.Parameter(typeof(Counter), "c");
            var l = Expression.Parameter(typeof(int), "l");

            var i = Expression.Variable(typeof(int), "i");

            var hitcounter = Expression.Lambda <Action <Counter, int> > (
                CustomExpression.For(
                    i,
                    Expression.Constant(0),
                    Expression.LessThan(i, l),
                    Expression.PreIncrementAssign(i),
                    Expression.Call(c, typeof(Counter).GetMethod("Hit", Type.EmptyTypes))),
                c, l).Compile();

            hitcounter(counter, 0);

            Assert.AreEqual(0, counter.Count);
        }
Пример #23
0
        public void ForEachArray()
        {
            var counters = Enumerable.Range(0, 3).Select(_ => new Counter()).ToArray();

            var cs = Expression.Parameter(typeof(Counter[]), "cs");

            var c = Expression.Variable(typeof(Counter), "i");

            var hitcounter = Expression.Lambda <Action <Counter[]> > (
                CustomExpression.ForEach(
                    c,
                    cs,
                    Expression.Call(c, typeof(Counter).GetMethod("Hit", Type.EmptyTypes))),
                cs).Compile();

            hitcounter(counters);

            foreach (var counter in counters)
            {
                Assert.AreEqual(1, counter.Count);
            }
        }
Пример #24
0
        public void While()
        {
            var i = Expression.Parameter(typeof(int), "i");
            var l = Expression.Parameter(typeof(int), "l");

            var lambda = Expression.Lambda <Action <int, int> > (
                CustomExpression.While(
                    Expression.LessThan(i, l),
                    Expression.Block(
                        Expression.Call(typeof(Console).GetMethod("WriteLine", new [] { typeof(int) }), Expression.PostIncrementAssign(i)))),
                i, l);

            AssertExpression(@"
void (int i, int l)
{
	while (i < l)
	{
		Console.WriteLine(i++);
	}
}
", lambda);
        }
Пример #25
0
        public void ForEachNonGenericStructEnumerator()
        {
            var counters           = new [] { new Counter(), new Counter(), new Counter() };
            var enumerable_counter = new NonGenericStructEnumerableCounter(counters);

            var ec   = Expression.Parameter(typeof(NonGenericStructEnumerableCounter), "ec");
            var item = Expression.Variable(typeof(Counter), "c");

            var hitcounter = Expression.Lambda <Action <NonGenericStructEnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Call(item, "Hit", Type.EmptyTypes)),
                ec).Compile();

            hitcounter(enumerable_counter);

            foreach (var counter in counters)
            {
                Assert.AreEqual(1, counter.Count);
                Assert.IsFalse(counter.Disposed);
            }
        }
Пример #26
0
        public void ForEach()
        {
            var args = Expression.Parameter(typeof(string []), "args");

            var s = Expression.Variable(typeof(string), "s");

            var lambda = Expression.Lambda <Action <string []> > (
                CustomExpression.ForEach(
                    s,
                    args,
                    Expression.Call(typeof(Console).GetMethod("WriteLine", new [] { typeof(string) }), s)),
                args);

            AssertExpression(@"
void (string[] args)
{
	foreach (string s in args)
	{
		Console.WriteLine(s);
	}
}
", lambda);
        }
Пример #27
0
 protected Expression Visit(CustomExpression node)
 {
     return(node?.Accept(this));
 }