]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_2/function/tostring-1.js
   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/ 
   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. 
  11  * The Original Code is Mozilla Communicator client code, released March 
  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 
  23     File Name:          tostring-1.js 
  24     Section:            Function.toString 
  27     Since the behavior of Function.toString() is implementation-dependent, 
  28     toString tests for function are not in the ECMA suite. 
  30     Currently, an attempt to parse the toString output for some functions 
  31     and verify that the result is something reasonable. 
  33     Author:             christine@netscape.com 
  34     Date:               12 november 1997 
  37 // These test cases should not be testing for a particular 
  38 // whitespace formatting (this is implementation defined). 
  39 // Strip out whitespace, or in the case of whitespace 
  40 // abutting a word character reduce to a single space. 
  41 function simplify(str
) 
  43     return str
.replace(/\s+/g, " ").replace(/ (\W
)/g
, "$1").replace(/(\W) /g, "$1").trim(); 
  46     var SECTION 
= "tostring-1"; 
  47     var VERSION 
= "JS1_2"; 
  49     var TITLE   
= "Function.toString()"; 
  51     writeHeaderToLog( SECTION 
+ " "+ TITLE
); 
  53     var testcases 
= new Array(); 
  57     t1 
= new TestFunction( "stub", "value", tab 
+ "return value;" ); 
  59     t2 
= new TestFunction( "ToString", "object", tab
+"return object + \"\";" ); 
  61     t3 
= new TestFunction( "Add", "a, b, c, d, e",  tab 
+"var s = a + b + c + d + e;\n" + 
  64     t4 
= new TestFunction( "noop", "value" ); 
  66     t5 
= new TestFunction( "anonymous", "", tab
+"return \"hello!\";" ); 
  68     var f 
= new Function( "return \"hello!\";"); 
  70     testcases
[tc
++] = new TestCase( SECTION
, 
  72                                     simplify(t1
.valueOf()), 
  73                                     simplify(stub
.toString()) ); 
  75     testcases
[tc
++] = new TestCase( SECTION
, 
  76                                     "ToString.toString()", 
  77                                     simplify(t2
.valueOf()), 
  78                                     simplify(ToString
.toString()) ); 
  80     testcases
[tc
++] = new TestCase( SECTION
, 
  82                                     simplify(t3
.valueOf()), 
  83                                     simplify(Add
.toString()) ); 
  85     testcases
[tc
++] = new TestCase( SECTION
, 
  87                                     simplify(t4
.toString()), 
  88                                     simplify(noop
.toString()) ); 
  90     testcases
[tc
++] = new TestCase( SECTION
, 
  92                                     simplify(t5
.toString()), 
  93                                     simplify(f
.toString()) ); 
  96 function noop( value 
) { 
  98 function Add( a
, b
, c
, d
, e 
) { 
  99     var s 
= a 
+ b 
+ c 
+ d 
+ e
; 
 102 function stub( value 
) { 
 105 function ToString( object 
) { 
 109 function ToBoolean( value 
) { 
 110     if ( value 
== 0 || value 
== NaN 
|| value 
== false ) { 
 118     for ( tc
=0; tc 
< testcases
.length
; tc
++ ) { 
 119         testcases
[tc
].passed 
= writeTestCaseResult( 
 120                             testcases
[tc
].expect
, 
 121                             testcases
[tc
].actual
, 
 122                             testcases
[tc
].description 
+" = "+ 
 123                             testcases
[tc
].actual 
); 
 125         testcases
[tc
].reason 
+= ( testcases
[tc
].passed 
) ? "" : "wrong value "; 
 128     return ( testcases 
); 
 131 function TestFunction( name
, args
, body 
) { 
 132     if ( name 
== "anonymous" && version() == 120 ) { 
 137     this.arguments 
= args
.toString(); 
 140     /* the format of Function.toString() in JavaScript 1.2 is: 
 142     function name ( arguments ) { 
 146     this.value 
= "\nfunction " + (name 
? name : "" )+ 
 147     "("+args
+") {\n"+ (( body 
) ? body 
+"\n" : "") + "}\n"; 
 149     this.toString 
= new Function( "return this.value" ); 
 150     this.valueOf 
= new Function( "return this.value" );