1// exception.as - MTASC testcase for try/catch 2// 3// Copyright (C) 2005, 2006, 2007, 2009, 2010 Free Software 4// Foundation, Inc 5// 6// This program is free software; you can redistribute it and/or modify 7// it under the terms of the GNU General Public License as published by 8// the Free Software Foundation; either version 3 of the License, or 9// (at your option) any later version. 10// 11// This program is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15// 16// You should have received a copy of the GNU General Public License 17// along with this program; if not, write to the Free Software 18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19// 20// 21// Original author: Asger Ottar Alstrup <asger@area9.dk> 22// 23 24#include "check.as" 25 26// This movie tests exceptions 27 28class Test 29{ 30 31 // constructor 32 function Test() 33 { 34 note("Test constructor called"); 35 } 36 37 function addOneOnFinal(o) 38 { 39 try { 40 return 'try'; 41 } 42 finally { 43 o.num += 1; 44 return 'finally'; 45 } 46 } 47 48 function throwAndCatchAddingOne(o) 49 { 50 try { 51 throw 'catch'; 52 return 'try'; 53 } 54 catch (e) { 55 return e; 56 } 57 finally { 58 o.num += 1; 59 } 60 } 61 62 function throwFromCatchAddingOne(o) 63 { 64 try { 65 throw 'catch'; 66 return 'try'; 67 } 68 catch (e) { 69 o.num += 1; 70 note("Catch inside, throwing again (not expected to survive after finally)!"); 71 throw e; // double throw not supported ? 72 return 'catch'; 73 } 74 finally { 75 return 'finally'; 76 } 77 } 78 79 function throwNested(o) 80 { 81 try { 82 throw 'throw'; 83 } catch (e) { 84 note("Catch inside, throwing again!"); 85 throw e; 86 } 87 } 88 89 function returnInTryAndCatch(o) 90 { 91 try { 92 return 'try'; 93 note ("After return in try"); 94 o.num += 1; 95 } 96 catch (e) { 97 note ("Catch after return in try"); 98 o.num += 1; 99 return 'catch'; 100 } 101 } 102 103 function returnInTryCatchAndFinally(o) 104 { 105 try { 106 return 'try'; 107 note ("After return in try"); 108 o.num += 1; 109 } 110 catch (e) { 111 note ("Catch after return in try"); 112 o.num += 1; 113 return 'catch'; 114 } 115 finally { 116 note ("Finally after returns in try and catch"); 117 o.num += 1; 118 return 'finally'; 119 } 120 } 121 122 function tryCatchAndFinally(o) 123 { 124 try { 125 o.sequence += "try"; 126 o.num += 1; 127 } 128 catch (e) { 129 o.sequence += "catch"; 130 o.num += 1; 131 } 132 finally { 133 o.sequence += "finally"; 134 o.num += 1; 135 } 136 } 137 138 function returnInCatchAndFinally(o) 139 { 140 try { 141 throw 'try' 142 note ("After throw in try"); 143 o.num += 1; 144 } 145 catch (e) { 146 note ("Catch after throw in try"); 147 return 'catch'; 148 o.num += 1; 149 } 150 finally { 151 note ("Finally after returns in catch"); 152 o.num += 1; 153 return 'finally'; 154 } 155 } 156 157 158 function test_all() 159 { 160 var res = 'string'; 161 try { 162 throw(1); 163 res = 0; 164 } catch (e) { 165 res = e; 166 } 167 check_equals(typeof(res), 'number'); 168 check_equals(res, 1); 169 170 res = 'string'; 171 try { 172 throw('thrown'); 173 res = 0; 174 } catch(e) { 175 res = e; 176 } 177 finally { 178 res += '_finally'; 179 } 180 check_equals(typeof(res), 'string'); 181 check_equals(res, 'thrown_finally'); 182 183 res = 'string'; 184 try { 185 res = 0; 186 } catch(e) { 187 res = e; 188 } 189 finally { 190 res = 3; 191 } 192 check_equals(typeof(res), 'number'); 193 check_equals(res, 3); 194 195 var o = new Object(); 196 o.num = 1; 197 var ret = addOneOnFinal(o); 198 check_equals(ret, 'finally'); 199 check_equals(o.num, 2); 200 201 ret = throwAndCatchAddingOne(o); 202 check_equals(ret, 'catch'); 203 check_equals(o.num, 3); 204 205 try { 206 ret = throwAndCatchAddingOne(o); 207 } catch (e) { 208 ret = 'catch_outside'; 209 } 210 check_equals(ret, 'catch'); 211 check_equals(o.num, 4); 212 213 try { 214 ret = throwFromCatchAddingOne(o); 215 } catch (e) { 216 note("Catch outside"); 217 o.num += 1; 218 ret += e+'_outside'; 219 } 220 check_equals(ret, 'finally'); 221 check_equals(o.num, 5); 222 223 224 try { 225 ret = returnInTryAndCatch(o); 226 } 227 check_equals(ret, "try"); 228 check_equals(o.num, 5); 229 230 ret = returnInTryCatchAndFinally(o); 231 check_equals(ret, "finally"); 232 check_equals(o.num, 6); 233 234 o.sequence = ""; 235 tryCatchAndFinally(o); 236 check_equals(o.sequence, "tryfinally"); 237 check_equals(o.num, 8); 238 239 ret = returnInCatchAndFinally(o); 240 check_equals(o.num, 9); 241 check_equals(ret, "finally"); 242 243 try { 244 throwNested(); 245 } catch (e) { 246 note("Catch outside"); 247 o.num = e; 248 } 249 check_equals(o.num, 'throw'); 250 } 251 252 static function main(mc) 253 { 254 255 var myTest = new Test; 256 myTest.test_all(); 257 258 check_totals(23); 259 Dejagnu.done(); 260 } 261 262} 263