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 array = ["foo", [], Infinity, 4] 16 17assert(array.length === 4); 18 19assert(array.shift() === "foo"); 20assert(array.length === 3); 21 22var a = array.shift(); 23assert(a instanceof Array); 24assert(array.length === 2); 25 26assert(array.shift() === Infinity); 27assert(array.length === 1); 28 29assert(array.shift() === 4); 30assert(array.length === 0); 31 32assert(array.shift() === undefined); 33assert(array.length === 0); 34 35var referenceErrorThrower = function () { 36 throw new ReferenceError ("foo"); 37} 38 39// Checking behavior when unable to get length 40var obj = { shift : Array.prototype.shift }; 41Object.defineProperty(obj, 'length', { 'get' : referenceErrorThrower }); 42 43try { 44 obj.shift(); 45 assert(false); 46} catch (e) { 47 assert(e.message === "foo"); 48 assert(e instanceof ReferenceError); 49} 50 51// Checking behavior when unable to set length 52var obj = { shift : Array.prototype.shift }; 53Object.defineProperty(obj, 'length', { 'set' : referenceErrorThrower }); 54 55try { 56 obj.shift(); 57 assert(false); 58} catch (e) { 59 assert(e.message === "foo"); 60 assert(e instanceof ReferenceError); 61} 62 63// Checking behavior when no length property defined 64var obj = { shift : Array.prototype.shift }; 65assert (obj.length === undefined) 66assert (obj.shift() === undefined) 67assert (obj.length === 0) 68 69// Checking behavior when unable to get element 70var obj = { shift : Array.prototype.shift, length : 1 }; 71Object.defineProperty(obj, '0', { 'get' : referenceErrorThrower }); 72 73try { 74 obj.shift(); 75 assert(false); 76} catch (e) { 77 assert(e.message === "foo"); 78 assert(e instanceof ReferenceError); 79} 80 81/* ES v5.1 15.4.4.9.7.c. 82 Checking behavior when the array is freezed */ 83try { 84 f = function () { throw new ReferenceError("getter"); }; 85 arr = { length : 9 }; 86 Object.defineProperty(arr, '8', { 'get' : f }); 87 Array.prototype.shift.call(arr); 88 assert(false); 89} catch (e) { 90 assert(e instanceof ReferenceError); 91 assert(e.message == "getter"); 92} 93 94/* ES v5.1 15.4.4.9.7.d.ii. 95 Checking behavior when the array is freezed */ 96try { 97 arr = { length : 9 }; 98 Object.defineProperty(arr, '8', { value : 8 }); 99 Object.defineProperty(arr, '7', { value : 7 }); 100 Array.prototype.shift.call(arr); 101 assert(false); 102} catch (e) { 103 assert(e instanceof TypeError); 104} 105 106/* ES v5.1 15.4.4.9.7.e.i. 107 Checking behavior when the first element is null */ 108try { 109 arr = { length : 9 }; 110 Object.defineProperty(arr, '0', { value : null }); 111 Array.prototype.shift.call(arr); 112 assert(false); 113} catch (e) { 114 assert(e instanceof TypeError); 115} 116 117/* ES v5.1 15.4.4.9.8. 118 Checking behavior when last element is not writable */ 119try { 120 arr = { length : 9 }; 121 Object.defineProperty(arr, '8', { writable : false }); 122 Array.prototype.shift.call(arr); 123 assert(false); 124} catch (e) { 125 assert(e instanceof TypeError); 126} 127 128/* ES v5.1 15.4.4.9.9. 129 Checking behavior when the array is freezed */ 130try { 131 arr = { length : 9 }; 132 Object.freeze(arr); 133 Array.prototype.shift.call(arr); 134 assert(false); 135} catch (e) { 136 assert(e instanceof TypeError); 137} 138