PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

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