File indexing completed on 2024-04-28 15:28:37
0001 function shouldBe(a, b, c) 0002 { 0003 if ( a == b ) 0004 debug(c+" .......... PASS"); 0005 else 0006 debug(c+" .......... FAIL"); 0007 } 0008 0009 function testThrow() 0010 { 0011 var caught = false; 0012 try { 0013 throw 99; 0014 } catch (e) { 0015 caught = true; 0016 } 0017 shouldBe(caught, true, "testing throw()"); 0018 } 0019 0020 // same as above but lacking a semicolon after throw 0021 function testThrow2() 0022 { 0023 var caught = false; 0024 try { 0025 throw 99 0026 } catch (e) { 0027 caught = true; 0028 } 0029 shouldBe(caught, true, "testing throw()"); 0030 } 0031 0032 function testReferenceError() 0033 { 0034 var err = "noerror"; 0035 var caught = false; 0036 try { 0037 var dummy = nonexistant; // throws reference error 0038 } catch (e) { 0039 caught = true; 0040 err = e.name; 0041 } 0042 // test err 0043 shouldBe(caught, true, "ReferenceError"); 0044 } 0045 0046 function testFunctionErrorHelper() 0047 { 0048 var a = b; // throws reference error 0049 } 0050 0051 function testFunctionError() 0052 { 0053 var caught = false; 0054 try { 0055 testFunctionErrorHelper(); 0056 } catch (e) { 0057 caught = true; 0058 } 0059 shouldBe(caught, true, "error propagation in functions"); 0060 } 0061 0062 function testMathFunctionError() 0063 { 0064 var caught = false; 0065 try { 0066 Math(); 0067 } catch (e) { 0068 debug("catch"); 0069 caught = true; 0070 } finally { 0071 debug("finally"); 0072 } 0073 shouldBe(caught, true, "Math() error"); 0074 } 0075 0076 function testWhileAbortion() 0077 { 0078 var caught = 0; 0079 try { 0080 while (a=b, 1) { // "endless error" in condition 0081 ; 0082 } 0083 } catch (e) { 0084 caught++; 0085 } 0086 0087 try { 0088 while (1) { 0089 var a = b; // error in body 0090 } 0091 } catch (e) { 0092 caught++; 0093 } 0094 shouldBe(caught, 2, "Abort while() on error"); 0095 } 0096 0097 function testCrashes() 0098 { 0099 var caught = 0; 0100 // prefix node returned an invalid object in case of an error 0101 try { 0102 eval("!--D()"); 0103 } catch(e) { 0104 ++caught; 0105 } 0106 try { 0107 eval("--D() + ''"); 0108 } catch(e) { 0109 ++caught; 0110 } 0111 shouldBe(caught, 2, "no crashes"); 0112 } 0113 0114 function testVariable() 0115 { 0116 try { 0117 throw 1; 0118 } catch (x) { // local to catch-block? 0119 var x = 2; 0120 var y = 3; 0121 z = 4; 0122 shouldBe(x, 2, "exception variable 1"); 0123 shouldBe(y, 3, "exception variable 2"); 0124 shouldBe(z, 4, "exception variable 3"); 0125 } 0126 // IE and Firefox disagree on this one 0127 var pass = typeof x == "undefined" || x == 2; 0128 shouldBe(pass, true, "out-of-scope exception variable 1"); 0129 shouldBe(y, 3, "out-of-scope exception variable 2"); 0130 shouldBe(z, 4, "out-of-scope exception variable 3"); 0131 } 0132 0133 debug("Except a lot of errors. They should all be caught and lead to PASS"); 0134 testThrow(); 0135 testThrow2(); 0136 testReferenceError(); 0137 testFunctionError(); 0138 testMathFunctionError(); 0139 testWhileAbortion(); 0140 testCrashes(); 0141 testVariable();