PageRenderTime 74ms CodeModel.GetById 16ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/ETScenarios/ControlFlow/Throw.cs

#
C# | 399 lines | 312 code | 77 blank | 10 comment | 0 complexity | 4a6dd9f417c31cce96603217cc372679 MD5 | raw file
  1#if !CLR2
  2using System.Linq.Expressions;
  3#else
  4using Microsoft.Scripting.Ast;
  5#endif
  6
  7using System;
  8using System.Collections.Generic;
  9using System.Text;
 10using System.Reflection;
 11using MsSc = System.Dynamic;
 12
 13namespace ETScenarios.ControlFlow {
 14    using EU = ETUtils.ExpressionUtils;
 15    using Expr = Expression;
 16    using AstUtils = Microsoft.Scripting.Ast.Utils;
 17
 18    public class Throw {
 19
 20        private static Expr TE(Type t, string Message) {
 21            ConstructorInfo ci = t.GetConstructor(new Type[] { typeof(String) });
 22            Expression Ex = Expr.New(ci, Expr.Constant(Message));
 23            return Ex;
 24        }
 25
 26
 27        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 1", new string[] { "positive", "throw", "controlflow", "Pri1" })]
 28        public static Expr Throw1(EU.IValidator V) {
 29            List<Expression> Expressions = new List<Expression>();
 30            ParameterExpression Result = Expr.Variable(typeof(string), "");
 31
 32            Expr Body = Expr.Throw(TE(typeof(RankException), "Well, duh!"));
 33
 34            CatchBlock Catch = Expr.Catch(typeof(RankException), Expr.Block(EU.ConcatEquals(Result, "NullThrow"), Expr.Throw(null)));
 35
 36            Expr Body2 = Expr.TryCatch(Body, Catch);
 37
 38            CatchBlock Catch2 = Expr.Catch(typeof(RankException), EU.BlockVoid(EU.ConcatEquals(Result, "Caught")));
 39
 40            Expressions.Add(Expr.TryCatch(Body2, Catch2));
 41            Expressions.Add(EU.GenAreEqual(Expr.Constant("NullThrowCaught"), Result, "Throw 1"));
 42
 43            var tree = EU.BlockVoid(new[] { Result }, Expressions);
 44            V.Validate(tree);
 45            return tree;
 46        }
 47
 48        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 2", new string[] { "negative", "throw", "controlflow", "Pri1" }, Exception = typeof(ArgumentException))]
 49        public static Expr Throw2(EU.IValidator V) {
 50            return EU.Throws<ArgumentException>(() => { Expression.Throw(Expression.Constant(5)); });
 51
 52        }
 53        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 3", new string[] { "positive", "throw", "controlflow", "Pri1" })]
 54        public static Expr Throw3(EU.IValidator V) {
 55            List<Expression> Expressions = new List<Expression>();
 56            ParameterExpression Result = Expr.Variable(typeof(string), "");
 57
 58            Expr OhMy = Expr.Block(Expr.Throw(TE(typeof(FormatException), "")), TE(typeof(RankException), ""));
 59
 60            Expr Body = Expr.Throw(OhMy);
 61
 62            CatchBlock Catch = Expr.Catch(typeof(FormatException), EU.BlockVoid(EU.ConcatEquals(Result, "Caught")));
 63
 64            Expressions.Add(Expr.TryCatch(Body, Catch));
 65            Expressions.Add(EU.GenAreEqual(Expr.Constant("Caught"), Result, "Throw 1"));
 66
 67            var tree = EU.BlockVoid(new[] { Result }, Expressions);
 68            V.Validate(tree);
 69            return tree;
 70        }
 71
 72        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 4", new string[] { "positive", "throw", "controlflow", "Pri1" })]
 73        public static Expr Throw4(EU.IValidator V) {
 74            List<Expression> Expressions = new List<Expression>();
 75            ParameterExpression Result = Expr.Variable(typeof(string), "");
 76
 77            Expr OhMy = Expr.Constant(null, typeof(RankException));
 78
 79            Expr Body = Expr.Throw(OhMy);
 80
 81            CatchBlock Catch = Expr.Catch(typeof(NullReferenceException), EU.BlockVoid(EU.ConcatEquals(Result, "Caught")));
 82
 83            Expressions.Add(Expr.TryCatch(Body, Catch));
 84            Expressions.Add(EU.GenAreEqual(Expr.Constant("Caught"), Result, "Throw 1"));
 85
 86            var tree = EU.BlockVoid(new[] { Result }, Expressions);
 87            V.Validate(tree);
 88            return tree;
 89        }
 90
 91        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 5", new string[] { "negative", "throw", "controlflow", "Pri1" }, Exception = typeof(ArgumentException))]
 92        public static Expr Throw5(EU.IValidator V) {
 93            List<Expression> Expressions = new List<Expression>();
 94            ParameterExpression Result = Expr.Variable(typeof(string), "");
 95
 96            Expr OhMy = EU.BlockVoid(Expr.Empty());
 97
 98            Expr Body =
 99            EU.Throws<System.ArgumentException>(() =>
100            {
101                Expr.Throw(OhMy);
102            });
103
104            return Expr.Empty();
105        }
106
107        // pass a scope as the expression to throw
108        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 6", new string[] { "positive", "throw", "controlflow", "Pri2" })]
109        public static Expr Throw6(EU.IValidator V) {
110            List<Expression> Expressions = new List<Expression>();
111            ParameterExpression Result = Expr.Variable(typeof(string), "");
112            ParameterExpression ScopedVal = Expr.Variable(typeof(Int32), "");
113
114            BlockExpression ToThrow = Expr.Block(
115                        new ParameterExpression[] { ScopedVal },
116                        EU.ConcatEquals(Result, "ToThrow"),
117                        Expr.Assign(ScopedVal, Expr.Constant(5)),
118                        Expr.Assign(ScopedVal, Expr.Add(ScopedVal, Expr.Constant(3))),
119                        EU.GenAreEqual(ScopedVal, Expr.Constant(8)),
120                        TE(typeof(RankException), "")
121                    );
122
123            Expr Body = Expr.Throw(ToThrow, typeof(void));
124
125            CatchBlock Catch = Expr.Catch(typeof(RankException), EU.BlockVoid(EU.ConcatEquals(Result, "Caught")));
126
127            Expressions.Add(Expr.TryCatch(Body, Catch));
128
129            Expressions.Add(EU.GenAreEqual(Expr.Constant("ToThrowCaught"), Result, "Throw 1"));
130
131            var tree = EU.BlockVoid(new ParameterExpression[] { Result }, Expressions);
132            V.Validate(tree);
133            return tree;
134        }
135
136        // throw and catch a non-exception object
137        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 7", new string[] { "positive", "throw", "controlflow", "Pri2" })]
138        public static Expr Throw7(EU.IValidator V) {
139            List<Expression> Expressions = new List<Expression>();
140
141            ParameterExpression Result = Expr.Variable(typeof(string), "Result");
142
143            Expr tree =
144                Expr.TryCatch(
145                    Expr.Block(
146                        EU.ConcatEquals(Result, Expr.Constant("BeforeThrow")),
147                        Expr.Throw(Expr.Constant("StringException")),
148                        EU.ConcatEquals(Result, Expr.Constant("AfterThrow"))
149                    ),
150                    new CatchBlock[] {
151                        Expr.Catch(typeof(DivideByZeroException), EU.ConcatEquals(Result, Expr.Constant("Bad"))),
152                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad"))),
153                        Expr.Catch(typeof(string), EU.ConcatEquals(Result, Expr.Constant("StringCaught")))
154                    }
155                );
156
157            Expressions.Add(tree);
158            Expressions.Add(EU.GenAreEqual(Expr.Constant("BeforeThrowStringCaught"), Result, "Throw 1"));
159
160            Expressions.Add(Expr.Assign(Result, Expr.Constant("")));
161
162            ParameterExpression Ex = Expr.Variable(typeof(string), "Result");
163
164            // let the non-exception object get caught by an outer catch
165            Expr tree2 =
166                Expr.TryCatch(
167                    Expr.Block(
168                        EU.ConcatEquals(Result, Expr.Constant("A")),
169                        Expr.TryCatch(
170                            Expr.Block(
171                                EU.ConcatEquals(Result, Expr.Constant("B")),
172                                Expr.Throw(Expr.Constant("StringException")),
173                                EU.ConcatEquals(Result, Expr.Constant("C"))
174                            ),
175                            Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("InnerCatch")))
176                        ),
177                        EU.ConcatEquals(Result, Expr.Constant("D"))
178                    ),
179                    new CatchBlock[] {
180                        Expr.Catch(typeof(DivideByZeroException), EU.ConcatEquals(Result, Expr.Constant("Bad"))),
181                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad"))),
182                        Expr.Catch(
183                            Ex,
184                            Expr.Block(
185                                EU.ConcatEquals(Result, Expr.Constant("StringCaught")),
186                                EU.ConcatEquals(Result, Ex)
187                            )
188                        )
189                    }
190                );
191
192            Expressions.Add(tree2);
193            Expressions.Add(EU.GenAreEqual(Expr.Constant("ABStringCaughtStringException"), Result, "Throw 1"));
194
195            var FinalTree = Expr.Block(new[] { Result, Ex }, Expressions);
196            V.Validate(FinalTree);
197            return FinalTree;
198        }
199
200//RuntimeWrappedException is inaccessible on Silverlight
201#if !SILVERLIGHT
202        // throw a non-exception object
203        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 8", new string[] { "negative", "throw", "controlflow", "Pri2" }, Exception = typeof(System.Runtime.CompilerServices.RuntimeWrappedException))]
204        public static Expr Throw8(EU.IValidator V) {
205            List<Expression> Expressions = new List<Expression>();
206
207            ParameterExpression Result = Expr.Variable(typeof(string), "Result");
208
209            Expr tree =
210                Expr.TryCatch(
211                    Expr.Block(
212                        EU.ConcatEquals(Result, Expr.Constant("BeforeThrow")),
213                        Expr.Throw(Expr.Constant("StringException"), typeof(string)),
214                        EU.ConcatEquals(Result, Expr.Constant("AfterThrow"))
215                    ),
216                    new CatchBlock[] {
217                        Expr.Catch(typeof(DivideByZeroException), EU.ConcatEquals(Result, Expr.Constant("Bad"))),
218                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad")))
219                    }
220                );
221
222            Expressions.Add(tree);
223            Expressions.Add(EU.GenAreEqual(Expr.Constant("BeforeThrowStringCaught"), Result, "Throw 1"));
224
225            var FinalTree = Expr.Block(new[] { Result }, Expressions);
226            V.ValidateException<System.Runtime.CompilerServices.RuntimeWrappedException>(FinalTree, false);
227            return FinalTree;
228        }
229#endif
230
231
232        // throw a value type
233        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 9", new string[] { "negative", "throw 9", "controlflow", "Pri1" }, Exception = typeof(ArgumentException))]
234        public static Expr Throw9(EU.IValidator V) {
235            List<Expression> Expressions = new List<Expression>();
236
237            ParameterExpression Result = Expr.Variable(typeof(string), "Result");
238            ParameterExpression Ex = Expr.Variable(typeof(int), "Result");
239
240            Expr tree =
241                EU.Throws<System.ArgumentException>(() =>
242                {
243                    Expr.TryCatch(
244                        Expr.Block(
245                            EU.ConcatEquals(Result, Expr.Constant("A")),
246                            Expr.Throw(Expr.Constant(1)),
247                            EU.ConcatEquals(Result, Expr.Constant("B"))
248                        ),
249                        new CatchBlock[] {
250	                        Expr.Catch(typeof(DivideByZeroException), EU.ConcatEquals(Result, Expr.Constant("Bad"))),
251	                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad"))),
252	                        Expr.Catch(
253	                            Ex,
254	                            Expr.Block(
255	                                EU.ConcatEquals(Result, Expr.Constant("IntCaught")),
256	                                EU.ConcatEquals(Result, Ex)
257	                            )
258	                        )
259	                    }
260                    );
261                });
262
263            Expressions.Add(tree);
264
265            return Expr.Empty();
266        }
267
268        public class MyClass {
269            public string Data;
270            public MyClass(string x) { Data = x; }
271        }
272
273        // throw and catch a user defined non-exception object
274        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 10", new string[] { "positive", "throw", "controlflow", "Pri2" })]
275        public static Expr Throw10(EU.IValidator V) {
276            List<Expression> Expressions = new List<Expression>();
277
278            ParameterExpression Result = Expr.Variable(typeof(string), "Result");
279            ParameterExpression Ex = Expr.Variable(typeof(MyClass), "Ex");
280
281            Expr tree =
282                Expr.TryCatch(
283                    Expr.Block(
284                        EU.ConcatEquals(Result, Expr.Constant("BeforeThrow")),
285                        Expr.Throw(Expr.Constant(new MyClass("SomeValue"))),
286                        EU.ConcatEquals(Result, Expr.Constant("AfterThrow"))
287                    ),
288                    new CatchBlock[] {
289                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad"))),
290                        Expr.Catch(typeof(string), EU.ConcatEquals(Result, Expr.Constant("StringCaught"))),
291                        Expr.Catch(Ex, EU.ConcatEquals(Result, Expr.Constant("MyClassCaught")))
292                    }
293                );
294
295            Expressions.Add(tree);
296            Expressions.Add(EU.GenAreEqual(Expr.Constant("BeforeThrowMyClassCaught"), Result, "Throw 1"));
297
298            var FinalTree = Expr.Block(new[] { Result, Ex }, Expressions);
299            V.Validate(FinalTree);
300            return FinalTree;
301        }
302
303        public struct MyStruct {
304            public string Data;
305            public MyStruct(string x) { Data = x; }
306        }
307
308        // throw and catch a user defined non-exception object
309        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 11", new string[] { "negative", "throw", "controlflow", "Pri2" }, Exception = typeof(ArgumentException))]
310        public static Expr Throw11(EU.IValidator V) {
311            List<Expression> Expressions = new List<Expression>();
312
313            ParameterExpression Result = Expr.Variable(typeof(string), "Result");
314            ParameterExpression Ex = Expr.Variable(typeof(MyStruct), "Ex");
315
316            Expr tree =
317                EU.Throws<System.ArgumentException>(() =>
318                {
319                    Expr.TryCatch(
320                        Expr.Block(
321                            EU.ConcatEquals(Result, Expr.Constant("BeforeThrow")),
322                            Expr.Throw(Expr.Constant(new MyStruct("SomeValue"))),
323                            EU.ConcatEquals(Result, Expr.Constant("AfterThrow"))
324                        ),
325                        new CatchBlock[] {
326	                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad"))),
327	                        Expr.Catch(typeof(string), EU.ConcatEquals(Result, Expr.Constant("StringCaught"))),
328	                        Expr.Catch(Ex, EU.ConcatEquals(Result, Expr.Constant("MyStructCaught")))
329	                    }
330                    );
331                });
332
333            Expressions.Add(tree);
334            Expressions.Add(EU.GenAreEqual(Expr.Constant("BeforeThrowMyClassCaught"), Result, "Throw 1"));
335
336            return Expr.Block(new[] { Result, Ex }, Expressions);
337        }
338
339        // throw a derived class of object and catch it as typeof(object)
340        [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Throw 12", new string[] { "positive", "throw 12", "controlflow", "Pri1" })]
341        public static Expr Throw12(EU.IValidator V) {
342            List<Expression> Expressions = new List<Expression>();
343
344            ParameterExpression Result = Expr.Variable(typeof(string), "Result");
345
346            Expr tree =
347                Expr.TryCatch(
348                    Expr.Block(
349                        EU.ConcatEquals(Result, Expr.Constant("A")),
350                        Expr.Throw(Expr.Constant(new List<int> { 1, 2, 3 })),
351                        EU.ConcatEquals(Result, Expr.Constant("B"))
352                    ),
353                    new CatchBlock[] {
354                        Expr.Catch(typeof(DivideByZeroException), EU.ConcatEquals(Result, Expr.Constant("Bad"))),
355                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad"))),
356                        Expr.Catch(typeof(Object), EU.ConcatEquals(Result, Expr.Constant("ObjectCaught")))
357                    }
358                );
359
360            Expressions.Add(tree);
361            Expressions.Add(EU.GenAreEqual(Expr.Constant("AObjectCaught"), Result, "Throw 1"));
362
363            // throw an int cast to object
364            ParameterExpression Ex = Expr.Variable(typeof(object), "Ex");
365            ParameterExpression TestValue = Expr.Variable(typeof(int), "TestValue");
366
367            Expressions.Add(Expr.Assign(TestValue, Expr.Constant(2)));
368            Expressions.Add(Expr.Assign(Result, Expr.Constant("")));
369
370            Expr tree2 =
371                Expr.TryCatch(
372                    Expr.Block(
373                        EU.ConcatEquals(Result, Expr.Constant("A")),
374                        Expr.Throw(Expr.Convert(Expr.Constant(3), typeof(object))),
375                        EU.ConcatEquals(Result, Expr.Constant("B"))
376                    ),
377                    new CatchBlock[] {
378                        Expr.Catch(typeof(DivideByZeroException), EU.ConcatEquals(Result, Expr.Constant("Bad"))),
379                        Expr.Catch(typeof(Exception), EU.ConcatEquals(Result, Expr.Constant("AlsoBad"))),
380                        Expr.Catch(
381                            Ex,
382                            Expr.Block(
383                                Expr.Assign(TestValue, Expr.Add(TestValue, Expr.Unbox(Ex, typeof(int)))),
384                                EU.ConcatEquals(Result, Expr.Constant("ObjectCaught"))
385                            )
386                        )
387                    }
388                );
389
390            Expressions.Add(tree2);
391            Expressions.Add(EU.GenAreEqual(Expr.Constant("AObjectCaught"), Result, "Throw 2"));
392            Expressions.Add(EU.GenAreEqual(Expr.Constant(5), TestValue, "Throw 3"));
393
394            var FinalTree = Expr.Block(new[] { Result, TestValue }, Expressions);
395            V.Validate(FinalTree);
396            return FinalTree;
397        }
398    }
399}