1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_2/Objects/toString-001.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +// |reftest| skip -- obsolete test 1.5 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 + 1.11 +/** 1.12 + File Name: toString_1.js 1.13 + ECMA Section: Object.toString() 1.14 + Description: 1.15 + 1.16 + This checks the ToString value of Object objects under JavaScript 1.2. 1.17 + 1.18 + In JavaScript 1.2, Object.toString() 1.19 + 1.20 + Author: christine@netscape.com 1.21 + Date: 12 november 1997 1.22 +*/ 1.23 + 1.24 +var SECTION = "JS1_2"; 1.25 +var VERSION = "JS1_2"; 1.26 +startTest(); 1.27 +var TITLE = "Object.toString()"; 1.28 + 1.29 +writeHeaderToLog( SECTION + " "+ TITLE); 1.30 + 1.31 +var o = new Object(); 1.32 + 1.33 +new TestCase( SECTION, 1.34 + "var o = new Object(); o.toString()", 1.35 + "{}", 1.36 + o.toString() ); 1.37 + 1.38 +o = {}; 1.39 + 1.40 +new TestCase( SECTION, 1.41 + "o = {}; o.toString()", 1.42 + "{}", 1.43 + o.toString() ); 1.44 + 1.45 +o = { name:"object", length:0, value:"hello" } 1.46 + 1.47 + new TestCase( SECTION, 1.48 + "o = { name:\"object\", length:0, value:\"hello\" }; o.toString()", 1.49 + true, 1.50 + checkObjectToString(o.toString(), ['name:"object"', 'length:0', 1.51 + 'value:"hello"'])); 1.52 + 1.53 +o = { name:"object", length:0, value:"hello", 1.54 + toString:new Function( "return this.value+''" ) } 1.55 + 1.56 + new TestCase( SECTION, 1.57 + "o = { name:\"object\", length:0, value:\"hello\", "+ 1.58 + "toString:new Function( \"return this.value+''\" ) }; o.toString()", 1.59 + "hello", 1.60 + o.toString() ); 1.61 + 1.62 + 1.63 + 1.64 +test(); 1.65 + 1.66 +/** 1.67 + * checkObjectToString 1.68 + * 1.69 + * In JS1.2, Object.prototype.toString returns a representation of the 1.70 + * object's properties as a string. However, the order of the properties 1.71 + * in the resulting string is not specified. This function compares the 1.72 + * resulting string with an array of strings to make sure that the 1.73 + * resulting string is some permutation of the strings in the array. 1.74 + */ 1.75 +function checkObjectToString(s, a) { 1.76 + var m = /^\{(.*)\}$/(s); 1.77 + if (!m) 1.78 + return false; // should begin and end with curly brackets 1.79 + var a2 = m[1].split(", "); 1.80 + if (a.length != a2.length) 1.81 + return false; // should be same length 1.82 + a.sort(); 1.83 + a2.sort(); 1.84 + for (var i=0; i < a.length; i++) { 1.85 + if (a[i] != a2[i]) 1.86 + return false; // should have identical elements 1.87 + } 1.88 + return true; 1.89 +} 1.90 +