Home | History | Annotate | Download | only in Exceptions
      1 /*
      2 * The contents of this file are subject to the Netscape Public
      3 * License Version 1.1 (the "License"); you may not use this file
      4 * except in compliance with the License. You may obtain a copy of
      5 * the License at http://www.mozilla.org/NPL/
      6 *
      7 * Software distributed under the License is distributed on an
      8 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed
      9 * or implied. See the License for the specific language governing
     10 * rights and limitations under the License.
     11 *
     12 * The Original Code is mozilla.org code.
     13 *
     14 * The Initial Developer of the Original Code is Netscape
     15 * Communications Corporation.  Portions created by Netscape are
     16 * Copyright (C) 1998 Netscape Communications Corporation.
     17 * All Rights Reserved.
     18 *
     19 * Contributor(s): brendan (at) mozilla.org, pschwartau (at) netscape.com
     20 * Date: 2001-08-27
     21 *
     22 * SUMMARY:  Testing binding of function names
     23 *
     24 * Brendan:
     25 *
     26 * "... the question is, does Rhino bind 'sum' in the global object
     27 * for the following test? If it does, it's buggy.
     28 *
     29 *   var f = function sum(){};
     30 *   print(sum);  // should fail with 'sum is not defined' "
     31 *
     32 */
     33 //-----------------------------------------------------------------------------
     34 var UBound = 0;
     35 var bug = '(none)';
     36 var summary = 'Testing binding of function names';
     37 var ERR_REF_YES = 'ReferenceError';
     38 var ERR_REF_NO = 'did NOT generate a ReferenceError';
     39 var statusitems = [];
     40 var actualvalues = [];
     41 var expectedvalues = [];
     42 var status = summary;
     43 var actual = ERR_REF_NO;
     44 var expect= ERR_REF_YES;
     45 
     46 
     47 try
     48 {
     49   var f = function sum(){};
     50   print(sum);
     51 }
     52 catch (e)
     53 {
     54   status = 'Section 1 of test';
     55   actual = e instanceof ReferenceError;
     56   expect = true;
     57   addThis();
     58 
     59 
     60   /*
     61    * This test is more literal, and one day may not be valid.
     62    * Searching for literal string "ReferenceError" in e.toString()
     63    */
     64   status = 'Section 2 of test';
     65   var match = e.toString().search(/ReferenceError/);
     66   actual = (match > -1);
     67   expect = true;
     68   addThis();
     69 }
     70 
     71 
     72 
     73 //-----------------------------------------------------------------------------
     74 test();
     75 //-----------------------------------------------------------------------------
     76 
     77 
     78 function addThis()
     79 {
     80   statusitems[UBound] = status;
     81   actualvalues[UBound] = isReferenceError(actual);
     82   expectedvalues[UBound] = isReferenceError(expect);
     83   UBound++;
     84 }
     85 
     86 
     87 function test()
     88 {
     89   enterFunc ('test');
     90   printBugNumber (bug);
     91   printStatus (summary);
     92 
     93   for (var i = 0; i < UBound; i++)
     94   {
     95     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
     96   }
     97 
     98   exitFunc ('test');
     99 }
    100 
    101 
    102 // converts a Boolean result into a textual result -
    103 function isReferenceError(bResult)
    104 {
    105   return bResult? ERR_REF_YES : ERR_REF_NO;
    106 }
    107