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 
     25 var SECTION	= "";
     26 var VERSION	= "";
     27 var BUGNUMBER =	"";
     28 
     29 var	GLOBAL = "[object global]";
     30 var PASSED = " PASSED!"
     31 var FAILED = " FAILED! expected: ";
     32 
     33 startTest();
     34 
     35     version(120);
     36 
     37 function test() {
     38     for ( tc=0; tc < testcases.length; tc++ ) {
     39         testcases[tc].passed = writeTestCaseResult(
     40                             testcases[tc].expect,
     41                             testcases[tc].actual,
     42                             testcases[tc].description +" = "+
     43                             testcases[tc].actual );
     44 
     45         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     46     }
     47     stopTest();
     48     return ( testcases );
     49 }
     50 /* wrapper for test cas constructor that doesn't require the SECTION
     51  * argument.
     52  */
     53 
     54 function AddTestCase( description, expect, actual ) {
     55     testcases[tc++] = new TestCase( SECTION, description, expect, actual );
     56 }
     57 
     58 
     59 function TestCase( n, d, e, a ) {
     60     this.name        = n;
     61     this.description = d;
     62     this.expect      = e;
     63     this.actual      = a;
     64     this.passed      = true;
     65     this.reason      = "";
     66 
     67     this.passed = getTestCaseResult( this.expect, this.actual );
     68 }
     69 function startTest() {
     70     version(120);
     71 
     72     if ( BUGNUMBER ) {
     73         writeLineToLog ("BUGNUMBER: " + BUGNUMBER );
     74     }
     75 
     76     testcases = new Array();
     77     tc = 0;
     78 }
     79 function getTestCaseResult( expect, actual ) {
     80     //  because ( NaN == NaN ) always returns false, need to do
     81     //  a special compare to see if we got the right result.
     82         if ( actual != actual ) {
     83             if ( typeof actual == "object" ) {
     84                 actual = "NaN object";
     85             } else {
     86                 actual = "NaN number";
     87             }
     88         }
     89         if ( expect != expect ) {
     90             if ( typeof expect == "object" ) {
     91                 expect = "NaN object";
     92             } else {
     93                 expect = "NaN number";
     94             }
     95         }
     96 
     97         var passed = ( expect == actual ) ? true : false;
     98 
     99     //  if both objects are numbers, give a little leeway for rounding.
    100         if (    !passed
    101                 && typeof(actual) == "number"
    102                 && typeof(expect) == "number"
    103             ) {
    104                 if ( Math.abs(actual-expect) < 0.0000001 ) {
    105                     passed = true;
    106                 }
    107         }
    108 
    109     //  verify type is the same
    110         if ( typeof(expect) != typeof(actual) ) {
    111             passed = false;
    112         }
    113 
    114         return passed;
    115 }
    116 /*
    117  * Begin printing functions.  These functions use the shell's
    118  * print function.  When running tests in the browser, these
    119  * functions, override these functions with functions that use
    120  * document.write.
    121  */
    122 
    123 function writeTestCaseResult( expect, actual, string ) {
    124 		var	passed = getTestCaseResult(	expect,	actual );
    125 		writeFormattedResult( expect, actual, string, passed );
    126 		return passed;
    127 }
    128 function writeFormattedResult( expect, actual, string, passed ) {
    129         var s = string ;
    130         s += ( passed ) ? PASSED : FAILED + expect;
    131         writeLineToLog( s);
    132         return passed;
    133 }
    134 function writeLineToLog( string	) {
    135 	print( string );
    136 }
    137 function writeHeaderToLog( string )	{
    138 	print( string );
    139 }
    140 /* end of print functions */
    141 
    142 function stopTest() {
    143    var gc;
    144    if ( gc != undefined ) {
    145         gc();
    146    }
    147 }
    148