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    ECMA Section:       Object.toString()
25    Description:
26
27    This checks the ToString value of Object objects under JavaScript 1.2.
28
29    In JavaScript 1.2, Object.toString()
30
31    Author:             christine@netscape.com
32    Date:               12 november 1997
33*/
34
35    var SECTION = "JS1_2";
36    var VERSION = "JS1_2";
37    startTest();
38    var TITLE   = "Object.toString()";
39
40    writeHeaderToLog( SECTION + " "+ TITLE);
41
42    var testcases = new Array();
43
44    var o = new Object();
45
46    testcases[testcases.length] = new TestCase( SECTION,
47        "var o = new Object(); o.toString()",
48        "{}",
49        o.toString() );
50
51    o = {};
52
53    testcases[testcases.length] = new TestCase( SECTION,
54        "o = {}; o.toString()",
55        "{}",
56        o.toString() );
57
58    o = { name:"object", length:0, value:"hello" }
59
60    testcases[testcases.length] = new TestCase( SECTION,
61        "o = { name:\"object\", length:0, value:\"hello\" }; o.toString()",
62        true,
63        checkObjectToString(o.toString(), ['name:"object"', 'length:0',
64                                           'value:"hello"']));
65
66     o = { name:"object", length:0, value:"hello",
67        toString:new Function( "return this.value+''" ) }
68
69    testcases[testcases.length] = new TestCase( SECTION,
70        "o = { name:\"object\", length:0, value:\"hello\", "+
71            "toString:new Function( \"return this.value+''\" ) }; o.toString()",
72        "hello",
73        o.toString() );
74
75
76
77    test();
78
79function test() {
80    for ( tc=0; tc < testcases.length; tc++ ) {
81        testcases[tc].passed = writeTestCaseResult(
82                            testcases[tc].expect,
83                            testcases[tc].actual,
84                            testcases[tc].description +" = "+
85                            testcases[tc].actual );
86
87        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
88    }
89    stopTest();
90    return ( testcases );
91}
92
93/**
94 * checkObjectToString
95 *
96 * In JS1.2, Object.prototype.toString returns a representation of the
97 * object's properties as a string. However, the order of the properties
98 * in the resulting string is not specified. This function compares the
99 * resulting string with an array of strings to make sure that the
100 * resulting string is some permutation of the strings in the array.
101 */
102function checkObjectToString(s, a) {
103    var m = /^\{(.*)\}$/(s);
104    if (!m)
105       return false;	// should begin and end with curly brackets
106    var a2 = m[1].split(", ");
107    if (a.length != a2.length)
108        return false;	// should be same length
109    a.sort();
110    a2.sort();
111    for (var i=0; i < a.length; i++) {
112        if (a[i] != a2[i])
113           return false;	// should have identical elements
114    }
115    return true;
116}
117
118