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
24 ECMA Section: 9.9 Type Conversion: ToObject
27 undefined generate a runtime error
28 null generate a runtime error
29 boolean create a new Boolean object whose default
30 value is the value of the boolean.
31 number Create a new Number object whose default
32 value is the value of the number.
33 string Create a new String object whose default
34 value is the value of the string.
35 object Return the input argument (no conversion).
36 Author: christine@netscape.com
40 var VERSION = "ECMA_1";
42 var SECTION = "9.9-1";
44 writeHeaderToLog( SECTION + " Type Conversion: ToObject" );
46 var testcases = getTestCases();
48 // all tests must call a function that returns an array of TestCase objects.
51 function getTestCases() {
52 var array = new Array();
55 array[item++] = new TestCase( SECTION, "Object(true).valueOf()", true, (Object(true)).valueOf() );
56 array[item++] = new TestCase( SECTION, "typeof Object(true)", "object", typeof Object(true) );
57 array[item++] = new TestCase( SECTION, "(Object(true)).__proto__", Boolean.prototype, (Object(true)).__proto__ );
59 array[item++] = new TestCase( SECTION, "Object(false).valueOf()", false, (Object(false)).valueOf() );
60 array[item++] = new TestCase( SECTION, "typeof Object(false)", "object", typeof Object(false) );
61 array[item++] = new TestCase( SECTION, "(Object(true)).__proto__", Boolean.prototype, (Object(true)).__proto__ );
63 array[item++] = new TestCase( SECTION, "Object(0).valueOf()", 0, (Object(0)).valueOf() );
64 array[item++] = new TestCase( SECTION, "typeof Object(0)", "object", typeof Object(0) );
65 array[item++] = new TestCase( SECTION, "(Object(0)).__proto__", Number.prototype, (Object(0)).__proto__ );
67 array[item++] = new TestCase( SECTION, "Object(-0).valueOf()", -0, (Object(-0)).valueOf() );
68 array[item++] = new TestCase( SECTION, "typeof Object(-0)", "object", typeof Object(-0) );
69 array[item++] = new TestCase( SECTION, "(Object(-0)).__proto__", Number.prototype, (Object(-0)).__proto__ );
71 array[item++] = new TestCase( SECTION, "Object(1).valueOf()", 1, (Object(1)).valueOf() );
72 array[item++] = new TestCase( SECTION, "typeof Object(1)", "object", typeof Object(1) );
73 array[item++] = new TestCase( SECTION, "(Object(1)).__proto__", Number.prototype, (Object(1)).__proto__ );
75 array[item++] = new TestCase( SECTION, "Object(-1).valueOf()", -1, (Object(-1)).valueOf() );
76 array[item++] = new TestCase( SECTION, "typeof Object(-1)", "object", typeof Object(-1) );
77 array[item++] = new TestCase( SECTION, "(Object(-1)).__proto__", Number.prototype, (Object(-1)).__proto__ );
79 array[item++] = new TestCase( SECTION, "Object(Number.MAX_VALUE).valueOf()", 1.7976931348623157e308, (Object(Number.MAX_VALUE)).valueOf() );
80 array[item++] = new TestCase( SECTION, "typeof Object(Number.MAX_VALUE)", "object", typeof Object(Number.MAX_VALUE) );
81 array[item++] = new TestCase( SECTION, "(Object(Number.MAX_VALUE)).__proto__", Number.prototype, (Object(Number.MAX_VALUE)).__proto__ );
83 array[item++] = new TestCase( SECTION, "Object(Number.MIN_VALUE).valueOf()", 5e-324, (Object(Number.MIN_VALUE)).valueOf() );
84 array[item++] = new TestCase( SECTION, "typeof Object(Number.MIN_VALUE)", "object", typeof Object(Number.MIN_VALUE) );
85 array[item++] = new TestCase( SECTION, "(Object(Number.MIN_VALUE)).__proto__", Number.prototype, (Object(Number.MIN_VALUE)).__proto__ );
87 array[item++] = new TestCase( SECTION, "Object(Number.POSITIVE_INFINITY).valueOf()", Number.POSITIVE_INFINITY, (Object(Number.POSITIVE_INFINITY)).valueOf() );
88 array[item++] = new TestCase( SECTION, "typeof Object(Number.POSITIVE_INFINITY)", "object", typeof Object(Number.POSITIVE_INFINITY) );
89 array[item++] = new TestCase( SECTION, "(Object(Number.POSITIVE_INFINITY)).__proto__", Number.prototype, (Object(Number.POSITIVE_INFINITY)).__proto__ );
91 array[item++] = new TestCase( SECTION, "Object(Number.NEGATIVE_INFINITY).valueOf()", Number.NEGATIVE_INFINITY, (Object(Number.NEGATIVE_INFINITY)).valueOf() );
92 array[item++] = new TestCase( SECTION, "typeof Object(Number.NEGATIVE_INFINITY)", "object", typeof Object(Number.NEGATIVE_INFINITY) );
93 array[item++] = new TestCase( SECTION, "(Object(Number.NEGATIVE_INFINITY)).__proto__", Number.prototype, (Object(Number.NEGATIVE_INFINITY)).__proto__ );
95 array[item++] = new TestCase( SECTION, "Object(Number.NaN).valueOf()", Number.NaN, (Object(Number.NaN)).valueOf() );
96 array[item++] = new TestCase( SECTION, "typeof Object(Number.NaN)", "object", typeof Object(Number.NaN) );
97 array[item++] = new TestCase( SECTION, "(Object(Number.NaN)).__proto__", Number.prototype, (Object(Number.NaN)).__proto__ );
99 array[item++] = new TestCase( SECTION, "Object('a string').valueOf()", "a string", (Object("a string")).valueOf() );
100 array[item++] = new TestCase( SECTION, "typeof Object('a string')", "object", typeof (Object("a string")) );
101 array[item++] = new TestCase( SECTION, "(Object('a string')).__proto__", String.prototype, (Object("a string")).__proto__ );
103 array[item++] = new TestCase( SECTION, "Object('').valueOf()", "", (Object("")).valueOf() );
104 array[item++] = new TestCase( SECTION, "typeof Object('')", "object", typeof (Object("")) );
105 array[item++] = new TestCase( SECTION, "(Object('')).__proto__", String.prototype, (Object("")).__proto__ );
107 array[item++] = new TestCase( SECTION, "Object('\\r\\t\\b\\n\\v\\f').valueOf()", "\r\t\b\n\v\f", (Object("\r\t\b\n\v\f")).valueOf() );
108 array[item++] = new TestCase( SECTION, "typeof Object('\\r\\t\\b\\n\\v\\f')", "object", typeof (Object("\\r\\t\\b\\n\\v\\f")) );
109 array[item++] = new TestCase( SECTION, "(Object('\\r\\t\\b\\n\\v\\f')).__proto__", String.prototype, (Object("\\r\\t\\b\\n\\v\\f")).__proto__ );
111 array[item++] = new TestCase( SECTION, "Object( '\\\'\\\"\\' ).valueOf()", "\'\"\\", (Object("\'\"\\")).valueOf() );
112 array[item++] = new TestCase( SECTION, "typeof Object( '\\\'\\\"\\' )", "object", typeof Object("\'\"\\") );
113 array[item++] = new TestCase( SECTION, "Object( '\\\'\\\"\\' ).__proto__", String.prototype, (Object("\'\"\\")).__proto__ );
115 array[item++] = new TestCase( SECTION, "Object( new MyObject(true) ).valueOf()", true, eval("Object( new MyObject(true) ).valueOf()") );
116 array[item++] = new TestCase( SECTION, "typeof Object( new MyObject(true) )", "object", eval("typeof Object( new MyObject(true) )") );
117 array[item++] = new TestCase( SECTION, "(Object( new MyObject(true) )).toString()", "[object Object]", eval("(Object( new MyObject(true) )).toString()") );
123 for ( tc = 0; tc < testcases.length; tc++ ) {
125 testcases[tc].passed = writeTestCaseResult(
126 testcases[tc].expect,
127 testcases[tc].actual,
128 testcases[tc].description +" = "+
129 testcases[tc].actual );
131 testcases[tc].reason +=
132 ( testcases[tc].passed ) ? "" : "wrong value ";
137 // all tests must return an array of TestCase objects
138 return ( testcases );
140 function MyObject( value ) {
142 this.valueOf = new Function ( "return this.value" );