michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: var completed = false; michael@0: var testcases; michael@0: var tc = 0; michael@0: michael@0: SECTION = ""; michael@0: VERSION = ""; michael@0: BUGNUMBER = ""; michael@0: EXCLUDE = ""; michael@0: michael@0: /* michael@0: * constant strings michael@0: */ michael@0: var GLOBAL = "[object global]"; michael@0: var PASSED = " PASSED!" michael@0: var FAILED = " FAILED! expected: "; michael@0: michael@0: var DEBUG = false; michael@0: michael@0: version("120"); michael@0: /* michael@0: * change this for date tests if you're not in PST michael@0: */ michael@0: michael@0: TZ_DIFF = -8; michael@0: /* wrapper for test cas constructor that doesn't require the SECTION michael@0: * argument. michael@0: */ michael@0: michael@0: function AddTestCase( description, expect, actual ) { michael@0: testcases[tc++] = new TestCase( SECTION, description, expect, actual ); michael@0: } michael@0: function TestCase( n, d, e, a ) { michael@0: this.name = n; michael@0: this.description = d; michael@0: this.expect = e; michael@0: this.actual = a; michael@0: this.passed = true; michael@0: this.reason = ""; michael@0: this.bugnumber = BUGNUMBER; michael@0: michael@0: this.passed = getTestCaseResult( this.expect, this.actual ); michael@0: } michael@0: function startTest() { michael@0: version(120); michael@0: michael@0: // for ecma version 2.0, we will leave the javascript version to michael@0: // the default ( for now ). michael@0: // print out bugnumber michael@0: michael@0: if ( BUGNUMBER ) { michael@0: print ("BUGNUMBER: " + BUGNUMBER ); michael@0: } michael@0: michael@0: testcases = new Array(); michael@0: tc = 0; michael@0: michael@0: } michael@0: function test() { michael@0: for ( tc=0; tc < testcases.length; tc++ ) { michael@0: testcases[tc].passed = writeTestCaseResult( michael@0: testcases[tc].expect, michael@0: testcases[tc].actual, michael@0: testcases[tc].description +" = "+ michael@0: testcases[tc].actual ); michael@0: michael@0: testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; michael@0: } michael@0: stopTest(); michael@0: return ( testcases ); michael@0: } michael@0: function getTestCaseResult( expect, actual ) { michael@0: // because ( NaN == NaN ) always returns false, need to do michael@0: // a special compare to see if we got the right result. michael@0: if ( actual != actual ) { michael@0: if ( typeof actual == "object" ) { michael@0: actual = "NaN object"; michael@0: } else { michael@0: actual = "NaN number"; michael@0: } michael@0: } michael@0: if ( expect != expect ) { michael@0: if ( typeof expect == "object" ) { michael@0: expect = "NaN object"; michael@0: } else { michael@0: expect = "NaN number"; michael@0: } michael@0: } michael@0: michael@0: var passed = ( expect == actual ) ? true : false; michael@0: michael@0: // if both objects are numbers, give a little leeway for rounding. michael@0: if ( !passed michael@0: && typeof(actual) == "number" michael@0: && typeof(expect) == "number" michael@0: ) { michael@0: if ( Math.abs(actual-expect) < 0.0000001 ) { michael@0: passed = true; michael@0: } michael@0: } michael@0: michael@0: // verify type is the same michael@0: if ( typeof(expect) != typeof(actual) ) { michael@0: passed = false; michael@0: } michael@0: michael@0: return passed; michael@0: } michael@0: /* michael@0: * Begin printing functions. These functions use the shell's michael@0: * print function. When running tests in the browser, these michael@0: * functions, override these functions with functions that use michael@0: * document.write. michael@0: */ michael@0: michael@0: function writeTestCaseResult( expect, actual, string ) { michael@0: var passed = getTestCaseResult( expect, actual ); michael@0: writeFormattedResult( expect, actual, string, passed ); michael@0: return passed; michael@0: } michael@0: function writeFormattedResult( expect, actual, string, passed ) { michael@0: var s = string ; michael@0: s += ( passed ) ? PASSED : FAILED + expect; michael@0: print( s); michael@0: return passed; michael@0: } michael@0: michael@0: function writeHeaderToLog( string ) { michael@0: print( string ); michael@0: } michael@0: /* end of print functions */ michael@0: michael@0: michael@0: function stopTest() { michael@0: var sizeTag = "<#TEST CASES SIZE>"; michael@0: var doneTag = "<#TEST CASES DONE>"; michael@0: var beginTag = "<#TEST CASE "; michael@0: var endTag = ">"; michael@0: michael@0: print(sizeTag); michael@0: print(testcases.length); michael@0: for (tc = 0; tc < testcases.length; tc++) michael@0: { michael@0: print(beginTag + 'PASSED' + endTag); michael@0: print(testcases[tc].passed); michael@0: print(beginTag + 'NAME' + endTag); michael@0: print(testcases[tc].name); michael@0: print(beginTag + 'EXPECTED' + endTag); michael@0: print(testcases[tc].expect); michael@0: print(beginTag + 'ACTUAL' + endTag); michael@0: print(testcases[tc].actual); michael@0: print(beginTag + 'DESCRIPTION' + endTag); michael@0: print(testcases[tc].description); michael@0: print(beginTag + 'REASON' + endTag); michael@0: print(( testcases[tc].passed ) ? "" : "wrong value "); michael@0: print(beginTag + 'BUGNUMBER' + endTag); michael@0: print( BUGNUMBER ); michael@0: } michael@0: print(doneTag); michael@0: gc(); michael@0: } michael@0: michael@0: function getFailedCases() { michael@0: for ( var i = 0; i < testcases.length; i++ ) { michael@0: if ( ! testcases[i].passed ) { michael@0: print( testcases[i].description +" = " +testcases[i].actual +" expected: "+ testcases[i].expect ); michael@0: } michael@0: } michael@0: } michael@0: function err( msg, page, line ) { michael@0: testcases[tc].actual = "error"; michael@0: testcases[tc].reason = msg; michael@0: writeTestCaseResult( testcases[tc].expect, michael@0: testcases[tc].actual, michael@0: testcases[tc].description +" = "+ testcases[tc].actual + michael@0: ": " + testcases[tc].reason ); michael@0: stopTest(); michael@0: return true; michael@0: } michael@0: function Enumerate ( o ) { michael@0: var p; michael@0: for ( p in o ) { michael@0: print( p +": " + o[p] ); michael@0: } michael@0: } michael@0: function GetContext() { michael@0: return Packages.com.netscape.javascript.Context.getCurrentContext(); michael@0: } michael@0: function OptLevel( i ) { michael@0: i = Number(i); michael@0: var cx = GetContext(); michael@0: cx.setOptimizationLevel(i); michael@0: }