1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15var obj = {}; 16 17// Test if the toString fails. 18try { 19 obj.propertyIsEnumerable({ toString: function() { throw new ReferenceError ("foo"); } }); 20 21 assert (false); 22} catch (e) { 23 assert (e.message === "foo"); 24 assert (e instanceof ReferenceError); 25} 26 27// Test if the toObject fails. 28var obj1; 29try { 30 obj1.propertyIsEnumerable("fail"); 31 32 assert (false); 33} catch (e) { 34 assert (e instanceof TypeError); 35} 36 37var array = []; 38obj.prop = "bar"; 39array[0] = "bar"; 40 41assert (obj.propertyIsEnumerable('prop') === true); 42assert (array.propertyIsEnumerable(0) === true); 43 44assert (obj.propertyIsEnumerable('length') === false); 45assert (array.propertyIsEnumerable('length') === false); 46assert (Math.propertyIsEnumerable('random') === false); 47 48// Creating a new property 49Object.defineProperty(obj, 'prop1', { value: 'foo', enumerable: true }); 50Object.defineProperty(obj, 'prop2', { value: 'foo', enumerable: false }); 51Object.defineProperty(obj, 'prop3', { value: 'foo' }); 52assert (obj.propertyIsEnumerable('prop1') === true); 53assert (obj.propertyIsEnumerable('prop2') === false); 54assert (obj.propertyIsEnumerable('prop3') === false); 55 56Object.defineProperty(array, 'prop1', { value: 'foo', enumerable: true }); 57Object.defineProperty(array, 'prop2', { value: 'foo', enumerable: false }); 58Object.defineProperty(array, 'prop3', { value: 'foo' }); 59assert (array.propertyIsEnumerable('prop1') === true); 60assert (array.propertyIsEnumerable('prop2') === false); 61assert (array.propertyIsEnumerable('prop3') === false); 62 63// Modify an existing one 64Object.defineProperty(obj, 'prop', { value: 'foo', enumerable: false }); 65assert (obj.propertyIsEnumerable('prop') === false); 66Object.defineProperty(obj, 'prop', { value: 'foo', enumerable: true }); 67assert (obj.propertyIsEnumerable('prop') === true); 68 69Object.defineProperty(array, 0, { value: 'foo', enumerable: false }); 70assert (array.propertyIsEnumerable(0) === false); 71Object.defineProperty(array, 0, { value: 'foo', enumerable: true }); 72assert (array.propertyIsEnumerable(0) === true); 73 74// Enumerability of inherited properties 75function construct1() 76{ 77 this.prop1 = 'foo'; 78} 79 80function construct2() 81{ 82 this.prop2 = 'foo'; 83} 84 85construct2.prototype = new construct1; 86construct2.prototype.constructor = construct2; 87 88var obj2 = new construct2(); 89obj2.prop3 = 'foo'; 90 91assert (obj2.propertyIsEnumerable('prop3') === true); 92assert (obj2.propertyIsEnumerable('prop2') === true); 93assert (obj2.propertyIsEnumerable('prop1') === false); 94 95obj2.prop1 = 'foo'; 96 97assert (obj2.propertyIsEnumerable('prop1') === true); 98