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