jerryscript/tests/jerry/array-prototype-unshift.js
Szilagyi Adam 4924f9fd31
Remove ES_NEXT macro (#4915)
- remove all '#JERRY_ESNEXT' macro
- remove 5.1 build profile, update test runner accordingly (Note: all builtins are turn on by default)
- move tests from tests/jerry/esnext into tests/jerry, concatenate files with same names
- add skiplist to some snapshot tests that were supported only in 5.1
- fix doxygen issues that were hidden before (bc. of es.next macro)

Co-authored-by: Martin Negyokru negyokru@inf.u-szeged.hu
JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
2022-01-31 16:46:00 +01:00

130 lines
3.5 KiB
JavaScript

// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var array = []
assert(array.length === 0);
array.unshift("foo");
assert(array.length === 1);
assert(array[0] === "foo");
array.unshift(new Array())
assert(array.length === 2);
assert(array[0] instanceof Array);
assert(array[1] === "foo")
array.unshift(Infinity);
assert(array.length === 3);
assert(array[0] === Infinity);
assert(array[1] instanceof Array);
assert(array[2] === "foo")
array.unshift("bar", 0);
assert(array.length === 5);
assert(array[0] === "bar");
assert(array[1] === 0);
assert(array[2] === Infinity);
assert(array[3] instanceof Array);
assert(array[4] === "foo")
// Checking behavior when no length property defined
var obj = { unshift : Array.prototype.unshift };
assert(obj.length === undefined);
obj.unshift(1,2,3);
assert(obj.length === 3);
// Checking behavior when unable to get length
var obj = { unshift : Array.prototype.unshift };
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
try {
obj.unshift(1);
assert(false)
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}
// Checking behavior when unable to set length
var obj = { unshift : Array.prototype.unshift };
Object.defineProperty(obj, 'length', { 'set' : function () {throw new ReferenceError ("foo"); } });
try {
obj.unshift(2);
assert(false)
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}
// Checking behavior when unable shift elements
var obj = { unshift : Array.prototype.unshift, length : 1 };
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
try {
obj.unshift(3);
assert(false);
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}
var obj = { unshift : Array.prototype.unshift, length : 1 };
Object.defineProperty(obj, '0', { 'set' : function () {throw new ReferenceError ("foo"); } });
try {
obj.unshift(4);
assert(false);
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}
// Checking behavior when a property is not defined
var obj = { '0' : "foo", '2' : "bar", length : 3, unshift : Array.prototype.unshift };
assert(obj.unshift("baz") === 4);
assert(obj[0] === "baz");
assert(obj[1] === "foo");
assert(obj[2] === undefined);
assert(obj[3] === "bar");
/* ES v5.1 15.4.4.13.6.d.ii.
Checking behavior when the array is freezed */
try {
var arr = [0, 1];
Object.freeze(arr);
Array.prototype.unshift.call(arr, 2, 3);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
/* ES v5.1 15.4.4.13.6.e.i.
Checking behavior when the array has only one property and bigger length */
try {
var arr = { length : 9 };
Object.defineProperty(arr, '6', { value : 2 });
Array.prototype.unshift.call(arr, 2, 3);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
var arrayLike = {get 5() { throw "shouldn't throw"; }};
arrayLike.length = 10;
Array.prototype.unshift.call(arrayLike);