public void ClassicCpsRewriterWithErrorPropagation_Basics1() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var f = (Expression <Func <int> >)(() => Calculator.Add(Calculator.Add(1, Calculator.Div(4, 2)), 3)); var g = cps.Rewrite(f); var res = default(int); g.Compile()(x => res = x, ex => Assert.Fail()); Assert.AreEqual(6, res); }
public void ClassicCpsRewriterWithErrorPropagation_Return_Func3() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var f = (Expression <Func <int> >)(() => Calculator.Return(42)); var g = (Expression <Action <Action <int>, Action <Exception> > >)cps.Rewrite(f.Body); var res = default(int); g.Compile()(x => res = x, ex => Assert.Fail()); Assert.AreEqual(42, res); }
public void ClassicCpsRewriterWithErrorPropagation_ThrowExpression() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var ex = new Exception(); var f = Expression.Throw(Expression.Constant(ex), typeof(int)); var g = (Expression <Action <Action <int>, Action <Exception> > >)cps.Rewrite(f); var err = default(Exception); g.Compile()(x => Assert.Fail(), e => err = e); Assert.AreSame(ex, err); }
public void ClassicCpsRewriterWithErrorPropagation_Throw_Action3() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var ex = new Exception(); var f = (Expression <Action>)(() => Calculator.Throw(ex)); var g = (Expression <Action <Action, Action <Exception> > >)cps.Rewrite(f.Body); var err = default(Exception); g.Compile()(() => Assert.Fail(), e => err = e); Assert.AreSame(ex, err); }
public void ClassicCpsRewriterWithErrorPropagation_Func4() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var foo = new Foo(); var f = (Expression <Func <int, int, int, int> >)((x, y, z) => foo.Bar(x, y, z)); var g = cps.Rewrite(f); var res = default(int); g.Compile()(19, 16, 7, x => res = x, ex => Assert.Fail()); Assert.AreEqual(42, res); }
public void ClassicCpsRewriterWithErrorPropagation_Basics3() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var bar = new Bar(); var f = (Expression <Func <int, int> >)(x => bar.I(x)); var g = (Expression <Action <int, Action <int>, Action <Exception> > >)cps.Rewrite((LambdaExpression)f); var res = default(int); g.Compile()(42, x => res = x, ex => Assert.Fail()); Assert.AreEqual(42, res); }
public void ClassicCpsRewriterWithErrorPropagation_Action4() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var foo = new Foo(); var f = (Expression <Action <int, int, int> >)((x, y, z) => foo.Qux(x, y, z)); var g = cps.Rewrite(f); var done = default(bool); g.Compile()(19, 16, 7, () => done = true, ex => Assert.Fail()); Assert.IsTrue(done); Assert.AreEqual(42, foo.Qux3); }
public void ClassicCpsRewriterWithErrorPropagation_Return_Func5() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var res = default(int); var set = new Action <int>(x => res = x); var onNext = (Expression <Action <int> >)(x => set(x)); var onError = (Expression <Action <Exception> >)(ex => Assert.Fail()); var f = (Expression <Func <int> >)(() => Calculator.Return(42)); var g = Expression.Lambda <Action>(cps.Rewrite(f, onNext, onError)); g.Compile()(); Assert.AreEqual(42, res); }
public void ClassicCpsRewriterWithErrorPropagation_Throw_Action5() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var err = default(Exception); var set = new Action <Exception>(e => err = e); var onNext = (Expression <Action>)(() => Assert.Fail()); var onError = (Expression <Action <Exception> >)(e => set(e)); var ex = new Exception(); var f = (Expression <Action>)(() => Calculator.Throw(ex)); var g = Expression.Lambda <Action>(cps.Rewrite(f.Body, onNext, onError)); g.Compile()(); Assert.AreSame(ex, err); }
public void ClassicCpsRewriterWithErrorPropagation_Basics2() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var done = false; var complete = new Action(() => done = true); var bar = new Bar(); var f = (Expression <Action>)(() => bar.Foo()); var s = (Expression <Action>)(() => complete()); var e = (Expression <Action <Exception> >)(err => Assert.Fail()); var g = Expression.Lambda <Action>(cps.Rewrite(f, s, e)); g.Compile()(); Assert.IsTrue(done); Assert.IsTrue(bar.HasFood); }
public void ClassicCpsRewriterWithErrorPropagation_Basics4() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var bar = new Bar(); var f = (Expression <Action <int> >)(x => bar.J(x)); var g = (Expression <Action <int, Action, Action <Exception> > >)cps.Rewrite((LambdaExpression)f); var h = g.Compile(); var done = default(bool); var err = default(Exception); h(1, () => done = true, ex => Assert.Fail()); h(0, () => Assert.Fail(), ex => err = ex); Assert.IsTrue(done); Assert.IsTrue(err is DivideByZeroException); }
public void ClassicCpsRewriterWithErrorPropagation_Basics7() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var log = new List <int>(); var getX = new Func <bool>(() => { log.Add(1); return(true); }); var getY = new Func <string>(() => { log.Add(2); return("Hello"); }); var ex = new Exception(); var getZ = new Func <int>(() => { log.Add(3); throw ex; }); var f = (Expression <Func <int> >)(() => Calculator.Add(getX() ? 1 : 0, Calculator.Div(getZ(), getY().Length))); var g = cps.Rewrite(f); var err = default(Exception); g.Compile()(x => Assert.Fail(), e => err = e); Assert.AreSame(ex, err); Assert.IsTrue(new[] { 1, 3 }.SequenceEqual(log)); }
public void ClassicCpsRewriterWithErrorPropagation_Basics5() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var log = new List <int>(); var getX = new Func <bool>(() => { log.Add(1); return(true); }); var getY = new Func <string>(() => { log.Add(2); return("Hello"); }); var getZ = new Func <int>(() => { log.Add(3); return(42); }); var bar = new Bar(); var f = (Expression <Func <long> >)(() => bar.K(getX(), getY(), getZ())); var g = cps.Rewrite(f); var res = default(long); g.Compile()(x => res = x, ex => Assert.Fail()); Assert.AreEqual(1 + 42 / 5, res); Assert.IsTrue(new[] { 1, 2, 3 }.SequenceEqual(log)); }
public void ClassicCpsRewriterWithErrorPropagation_Basics6() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var log = new List <int>(); var getX = new Func <bool>(() => { log.Add(1); return(true); }); var getY = new Func <string>(() => { log.Add(2); return("Hello"); }); var getZ = new Func <int>(() => { log.Add(3); return(42); }); var f = (Expression <Func <int> >)(() => Calculator.Add(getX() ? 1 : 0, Calculator.Div(getZ(), getY().Length))); var g = cps.Rewrite(f); var res = default(int); g.Compile()(x => res = x, ex => Assert.Fail()); Assert.AreEqual(1 + 42 / 5, res); Assert.IsTrue(new[] { 1, 3, 2 }.SequenceEqual(log)); }
public void ClassicCpsRewriterWithErrorPropagation_ArgumentChecking() { var cps = new ClassicCpsRewriterWithErrorPropagation(); var e1 = Expression.Constant(42); var f1 = (Expression <Func <int> >)(() => 42); var c1 = (Expression <Action <int> >)(x => Console.WriteLine(x)); var d1 = (Expression <Action <Exception> >)(x => Console.WriteLine(x.Message)); var f2 = (Expression <Action>)(() => Console.WriteLine("bar")); var c2 = (Expression <Action>)(() => Console.WriteLine("foo")); #pragma warning disable IDE0034 // Simplify 'default' expression (illustrative of method signature) AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(LambdaExpression)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(default(Expression <Func <int> >)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int>(default(Expression <Func <int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int, int>(default(Expression <Func <int, int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int, int, int>(default(Expression <Func <int, int, int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression <Action>)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(default(Expression <Action <int> >)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int>(default(Expression <Action <int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int, int, int>(default(Expression <Action <int, int, int> >)), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression <Action>), c2, d1), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(f2, default(Expression <Action>), d1), ex => Assert.AreEqual("success", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(f2, c2, default(Expression <Action <Exception> >)), ex => Assert.AreEqual("failure", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(default(Expression <Func <int> >), c1, d1), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(f1, default(Expression <Action <int> >), d1), ex => Assert.AreEqual("success", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite <int>(f1, c1, default(Expression <Action <Exception> >)), ex => Assert.AreEqual("failure", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(default(Expression), e1, e1), ex => Assert.AreEqual("expression", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(e1, default(Expression), e1), ex => Assert.AreEqual("success", ex.ParamName)); AssertEx.ThrowsException <ArgumentNullException>(() => cps.Rewrite(e1, e1, default(Expression)), ex => Assert.AreEqual("failure", ex.ParamName)); #pragma warning restore IDE0034 // Simplify 'default' expression }