|
1 // |reftest| skip -- obsolete test |
|
2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 |
|
8 /** |
|
9 File Name: toString_1.js |
|
10 ECMA Section: Object.toString() |
|
11 Description: |
|
12 |
|
13 This checks the ToString value of Object objects under JavaScript 1.2. |
|
14 |
|
15 In JavaScript 1.2, Object.toString() |
|
16 |
|
17 Author: christine@netscape.com |
|
18 Date: 12 november 1997 |
|
19 */ |
|
20 |
|
21 var SECTION = "JS1_2"; |
|
22 var VERSION = "JS1_2"; |
|
23 startTest(); |
|
24 var TITLE = "Object.toString()"; |
|
25 |
|
26 writeHeaderToLog( SECTION + " "+ TITLE); |
|
27 |
|
28 var o = new Object(); |
|
29 |
|
30 new TestCase( SECTION, |
|
31 "var o = new Object(); o.toString()", |
|
32 "{}", |
|
33 o.toString() ); |
|
34 |
|
35 o = {}; |
|
36 |
|
37 new TestCase( SECTION, |
|
38 "o = {}; o.toString()", |
|
39 "{}", |
|
40 o.toString() ); |
|
41 |
|
42 o = { name:"object", length:0, value:"hello" } |
|
43 |
|
44 new TestCase( SECTION, |
|
45 "o = { name:\"object\", length:0, value:\"hello\" }; o.toString()", |
|
46 true, |
|
47 checkObjectToString(o.toString(), ['name:"object"', 'length:0', |
|
48 'value:"hello"'])); |
|
49 |
|
50 o = { name:"object", length:0, value:"hello", |
|
51 toString:new Function( "return this.value+''" ) } |
|
52 |
|
53 new TestCase( SECTION, |
|
54 "o = { name:\"object\", length:0, value:\"hello\", "+ |
|
55 "toString:new Function( \"return this.value+''\" ) }; o.toString()", |
|
56 "hello", |
|
57 o.toString() ); |
|
58 |
|
59 |
|
60 |
|
61 test(); |
|
62 |
|
63 /** |
|
64 * checkObjectToString |
|
65 * |
|
66 * In JS1.2, Object.prototype.toString returns a representation of the |
|
67 * object's properties as a string. However, the order of the properties |
|
68 * in the resulting string is not specified. This function compares the |
|
69 * resulting string with an array of strings to make sure that the |
|
70 * resulting string is some permutation of the strings in the array. |
|
71 */ |
|
72 function checkObjectToString(s, a) { |
|
73 var m = /^\{(.*)\}$/(s); |
|
74 if (!m) |
|
75 return false; // should begin and end with curly brackets |
|
76 var a2 = m[1].split(", "); |
|
77 if (a.length != a2.length) |
|
78 return false; // should be same length |
|
79 a.sort(); |
|
80 a2.sort(); |
|
81 for (var i=0; i < a.length; i++) { |
|
82 if (a[i] != a2[i]) |
|
83 return false; // should have identical elements |
|
84 } |
|
85 return true; |
|
86 } |
|
87 |