Home | History | Annotate | Download | only in js1_2
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 var	completed =	false;
     23 var	testcases;
     24 var tc = 0;
     25 
     26 SECTION	= "";
     27 VERSION	= "";
     28 BUGNUMBER =	"";
     29 EXCLUDE = "";
     30 
     31 /*
     32  * constant strings
     33  */
     34 var	GLOBAL = "[object global]";
     35 var PASSED = " PASSED!"
     36 var FAILED = " FAILED! expected: ";
     37 
     38 var	DEBUG =	false;
     39 
     40 version("120");
     41 /*
     42  * change this for date tests if you're not in PST
     43  */
     44 
     45 TZ_DIFF	= -8;
     46 /* wrapper for test cas constructor that doesn't require the SECTION
     47  * argument.
     48  */
     49 
     50 function AddTestCase( description, expect, actual ) {
     51     testcases[tc++] = new TestCase( SECTION, description, expect, actual );
     52 }
     53 function TestCase( n, d, e, a ) {
     54     this.name        = n;
     55     this.description = d;
     56     this.expect      = e;
     57     this.actual      = a;
     58     this.passed      = true;
     59     this.reason      = "";
     60     this.bugnumber   = BUGNUMBER;
     61 
     62     this.passed = getTestCaseResult( this.expect, this.actual );
     63 }
     64 function startTest() {
     65     version(120);
     66 
     67     // for ecma version 2.0, we will leave the javascript version to
     68     // the default ( for now ).
     69     // print out bugnumber
     70 
     71     if ( BUGNUMBER ) {
     72             writeLineToLog ("BUGNUMBER: " + BUGNUMBER );
     73     }
     74 
     75     testcases = new Array();
     76     tc = 0;
     77 
     78 }
     79 function test() {
     80     for ( tc=0; tc < testcases.length; tc++ ) {
     81         testcases[tc].passed = writeTestCaseResult(
     82                             testcases[tc].expect,
     83                             testcases[tc].actual,
     84                             testcases[tc].description +" = "+
     85                             testcases[tc].actual );
     86 
     87         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     88     }
     89     stopTest();
     90     return ( testcases );
     91 }
     92 function getTestCaseResult( expect, actual ) {
     93     //  because ( NaN == NaN ) always returns false, need to do
     94     //  a special compare to see if we got the right result.
     95         if ( actual != actual ) {
     96             if ( typeof actual == "object" ) {
     97                 actual = "NaN object";
     98             } else {
     99                 actual = "NaN number";
    100             }
    101         }
    102         if ( expect != expect ) {
    103             if ( typeof expect == "object" ) {
    104                 expect = "NaN object";
    105             } else {
    106                 expect = "NaN number";
    107             }
    108         }
    109 
    110         var passed = ( expect == actual ) ? true : false;
    111 
    112     //  if both objects are numbers, give a little leeway for rounding.
    113         if (    !passed
    114                 && typeof(actual) == "number"
    115                 && typeof(expect) == "number"
    116             ) {
    117                 if ( Math.abs(actual-expect) < 0.0000001 ) {
    118                     passed = true;
    119                 }
    120         }
    121 
    122     //  verify type is the same
    123         if ( typeof(expect) != typeof(actual) ) {
    124             passed = false;
    125         }
    126 
    127         return passed;
    128 }
    129 /*
    130  * Begin printing functions.  These functions use the shell's
    131  * print function.  When running tests in the browser, these
    132  * functions, override these functions with functions that use
    133  * document.write.
    134  */
    135 
    136 function writeTestCaseResult( expect, actual, string ) {
    137 		var	passed = getTestCaseResult(	expect,	actual );
    138 		writeFormattedResult( expect, actual, string, passed );
    139 		return passed;
    140 }
    141 function writeFormattedResult( expect, actual, string, passed ) {
    142         var s = string ;
    143         s += ( passed ) ? PASSED : FAILED + expect;
    144         writeLineToLog( s);
    145         return passed;
    146 }
    147 function writeLineToLog( string	) {
    148 	print( string );
    149 }
    150 function writeHeaderToLog( string )	{
    151 	print( string );
    152 }
    153 /* end of print functions */
    154 
    155 
    156 function stopTest() {
    157     var sizeTag  = "<#TEST CASES SIZE>";
    158     var doneTag  = "<#TEST CASES DONE>";
    159     var beginTag = "<#TEST CASE ";
    160     var endTag   = ">";
    161 
    162     print(sizeTag);
    163     print(testcases.length);
    164     for (tc = 0; tc < testcases.length; tc++)
    165     {
    166         print(beginTag + 'PASSED'      + endTag);
    167         print(testcases[tc].passed);
    168         print(beginTag + 'NAME'        + endTag);
    169         print(testcases[tc].name);
    170         print(beginTag + 'EXPECTED'    + endTag);
    171         print(testcases[tc].expect);
    172         print(beginTag + 'ACTUAL'      + endTag);
    173         print(testcases[tc].actual);
    174         print(beginTag + 'DESCRIPTION' + endTag);
    175         print(testcases[tc].description);
    176         print(beginTag + 'REASON'      + endTag);
    177         print(( testcases[tc].passed ) ? "" : "wrong value ");
    178         print(beginTag + 'BUGNUMBER'   + endTag);
    179         print( BUGNUMBER );
    180     }
    181     print(doneTag);
    182     gc();
    183 }
    184 
    185 function getFailedCases() {
    186   for ( var i = 0; i < testcases.length; i++ ) {
    187      if ( ! testcases[i].passed ) {
    188         print( testcases[i].description +" = " +testcases[i].actual +" expected: "+ testcases[i].expect );
    189      }
    190   }
    191 }
    192 function err( msg, page, line ) {
    193     testcases[tc].actual = "error";
    194     testcases[tc].reason = msg;
    195     writeTestCaseResult( testcases[tc].expect,
    196                          testcases[tc].actual,
    197                          testcases[tc].description +" = "+ testcases[tc].actual +
    198                          ": " + testcases[tc].reason );
    199     stopTest();
    200     return true;
    201 }
    202 function Enumerate ( o ) {
    203     var p;
    204     for ( p in o ) {
    205         print( p +": " + o[p] );
    206     }
    207 }
    208 function GetContext() {
    209     return Packages.com.netscape.javascript.Context.getCurrentContext();
    210 }
    211 function OptLevel( i ) {
    212     i = Number(i);
    213     var cx = GetContext();
    214     cx.setOptimizationLevel(i);
    215 }
    216