michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 28 August 2001 michael@0: * michael@0: * SUMMARY: A [DontEnum] prop, if overridden, should appear in for-in loops. michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=90596 michael@0: * michael@0: * NOTE: some inefficiencies in the test are made for the sake of readability. michael@0: * For example, we quote string values like "Hi" in lines like this: michael@0: * michael@0: * actual = enumerateThis(obj); michael@0: * expect = '{prop:"Hi"}'; michael@0: * michael@0: * But enumerateThis(obj) gets literal value Hi for obj.prop, not michael@0: * literal "Hi". We take care of all these details in the michael@0: * compactThis(), sortThis() functions. Sorting properties michael@0: * alphabetically is necessary for the test to work in Rhino. michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 90596; michael@0: var summary = '[DontEnum] props (if overridden) should appear in for-in loops'; michael@0: var cnCOMMA = ','; michael@0: var cnCOLON = ':'; michael@0: var cnLBRACE = '{'; michael@0: var cnRBRACE = '}'; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: var obj = {}; michael@0: michael@0: michael@0: status = inSection(1); michael@0: obj = {toString:9}; michael@0: actual = enumerateThis(obj); michael@0: expect = '{toString:9}'; michael@0: addThis(); michael@0: michael@0: status = inSection(2); michael@0: obj = {hasOwnProperty:"Hi"}; michael@0: actual = enumerateThis(obj); michael@0: expect = '{hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: michael@0: status = inSection(3); michael@0: obj = {toString:9, hasOwnProperty:"Hi"}; michael@0: actual = enumerateThis(obj); michael@0: expect = '{toString:9, hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: michael@0: status = inSection(4); michael@0: obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}; michael@0: actual = enumerateThis(obj); michael@0: expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: michael@0: michael@0: // TRY THE SAME THING IN EVAL CODE michael@0: var s = ''; michael@0: michael@0: status = inSection(5); michael@0: s = 'obj = {toString:9}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{toString:9}'; michael@0: addThis(); michael@0: michael@0: status = inSection(6); michael@0: s = 'obj = {hasOwnProperty:"Hi"}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: michael@0: status = inSection(7); michael@0: s = 'obj = {toString:9, hasOwnProperty:"Hi"}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{toString:9, hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: michael@0: status = inSection(8); michael@0: s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: michael@0: michael@0: // TRY THE SAME THING IN FUNCTION CODE michael@0: function A() michael@0: { michael@0: status = inSection(9); michael@0: var s = 'obj = {toString:9}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{toString:9}'; michael@0: addThis(); michael@0: } michael@0: A(); michael@0: michael@0: function B() michael@0: { michael@0: status = inSection(10); michael@0: var s = 'obj = {hasOwnProperty:"Hi"}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: } michael@0: B(); michael@0: michael@0: function C() michael@0: { michael@0: status = inSection(11); michael@0: var s = 'obj = {toString:9, hasOwnProperty:"Hi"}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{toString:9, hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: } michael@0: C(); michael@0: michael@0: function D() michael@0: { michael@0: status = inSection(12); michael@0: var s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}'; michael@0: eval(s); michael@0: actual = enumerateThis(obj); michael@0: expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}'; michael@0: addThis(); michael@0: } michael@0: D(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function enumerateThis(obj) michael@0: { michael@0: var arr = new Array(); michael@0: michael@0: for (var prop in obj) michael@0: { michael@0: arr.push(prop + cnCOLON + obj[prop]); michael@0: } michael@0: michael@0: var ret = addBraces(String(arr)); michael@0: return ret; michael@0: } michael@0: michael@0: michael@0: function addBraces(text) michael@0: { michael@0: return cnLBRACE + text + cnRBRACE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Sort properties alphabetically so the test will work in Rhino michael@0: */ michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = sortThis(actual); michael@0: expectedvalues[UBound] = sortThis(expect); michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Takes a string of the form '{"c", "b", "a", 2}' and returns '{2,a,b,c}' michael@0: */ michael@0: function sortThis(sList) michael@0: { michael@0: sList = compactThis(sList); michael@0: sList = stripBraces(sList); michael@0: var arr = sList.split(cnCOMMA); michael@0: arr = arr.sort(); michael@0: var ret = String(arr); michael@0: ret = addBraces(ret); michael@0: return ret; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Strips out any whitespace or quotes from the text - michael@0: */ michael@0: function compactThis(text) michael@0: { michael@0: var charCode = 0; michael@0: var ret = ''; michael@0: michael@0: for (var i=0; i