• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:          15.5.2.js
24    ECMA Section:       15.5.2 The String Constructor
25            			15.5.2.1 new String(value)
26			            15.5.2.2 new String()
27
28    Description:	When String is called as part of a new expression, it
29                    is a constructor; it initializes the newly constructed
30                    object.
31
32        			- The prototype property of the newly constructed
33		        	object is set to the original String prototype object,
34		        	the one that is the intial value of String.prototype
35        			- The internal [[Class]] property of the object is "String"
36        			- The value of the object is ToString(value).
37        			- If no value is specified, its value is the empty string.
38
39    Author:             christine@netscape.com
40    Date:               1 october 1997
41*/
42
43    var SECTION = "15.5.2";
44    var VERSION = "ECMA_1";
45    startTest();
46    var TITLE   = "The String Constructor";
47
48    writeHeaderToLog( SECTION + " "+ TITLE);
49
50    var testcases = getTestCases();
51    test();
52
53function getTestCases() {
54    var array = new Array();
55    var item = 0;
56
57    array[item++] = new TestCase( SECTION,	"typeof new String('string primitive')",	    "object",	        typeof new String('string primitive') );
58    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String('string primitive'); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String('string primitive'); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
59    array[item++] = new TestCase( SECTION,  "(new String('string primitive')).valueOf()",   'string primitive', (new String('string primitive')).valueOf() );
60    array[item++] = new TestCase( SECTION,  "(new String('string primitive')).substring",   String.prototype.substring,   (new String('string primitive')).substring );
61
62    array[item++] = new TestCase( SECTION,	"typeof new String(void 0)",	                "object",	        typeof new String(void 0) );
63    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String(void 0); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String(void 0); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
64    array[item++] = new TestCase( SECTION,  "(new String(void 0)).valueOf()",               "undefined", (new String(void 0)).valueOf() );
65    array[item++] = new TestCase( SECTION,  "(new String(void 0)).toString",               String.prototype.toString,   (new String(void 0)).toString );
66
67    array[item++] = new TestCase( SECTION,	"typeof new String(null)",	            "object",	        typeof new String(null) );
68    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String(null); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String(null); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
69    array[item++] = new TestCase( SECTION,  "(new String(null)).valueOf()",         "null",             (new String(null)).valueOf() );
70    array[item++] = new TestCase( SECTION,  "(new String(null)).valueOf",         String.prototype.valueOf,   (new String(null)).valueOf );
71
72    array[item++] = new TestCase( SECTION,	"typeof new String(true)",	            "object",	        typeof new String(true) );
73    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String(true); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String(true); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
74    array[item++] = new TestCase( SECTION,  "(new String(true)).valueOf()",         "true",             (new String(true)).valueOf() );
75    array[item++] = new TestCase( SECTION,  "(new String(true)).charAt",         String.prototype.charAt,   (new String(true)).charAt );
76
77    array[item++] = new TestCase( SECTION,	"typeof new String(false)",	            "object",	        typeof new String(false) );
78    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String(false); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String(false); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
79    array[item++] = new TestCase( SECTION,  "(new String(false)).valueOf()",        "false",            (new String(false)).valueOf() );
80    array[item++] = new TestCase( SECTION,  "(new String(false)).charCodeAt",        String.prototype.charCodeAt,   (new String(false)).charCodeAt );
81
82    array[item++] = new TestCase( SECTION,	"typeof new String(new Boolean(true))",	       "object",	        typeof new String(new Boolean(true)) );
83    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String(new Boolean(true)); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String(new Boolean(true)); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
84    array[item++] = new TestCase( SECTION,  "(new String(new Boolean(true))).valueOf()",   "true",              (new String(new Boolean(true))).valueOf() );
85    array[item++] = new TestCase( SECTION,  "(new String(new Boolean(true))).indexOf",   String.prototype.indexOf,    (new String(new Boolean(true))).indexOf );
86
87    array[item++] = new TestCase( SECTION,	"typeof new String()",	                        "object",	        typeof new String() );
88    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String(); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String(); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
89    array[item++] = new TestCase( SECTION,  "(new String()).valueOf()",   '',                 (new String()).valueOf() );
90    array[item++] = new TestCase( SECTION,  "(new String()).lastIndexOf",   String.prototype.lastIndexOf,   (new String()).lastIndexOf );
91
92    array[item++] = new TestCase( SECTION,	"typeof new String('')",	    "object",	        typeof new String('') );
93    array[item++] = new TestCase( SECTION,	"var TESTSTRING = new String(''); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()", "[object String]",   eval("var TESTSTRING = new String(''); TESTSTRING.toString=Object.prototype.toString;TESTSTRING.toString()") );
94    array[item++] = new TestCase( SECTION,  "(new String('')).valueOf()",   '',                 (new String('')).valueOf() );
95    array[item++] = new TestCase( SECTION,  "(new String('')).split",   String.prototype.split,   (new String('')).split );
96
97    return ( array );
98}
99
100function test() {
101    for ( tc=0; tc < testcases.length; tc++ ) {
102        testcases[tc].passed = writeTestCaseResult(
103                            testcases[tc].expect,
104                            testcases[tc].actual,
105                            testcases[tc].description +" = "+
106                            testcases[tc].actual );
107
108        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
109    }
110    stopTest();
111    return ( testcases );
112}
113