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> CallBuilder(LightExceptionTests self) { yield return(AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod(nameof(LightExceptionTests.SomeCall)))), "call")); yield return(AddLogging(Expression.Call(typeof(LightExceptionTests).GetMethod(nameof(LightExceptionTests.ThrowingCall))), "call throw")); yield return(AddLogging(Expression.Call(typeof(LightExceptionTests).GetMethod(nameof(LightExceptionTests.ThrowingCallInvalidOp))), "call throw invalidop")); yield return(AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod(nameof(LightExceptionTests.LightThrowingCall)))), "call throw")); yield return(AddLogging(LightExceptions.CheckAndThrow(Expression.Call(typeof(LightExceptionTests).GetMethod(nameof(LightExceptionTests.LightThrowingCallInvalidOp)))), "call throw invalidop")); yield return(AddLogging(DynamicExpression.Dynamic(new LightExBinder("test", false), typeof(object), Expression.Constant(42)), "dynamic throw")); yield return(AddLogging( DynamicExpression.Dynamic(new LightExBinder("test", false), typeof(object), DynamicExpression.Dynamic(new LightExBinder("foo", false), typeof(object), Expression.Constant(42))), "dynamic nothrow")); }
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); } }
public DepthHolder(LightExceptionTests builder) { Builder = builder; builder._depth++; }
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> ReturnBuilder(LightExceptionTests self) { yield return(Expression.Return(_ret, Expression.Default(typeof(object)))); }