michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 346494; michael@0: var summary = 'various try...catch tests'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: var pfx = "(function (x) {try {throw x}", michael@0: cg1 = " catch (e if e === 42) {var v = 'catch guard 1 ' + e; actual += v + ','; print(v);}" michael@0: cg2 = " catch (e if e === 43) {var v = 'catch guard 2 ' + e; actual += v + ','; print(v);}" michael@0: cat = " catch (e) {var v = 'catch all ' + e; actual += v + ','; print(v);}" michael@0: fin = " finally{var v = 'fin'; actual += v + ','; print(v)}", michael@0: end = "})"; michael@0: michael@0: var exphash = { michael@0: pfx: "(function (y) { var result = ''; y = y + ',';", michael@0: cg1: "result += (y === '42,') ? ('catch guard 1 ' + y):'';", michael@0: cg2: "result += (y === '43,') ? ('catch guard 2 ' + y):'';", michael@0: cat: "result += /catch guard/.test(result) ? '': ('catch all ' + y);", michael@0: fin: "result += 'fin,';", michael@0: end: "return result;})" michael@0: }; michael@0: michael@0: var src = [ michael@0: pfx + fin + end, michael@0: pfx + cat + end, michael@0: pfx + cat + fin + end, michael@0: pfx + cg1 + end, michael@0: pfx + cg1 + fin + end, michael@0: pfx + cg1 + cat + end, michael@0: pfx + cg1 + cat + fin + end, michael@0: pfx + cg1 + cg2 + end, michael@0: pfx + cg1 + cg2 + fin + end, michael@0: pfx + cg1 + cg2 + cat + end, michael@0: pfx + cg1 + cg2 + cat + fin + end, michael@0: ]; michael@0: michael@0: var expsrc = [ michael@0: exphash.pfx + exphash.fin + exphash.end, michael@0: exphash.pfx + exphash.cat + exphash.end, michael@0: exphash.pfx + exphash.cat + exphash.fin + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.fin + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.cat + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.cat + exphash.fin + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.cg2 + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.cg2 + exphash.fin + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.cg2 + exphash.cat + exphash.end, michael@0: exphash.pfx + exphash.cg1 + exphash.cg2 + exphash.cat + exphash.fin + exphash.end, michael@0: ]; michael@0: michael@0: for (var i in src) { michael@0: print("\n=== " + src[i]); michael@0: var f = eval(src[i]); michael@0: print(src[i]); michael@0: var exp = eval(expsrc[i]); michael@0: // dis(f); michael@0: print('decompiling: ' + f); michael@0: michael@0: actual = ''; michael@0: try { expect = exp(42); f(42) } catch (e) { print('tried f(42), caught ' + e) } michael@0: reportCompare(expect, actual, summary); michael@0: michael@0: actual = ''; michael@0: try { expect = exp(43); f(43) } catch (e) { print('tried f(43), caught ' + e) } michael@0: reportCompare(expect, actual, summary); michael@0: michael@0: actual = ''; michael@0: try { expect = exp(44); f(44) } catch (e) { print('tried f(44), caught ' + e) } michael@0: reportCompare(expect, actual, summary); michael@0: } michael@0: michael@0: michael@0: exitFunc ('test'); michael@0: }