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/** 23 File Name: tostring-1.js 24 Section: Function.toString 25 Description: 26 27 Since the behavior of Function.toString() is implementation-dependent, 28 toString tests for function are not in the ECMA suite. 29 30 Currently, an attempt to parse the toString output for some functions 31 and verify that the result is something reasonable. 32 33 This verifies 34 http://scopus.mcom.com/bugsplat/show_bug.cgi?id=99212 35 36 Author: christine@netscape.com 37 Date: 12 november 1997 38*/ 39 40 var SECTION = "tostring-2"; 41 var VERSION = "JS1_2"; 42 startTest(); 43 var TITLE = "Function.toString()"; 44 var BUGNUMBER="123444"; 45 46 writeHeaderToLog( SECTION + " "+ TITLE); 47 48 var testcases = new Array(); 49 50 var tab = " "; 51 52 53var equals = new TestFunction( "Equals", "a, b", tab+ "return a == b;" ); 54function Equals (a, b) { 55 return a == b; 56} 57 58var reallyequals = new TestFunction( "ReallyEquals", "a, b", 59 ( version() <= 120 ) ? tab +"return a == b;" : tab +"return a === b;" ); 60function ReallyEquals( a, b ) { 61 return a === b; 62} 63 64var doesntequal = new TestFunction( "DoesntEqual", "a, b", tab + "return a != b;" ); 65function DoesntEqual( a, b ) { 66 return a != b; 67} 68 69var reallydoesntequal = new TestFunction( "ReallyDoesntEqual", "a, b", 70 ( version() <= 120 ) ? tab +"return a != b;" : tab +"return a !== b;" ); 71function ReallyDoesntEqual( a, b ) { 72 return a !== b; 73} 74 75var testor = new TestFunction( "TestOr", "a", tab+"if (a == null || a == void 0) {\n"+ 76 tab +tab+"return 0;\n"+tab+"} else {\n"+tab+tab+"return a;\n"+tab+"}" ); 77function TestOr( a ) { 78 if ( a == null || a == void 0 ) 79 return 0; 80 else 81 return a; 82} 83 84var testand = new TestFunction( "TestAnd", "a", tab+"if (a != null && a != void 0) {\n"+ 85 tab+tab+"return a;\n" + tab+ "} else {\n"+tab+tab+"return 0;\n"+tab+"}" ); 86function TestAnd( a ) { 87 if ( a != null && a != void 0 ) 88 return a; 89 else 90 return 0; 91} 92 93var or = new TestFunction( "Or", "a, b", tab + "return a | b;" ); 94function Or( a, b ) { 95 return a | b; 96} 97 98var and = new TestFunction( "And", "a, b", tab + "return a & b;" ); 99function And( a, b ) { 100 return a & b; 101} 102 103var xor = new TestFunction( "XOr", "a, b", tab + "return a ^ b;" ); 104function XOr( a, b ) { 105 return a ^ b; 106} 107 108 testcases[testcases.length] = new TestCase( SECTION, 109 "Equals.toString()", 110 equals.valueOf(), 111 Equals.toString() ); 112 113 testcases[testcases.length] = new TestCase( SECTION, 114 "ReallyEquals.toString()", 115 reallyequals.valueOf(), 116 ReallyEquals.toString() ); 117 118 testcases[testcases.length] = new TestCase( SECTION, 119 "DoesntEqual.toString()", 120 doesntequal.valueOf(), 121 DoesntEqual.toString() ); 122 123 testcases[testcases.length] = new TestCase( SECTION, 124 "ReallyDoesntEqual.toString()", 125 reallydoesntequal.valueOf(), 126 ReallyDoesntEqual.toString() ); 127 128 testcases[testcases.length] = new TestCase( SECTION, 129 "TestOr.toString()", 130 testor.valueOf(), 131 TestOr.toString() ); 132 133 testcases[testcases.length] = new TestCase( SECTION, 134 "TestAnd.toString()", 135 testand.valueOf(), 136 TestAnd.toString() ); 137 138 testcases[testcases.length] = new TestCase( SECTION, 139 "Or.toString()", 140 or.valueOf(), 141 Or.toString() ); 142 143 testcases[testcases.length] = new TestCase( SECTION, 144 "And.toString()", 145 and.valueOf(), 146 And.toString() ); 147 148 testcases[testcases.length] = new TestCase( SECTION, 149 "XOr.toString()", 150 xor.valueOf(), 151 XOr.toString() ); 152 153 test(); 154 155function test() { 156 for ( tc=0; tc < testcases.length; tc++ ) { 157 testcases[tc].passed = writeTestCaseResult( 158 testcases[tc].expect, 159 testcases[tc].actual, 160 testcases[tc].description +" = "+ 161 testcases[tc].actual ); 162 163 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 164 } 165 stopTest(); 166 return ( testcases ); 167} 168function TestFunction( name, args, body ) { 169 this.name = name; 170 this.arguments = args.toString(); 171 this.body = body; 172 173 /* the format of Function.toString() in JavaScript 1.2 is: 174 /n 175 function name ( arguments ) { 176 body 177 } 178 */ 179 this.value = "\nfunction " + (name ? name : "anonymous" )+ 180 "("+args+") {\n"+ (( body ) ? body +"\n" : "") + "}\n"; 181 182 this.toString = new Function( "return this.value" ); 183 this.valueOf = new Function( "return this.value" ); 184 return this; 185} 186