Example #1
0
 private static IEnumerable <Expression> TryFinallyBuilder(LightExceptionTests self)
 {
     using (new DepthHolder(self)) {
         foreach (var body in self.GetStatements())
         {
             foreach (var handler in self.GetStatements())
             {
                 yield return(Expression.TryFinally(AddLogging(body, "try finally"), AddLogging(handler, "finally")));
             }
         }
     }
 }
Example #2
0
        private static IEnumerable <Expression> TryCatchBuilder(LightExceptionTests self)
        {
            using (new DepthHolder(self)) {
                foreach (var body in self.GetStatements())
                {
                    foreach (var handler in self.GetStatements())
                    {
                        for (int i = 0; i < _catchExtras.Length; i++)
                        {
                            var extra = _catchExtras[i];

                            yield return(AddLogging(
                                             Expression.TryCatch(
                                                 Expression.Block(typeof(void), body),
                                                 Expression.Catch(
                                                     typeof(Exception),
                                                     AddLogging(Expression.Block(typeof(void), handler, extra), "catch " + i)
                                                     )
                                                 ),
                                             "try"
                                             ));

                            yield return(AddLogging(
                                             Expression.TryCatch(
                                                 Expression.Block(typeof(void), body),
                                                 Expression.Catch(
                                                     typeof(InvalidOperationException),
                                                     AddLogging(Expression.Block(typeof(void), handler, extra), "invalidEx catch 1 " + i)
                                                     )
                                                 ),
                                             "try"
                                             ));

                            yield return(AddLogging(
                                             Expression.TryCatch(
                                                 Expression.Block(typeof(void), body),
                                                 Expression.Catch(
                                                     typeof(InvalidOperationException),
                                                     AddLogging(Expression.Block(typeof(void), handler, extra), "invalidEx catch 2 " + i)
                                                     ),
                                                 Expression.Catch(
                                                     typeof(InvalidOperationException),
                                                     AddLogging(Expression.Block(typeof(void), handler, extra), "catch " + i)
                                                     )
                                                 ),
                                             "try"
                                             ));
                        }
                    }
                }
            }
        }
Example #3
0
        private static IEnumerable <Expression> CallBuilder(LightExceptionTests self)
        {
            yield return(AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod("SomeCall"))), "call"));

            yield return(AddLogging(Expression.Call(typeof(LightExceptionTests).GetMethod("ThrowingCall")), "call throw"));

            yield return(AddLogging(Expression.Call(typeof(LightExceptionTests).GetMethod("ThrowingCallInvalidOp")), "call throw invalidop"));

            yield return(AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod("LightThrowingCall"))), "call throw"));

            yield return(AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod("LightThrowingCallInvalidOp"))), "call throw invalidop"));

            yield return(AddLogging(Expression.Dynamic(new LightExBinder("test", false), typeof(object), Expression.Constant(42)), "dynamic throw"));

            yield return(AddLogging(
                             Expression.Dynamic(new LightExBinder("test", false), typeof(object),
                                                Expression.Dynamic(new LightExBinder("foo", false), typeof(object), Expression.Constant(42))), "dynamic nothrow"));
        }
Example #4
0
        public static void RunTests()
        {
            var param   = Expression.Parameter(typeof(Exception), "foo");
            var lambdax = Expression.Lambda <Func <object> >(
                Expression.Block(
                    LightExceptions.RewriteExternal(
                        Expression.TryCatch(
                            Expression.Default(typeof(object)),
                            Expression.Catch(param, Expression.Default(typeof(object)))
                            )
                        ),
                    LightExceptions.RewriteExternal(
                        Expression.TryCatch(
                            Expression.Default(typeof(object)),
                            Expression.Catch(param, Expression.Default(typeof(object)))
                            )
                        )
                    )
                );

            lambdax.Compile()();
            CompilerHelpers.LightCompile(lambdax)();

            var           builder       = new LightExceptionTests();
            List <string> record        = new List <string>();
            List <string> rewriteRecord = new List <string>();
            int           testCount     = 0;

            try {
                foreach (var lambda in builder.MakeLambda())
                {
                    // run each test in normal and lightweight exception modes, make sure they have the same result
                    try {
                        object res = lambda.Compile()(record);
                        if (res != null)
                        {
                            record.Add(res.ToString());
                        }
                    } catch (Exception e) {
                        record.Add(String.Format("EXCEPTION {0}", e.GetType()));
                    }

                    try {
                        object    res = ((Expression <Func <List <string>, object> >)LightExceptions.Rewrite(lambda)).Compile()(rewriteRecord);
                        Exception e   = LightExceptions.GetLightException(res);
                        if (e != null)
                        {
                            rewriteRecord.Add(String.Format("EXCEPTION {0}", e.GetType()));
                        }
                        else if (res != null)
                        {
                            rewriteRecord.Add(res.ToString());
                        }
                    } catch (Exception e) {
                        rewriteRecord.Add(String.Format("EXCEPTION {0}", e.GetType()));
                    }

                    if (record.Count != rewriteRecord.Count)
                    {
                        PrintLambda(lambda, record, rewriteRecord);
                        throw new Exception("Records differ in length");
                    }
                    for (int i = 0; i < record.Count; i++)
                    {
                        if (record[i] != rewriteRecord[i])
                        {
                            PrintLambda(lambda, record, rewriteRecord);
                            throw new Exception("Records differ");
                        }
                    }

                    record.Clear();
                    rewriteRecord.Clear();
                    testCount++;
                }
            } finally {
                Console.Write("Ran {0} tests", testCount);
            }
        }
Example #5
0
 public DepthHolder(LightExceptionTests builder)
 {
     Builder = builder;
     builder._depth++;
 }
Example #6
0
        private static IEnumerable <Expression> ThrowBuilder(LightExceptionTests self)
        {
            yield return(AddLogging(Expression.Throw(Expression.New(typeof(Exception))), "throw ex"));

            yield return(AddLogging(Expression.Throw(Expression.New(typeof(InvalidOperationException))), "throw invalidEx"));
        }
Example #7
0
 private static IEnumerable <Expression> ReturnBuilder(LightExceptionTests self)
 {
     yield return(Expression.Return(_ret, Expression.Default(typeof(object))));
 }
 public DepthHolder(LightExceptionTests builder) {
     Builder = builder;
     builder._depth++;
 }
        public static void RunTests() {
            var param = Expression.Parameter(typeof(Exception), "foo");
            var lambdax = Expression.Lambda<Func<object>>(
                Expression.Block(
                    LightExceptions.RewriteExternal(
                        Expression.TryCatch(
                            Expression.Default(typeof(object)),
                            Expression.Catch(param, Expression.Default(typeof(object)))
                        )
                    ),
                    LightExceptions.RewriteExternal(
                        Expression.TryCatch(
                            Expression.Default(typeof(object)),
                            Expression.Catch(param, Expression.Default(typeof(object)))
                        )
                    )
                )
            );

            lambdax.Compile()();
            CompilerHelpers.LightCompile(lambdax)();

            var builder = new LightExceptionTests();
            List<string> record = new List<string>();
            List<string> rewriteRecord = new List<string>();
            int testCount = 0;
            try {                
                foreach (var lambda in builder.MakeLambda()) {
                    // run each test in normal and lightweight exception modes, make sure they have the same result
                    try {
                        
                        object res = lambda.Compile()(record);
                        if (res != null) {
                            record.Add(res.ToString());
                        }
                    } catch (Exception e) {
                        record.Add(String.Format("EXCEPTION {0}", e.GetType()));
                    }

                    try {
                        object res = ((Expression<Func<List<string>, object>>)LightExceptions.Rewrite(lambda)).Compile()(rewriteRecord);
                        Exception e = LightExceptions.GetLightException(res);
                        if (e != null) {
                            rewriteRecord.Add(String.Format("EXCEPTION {0}", e.GetType()));
                        } else if (res != null) {
                            rewriteRecord.Add(res.ToString());
                        }
                    } catch (Exception e) {
                        rewriteRecord.Add(String.Format("EXCEPTION {0}", e.GetType()));
                    }

                    if (record.Count != rewriteRecord.Count) {
                        PrintLambda(lambda, record, rewriteRecord);
                        throw new Exception("Records differ in length");
                    }
                    for (int i = 0; i < record.Count; i++) {
                        if (record[i] != rewriteRecord[i]) {
                            PrintLambda(lambda, record, rewriteRecord);
                            throw new Exception("Records differ");
                        }
                    }

                    record.Clear();
                    rewriteRecord.Clear();
                    testCount++;
                }
            } finally {
                Console.Write("Ran {0} tests", testCount);
            }
        }
 private static IEnumerable<Expression> CallBuilder(LightExceptionTests self) {
     yield return AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod("SomeCall"))), "call");
     yield return AddLogging(Expression.Call(typeof(LightExceptionTests).GetMethod("ThrowingCall")), "call throw");
     yield return AddLogging(Expression.Call(typeof(LightExceptionTests).GetMethod("ThrowingCallInvalidOp")), "call throw invalidop");
     yield return AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod("LightThrowingCall"))), "call throw");
     yield return AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod("LightThrowingCallInvalidOp"))), "call throw invalidop");
     yield return AddLogging(Expression.Dynamic(new LightExBinder("test", false), typeof(object), Expression.Constant(42)), "dynamic throw");
     yield return AddLogging(
         Expression.Dynamic(new LightExBinder("test", false), typeof(object), 
         Expression.Dynamic(new LightExBinder("foo", false), typeof(object), Expression.Constant(42))), "dynamic nothrow");
 }
 private static IEnumerable<Expression> ThrowBuilder(LightExceptionTests self) {
     yield return AddLogging(Expression.Throw(Expression.New(typeof(Exception))), "throw ex");
     yield return AddLogging(Expression.Throw(Expression.New(typeof(InvalidOperationException))), "throw invalidEx");
 }
 private static IEnumerable<Expression> TryFinallyBuilder(LightExceptionTests self) {
     using (new DepthHolder(self)) {
         foreach (var body in self.GetStatements()) {
             foreach (var handler in self.GetStatements()) {
                 yield return Expression.TryFinally(AddLogging(body, "try finally"), AddLogging(handler, "finally"));
             }
         }
     }
 }
        private static IEnumerable<Expression> TryCatchBuilder(LightExceptionTests self) {
            using (new DepthHolder(self)) {
                foreach (var body in self.GetStatements()) {
                    foreach (var handler in self.GetStatements()) {
                        for (int i = 0; i < _catchExtras.Length; i++) {
                            var extra = _catchExtras[i];

                            yield return AddLogging(
                                Expression.TryCatch(
                                    Expression.Block(typeof(void), body),
                                    Expression.Catch(
                                        typeof(Exception),
                                        AddLogging(Expression.Block(typeof(void), handler, extra), "catch " + i)
                                    )
                                ),
                                "try"
                            );

                            yield return AddLogging(
                                Expression.TryCatch(
                                    Expression.Block(typeof(void), body),
                                    Expression.Catch(
                                        typeof(InvalidOperationException),
                                        AddLogging(Expression.Block(typeof(void), handler, extra), "invalidEx catch 1 " + i)
                                    )
                                ),
                                "try"
                            );

                            yield return AddLogging(
                                Expression.TryCatch(
                                    Expression.Block(typeof(void), body),
                                    Expression.Catch(
                                        typeof(InvalidOperationException),
                                        AddLogging(Expression.Block(typeof(void), handler, extra), "invalidEx catch 2 " + i)
                                    ),
                                    Expression.Catch(
                                        typeof(InvalidOperationException),
                                        AddLogging(Expression.Block(typeof(void), handler, extra), "catch " + i)
                                    )
                                ),
                                "try"
                            );
                        }

                    }
                }
            }
        }
 private static IEnumerable<Expression> ReturnBuilder(LightExceptionTests self) {
     yield return Expression.Return(_ret, Expression.Default(typeof(object)));
 }