File indexing completed on 2024-04-28 15:28:38

0001 
0002 // Tests for raising --- and non-raising exceptions on access to reference to undefined things...
0003 
0004 // Locals should throw on access if undefined..
0005 fnShouldThrow(function() { a = x; }, ReferenceError);
0006 
0007 // Read-modify-write versions of assignment should throw as well
0008 fnShouldThrow(function() { x += "foo"; }, ReferenceError);
0009 
0010 // Other reference types should just return undefined...
0011 a = new Object();
0012 fnShouldNotThrow(function() { b = a.x; });
0013 fnShouldNotThrow(function() { b = a['x']; });
0014 fnShouldNotThrow(function() { a['x'] += 'baz'; });
0015 shouldBe("a['x']", '"undefinedbaz"');
0016 fnShouldNotThrow(function() { b = a.y; });
0017 fnShouldNotThrow(function() { a.y += 'glarch'; });
0018 shouldBe("a['y']", '"undefinedglarch"');
0019 
0020 
0021 // Helpers!
0022 function fnShouldThrow(f, exType)
0023 {
0024   var exception;
0025   var _av;
0026   try {
0027      _av = f();
0028   } catch (e) {
0029      exception = e;
0030   }
0031 
0032   if (exception) {
0033     if (typeof exType == "undefined" || exception instanceof exType)
0034       testPassed(f + " threw exception " + exception + ".");
0035     else
0036       testFailed(f + " should throw exception " + exType + ". Threw exception " + exception + ".");
0037   } else if (typeof _av == "undefined")
0038     testFailed(f + " should throw exception " + exType + ". Was undefined.");
0039   else
0040     testFailed(f + " should throw exception " + exType + ". Was " + _av + ".");
0041 }
0042 
0043 function fnShouldNotThrow(f)
0044 {
0045   try {
0046     f();
0047     testPassed(f + " did not throw an exception");
0048   } catch (e) {
0049     testFailed(f + " threw an exception " + e + " when no exception expected");
0050   }
0051 }