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