1 // Copyright (c) 2006 Apple Computer, Inc. All rights reserved. 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions 5 // are met: 6 // 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 10 // 2. Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following 12 // disclaimer in the documentation and/or other materials provided 13 // with the distribution. 14 // 15 // 3. Neither the name of the copyright holder(s) nor the names of any 16 // contributors may be used to endorse or promote products derived 17 // from this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 28 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 30 // OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 // Based on LayoutTests/fast/js/Object-keys.html 33 34 assertEquals(Object.keys(2), []); 35 assertEquals(Object.keys("foo"), ["0", "1", "2"]); 36 assertThrows(function () { Object.keys(null) }, TypeError); 37 assertThrows(function () { Object.keys(undefined) }, TypeError); 38 39 assertEquals(Object.keys({}), []); 40 assertEquals(Object.keys({a:null}), ['a']); 41 assertEquals(Object.keys({a:null, b:null}), ['a', 'b']); 42 assertEquals(Object.keys({b:null, a:null}), ['b', 'a']); 43 assertEquals(Object.keys([]), []); 44 assertEquals(Object.keys([null]), ['0']); 45 assertEquals(Object.keys([undefined]), ['0']); 46 assertEquals(Object.keys([null,null]), ['0', '1']); 47 assertEquals(Object.keys([null,null,,,,null]), ['0', '1', '5']); 48 assertEquals(Object.keys({__proto__:{a:null}}), []); 49 assertEquals(Object.keys({__proto__:[1,2,3]}), []); 50 var x = []; 51 x.__proto__ = [1, 2, 3]; 52 assertEquals(Object.keys(x), []); 53 assertEquals(Object.keys(function () {}), []); 54 55 assertEquals('string', typeof(Object.keys([1])[0])); 56 57 function argsTest(a, b, c) { 58 assertEquals(['0', '1', '2'], Object.keys(arguments)); 59 } 60 61 argsTest(1, 2, 3); 62 63 var literal = {a: 1, b: 2, c: 3}; 64 var keysBefore = Object.keys(literal); 65 assertEquals(['a', 'b', 'c'], keysBefore); 66 keysBefore[0] = 'x'; 67 var keysAfter = Object.keys(literal); 68 assertEquals(['a', 'b', 'c'], keysAfter); 69 assertEquals(['x', 'b', 'c'], keysBefore); 70 71 72 var o = [1, 2, 3]; 73 assertEquals(['0', '1', '2'], Object.keys(o)); 74 Object.defineProperty(o, '0', { 75 enumerable: false, 76 }); 77 assertEquals(['1', '2'], Object.keys(o)); 78 79 80 (function(){ 81 assertEquals(['0', '1', '2'], Object.keys(arguments)); 82 Object.defineProperty(arguments, '0', { 83 enumerable: false, 84 }); 85 assertEquals(['1', '2'], Object.keys(arguments)); 86 })(0,1,2); 87 88 89 (function(a, b){ 90 assertEquals(['0', '1', '2'], Object.keys(arguments)); 91 Object.defineProperty(arguments, '0', { 92 enumerable: false, 93 }); 94 assertEquals(['1', '2'], Object.keys(arguments)); 95 })(0,1,2); 96 97 var b = []; 98 assertEquals(0, Object.keys(b).length); 99 b[0] = undefined; 100 assertEquals(1, Object.keys(b).length); 101