Merge pull request #378 from bocoup/Array.of

Add tests for Array.of
This commit is contained in:
Brian Terlson 2015-07-27 17:27:20 -07:00
commit d40961b334
25 changed files with 432 additions and 312 deletions

View File

@ -1,17 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
function Empty() {}
Empty.of = Array.of;
Object.defineProperty(Empty.prototype, "length", {get: function() { return 0; }});
assert.throws(TypeError, function() { Empty.of(); });

View File

@ -1,23 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var a = Array.of();
var elem = [];
assert.sameValue(a.length, 0);
a = Array.of(undefined, null, 3.14, elem);
assert(compareArray(a, [undefined, null, 3.14, elem]));
a = [];
for (var i = 0; i < 1000; i++) {
a[i] = i;
}
assert(compareArray(Array.of.apply(null, a), a));

View File

@ -1,36 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var hits = 0;
var lastObj = null, lastVal = undefined;
function setter(v) {
hits++;
lastObj = this;
lastVal = v;
}
// when the setter is on the new object
function Pack() {
Object.defineProperty(this, "length", {set: setter});
}
Pack.of = Array.of;
var pack = Pack.of("wolves", "cards", "cigarettes", "lies");
assert(compareArray(lastObj, pack));
assert.sameValue(lastVal, 4);
// when the setter is on the new object's prototype
function Bevy() {}
Object.defineProperty(Bevy.prototype, "length", {set: setter});
Bevy.of = Array.of;
var bevy = Bevy.of("quail");
assert(compareArray(lastObj, bevy));
assert.sameValue(lastVal, 1);

View File

@ -1,37 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of can be transplanted to other classes.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var hits = 0;
function Bag() {
hits++;
}
Bag.of = Array.of;
hits = 0;
var actual = Bag.of("zero", "one");
assert.sameValue(hits, 1);
hits = 0;
var expected = new Bag;
expected[0] = "zero";
expected[1] = "one";
expected.length = 2;
assert(compareArray(actual, expected));
hits = 0;
actual = Array.of.call(Bag, "zero", "one");
assert.sameValue(hits, 1);
assert(compareArray(actual, expected));

View File

@ -1,45 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
// Array.of Check superficial features.
var desc = Object.getOwnPropertyDescriptor(Array, "of");
assert.sameValue(desc.configurable, true);
assert.sameValue(desc.enumerable, false);
assert.sameValue(desc.writable, true);
assert.sameValue(Array.of.length, 0);
assert.throws(TypeError, function() { new Array.of() }); // not a constructor
// When the this-value passed in is not a constructor, the result is an array.
[
undefined,
null,
false,
"cow",
NaN,
67,
Infinity,
-Infinity,
Math.cos, // builtin functions with no [[Construct]] slot
Math.cos.bind(Math) // bound builtin functions with no [[Construct]] slot
].forEach(function(val) {
assert.sameValue(Array.isArray(Array.of.call(val, val)), true);
});
var boundFn = (function() {}).bind(null);
var instance = Array.of.call(boundFn, 1, 2, 3);
assert.sameValue(instance.length, 3);
assert.sameValue(instance instanceof boundFn, true);
assert.sameValue(Array.isArray(instance), false);

View File

@ -1,20 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of does not leave holes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
assert(compareArray(Array.of(undefined), [undefined]));
assert(compareArray(Array.of(undefined, undefined), [undefined, undefined]));
assert(compareArray(Array.of.apply(null, [,,undefined]), [undefined, undefined, undefined]));
assert(compareArray(Array.of.apply(null, Array(4)), [undefined, undefined, undefined, undefined]));

View File

@ -1,32 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of does not trigger prototype setters.
(It defines elements rather than assigning to them.)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
function Bag() {}
Bag.of = Array.of;
var status = "pass";
Object.defineProperty(Array.prototype, "0", {set: function(v) {status = "FAIL 1"}});
assert.sameValue(Array.of(1)[0], 1);
assert.sameValue(status, "pass");
Object.defineProperty(Bag.prototype, "0", {set: function(v) {status = "FAIL 2"}});
assert.sameValue(Bag.of(1)[0], 1);
assert.sameValue(status, "pass");

View File

@ -1,28 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of makes real arrays.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
---*/
function check(a) {
assert.sameValue(Object.getPrototypeOf(a), Array.prototype);
assert.sameValue(Array.isArray(a), true);
a[9] = 9;
assert.sameValue(a.length, 10);
}
check(Array.of());
check(Array.of(0));
check(Array.of(0, 1, 2));
var f = Array.of;
check(f());

View File

@ -1,26 +0,0 @@
// Copyright (c) 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
The Array.of() method creates a new Array instance
with a variable number of arguments, regardless of
number or type of the arguments.
Array.of passes the number of arguments to the constructor it calls.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of
includes: [compareArray.js]
---*/
var hits = 0;
function Herd(n) {
assert.sameValue(arguments.length, 1);
assert.sameValue(n, 5);
hits++;
}
Herd.of = Array.of;
Herd.of("sheep", "cattle", "elephants", "whales", "seals");
assert.sameValue(hits, 1);

View File

@ -1,24 +0,0 @@
// Copyright (c) 2014 Hank Yates. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3_T1
description: Testing Array#of when passed Strings
author: Hank Yates (hankyates@gmail.com)
includes: [runTestCase.js]
---*/
runTestCase(function () {
var testArr = Array.of('testString', 'anotherTestString');
if (testArr[0] !== 'testString') {
return false;
}
if (testArr[1] !== 'anotherTestString') {
return false;
}
return true;
});

View File

@ -1,24 +0,0 @@
// Copyright (c) 2014 Hank Yates. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3_T2
description: Testing Array#of when passed single argument
author: Hank Yates (hankyates@gmail.com)
includes: [runTestCase.js]
---*/
runTestCase(function () {
var testArr = Array.of(3);
if (testArr.length !== 1) {
return false;
}
if (testArr[0] !== 3) {
return false;
}
return true;
});

View File

@ -0,0 +1,38 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: Passes the number of arguments to the constructor it calls.
info: >
Array.of ( ...items )
1. Let len be the actual number of arguments passed to this function.
2. Let items be the List of arguments passed to this function.
3. Let C be the this value.
4. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
...
---*/
var length;
var hits = 0;
function C(len) {
length = len;
hits++;
}
Array.of.call(C);
assert.sameValue(length, 0, '`Array.of.call(C);` called `new C(0)`');
assert.sameValue(hits, 1, 'Called constructor once per call');
Array.of.call(C, 'a', 'b')
assert.sameValue(length, 2, '`Array.of.call(C, "a", "b"));` called `new C(2)`');
assert.sameValue(hits, 2, 'Called constructor once per call');
Array.of.call(C, false, null, undefined);
assert.sameValue(
length, 3,
'`Array.of.call(C, false, null, undefined);` called `new C(3)`'
);
assert.sameValue(hits, 3, 'Called constructor once per call');

View File

@ -0,0 +1,44 @@
// Copyright (c) 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Array.of method creates a new Array with a variable number of arguments.
info: >
22.1.2.3 Array.of ( ...items )
...
7. Let k be 0.
8. Repeat, while k < len
a. Let kValue be items[k].
b. Let Pk be ToString(k).
c. Let defineStatus be CreateDataPropertyOrThrow(A,Pk, kValue).
d. ReturnIfAbrupt(defineStatus).
e. Increase k by 1.
9. Let setStatus be Set(A, "length", len, true).
10. ReturnIfAbrupt(setStatus).
11. Return A.
---*/
var a1 = Array.of('Mike', 'Rick', 'Leo');
assert.sameValue(
a1.length, 3,
'The new array length is the same as the arguments size'
);
assert.sameValue(a1[0], 'Mike', 'set each property in order - #1');
assert.sameValue(a1[1], 'Rick', 'set each property in order - #2');
assert.sameValue(a1[2], 'Leo', 'set each property in order - #3');
var a2 = Array.of(undefined, false, null, undefined);
assert.sameValue(
a2.length, 4,
'Creates an array from the arguments, regarless of their type values'
);
assert.sameValue(a2[0], undefined, 'set each property in order - #1');
assert.sameValue(a2[1], false, 'set each property in order - #2');
assert.sameValue(a2[2], null, 'set each property in order - #3');
assert.sameValue(a2[3], undefined, 'set each property in order - #4');
var a3 = Array.of();
assert.sameValue(a3.length, 0, 'Array.of() returns an empty array');

View File

@ -0,0 +1,29 @@
// Copyright (c) 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: Array.of does not use prototype properties for arguments.
info: >
It defines elements rather than assigning to them.
---*/
Object.defineProperty(Array.prototype, "0", {
set: function(v) {
$ERROR('Should define own properties');
}
});
var arr = Array.of(true);
assert.sameValue(arr[0], true);
function Custom() {}
Object.defineProperty(Custom.prototype, "0", {
set: function(v) {
$ERROR('Should define own properties');
}
});
var custom = Array.of.call(Custom, true);
assert.sameValue(custom[0], true);

View File

@ -0,0 +1,21 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Array.of.length value and property descriptor
info: >
Array.of ( ...items )
The length property of the of function is 0.
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.of.length, 0,
'The value of `Array.of.length` is `0`'
);
verifyNotEnumerable(Array.of, 'length');
verifyNotWritable(Array.of, 'length');
verifyConfigurable(Array.of, 'length');

View File

@ -0,0 +1,22 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Array.of.name value and property descriptor
info: >
Array.of ( ...items )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.of.name, 'of',
'The value of `Array.of.name` is `"of"`'
);
verifyNotEnumerable(Array.of, 'name');
verifyNotWritable(Array.of, 'name');
verifyConfigurable(Array.of, 'name');

View File

@ -0,0 +1,11 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Array.of is not a constructor.
---*/
assert.throws(TypeError, function() {
new Array.of();
});

View File

@ -0,0 +1,17 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Array.of property descriptor
info: >
Array.of ( ...items )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
verifyNotEnumerable(Array, 'of');
verifyWritable(Array, 'of');
verifyConfigurable(Array, 'of');

View File

@ -0,0 +1,37 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Returns an instance from a custom constructor.
info: >
Array.of ( ...items )
...
4. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
...
11. Return A.
---*/
function Coop() {}
var coop = Array.of.call(Coop, 'Mike', 'Rick', 'Leo');
assert.sameValue(
coop.length, 3,
'Sets a length property with the number of arguments'
);
assert.sameValue(
coop[0], 'Mike',
'Sets each argument in order as integer properties - #1 argument'
);
assert.sameValue(
coop[1], 'Rick',
'Sets each argument in order as integer properties - #2 argument'
);
assert.sameValue(
coop[2], 'Leo',
'Sets each argument in order as integer properties - #3 argument'
);
assert(coop instanceof Coop, 'Returns an instance from a custom constructor');

View File

@ -0,0 +1,40 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Returns a new Array.
info: >
Array.of ( ...items )
1. Let len be the actual number of arguments passed to this function.
2. Let items be the List of arguments passed to this function.
3. Let C be the this value.
4. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
5. Else,
b. Let A be ArrayCreate(len).
...
11. Return A.
---*/
var result = Array.of();
assert(result instanceof Array, 'Array.of() returns a new Array');
result = Array.of.call(undefined);
assert(
result instanceof Array,
'this is not a constructor'
);
result = Array.of.call(Math.cos);
assert(
result instanceof Array,
'this is a builtin function with no [[Construct]] slot'
);
result = Array.of.call(Math.cos.bind(Math));
assert(
result instanceof Array,
'this is a bound builtin function with no [[Construct]] slot'
);

View File

@ -0,0 +1,27 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Return abrupt from this' constructor
info: >
Array.of ( ...items )
1. Let len be the actual number of arguments passed to this function.
2. Let items be the List of arguments passed to this function.
3. Let C be the this value.
4. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
5. Else,
b. Let A be ArrayCreate(len).
6. ReturnIfAbrupt(A).
...
---*/
function T() {
throw new Test262Error();
}
assert.throws(Test262Error, function() {
Array.of.call(T);
});

View File

@ -0,0 +1,38 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Return abrupt from Data Property creation
info: >
Array.of ( ...items )
...
7. Let k be 0.
8. Repeat, while k < len
a. Let kValue be items[k].
b. Let Pk be ToString(k).
c. Let defineStatus be CreateDataPropertyOrThrow(A,Pk, kValue).
d. ReturnIfAbrupt(defineStatus).
...
7.3.6 CreateDataPropertyOrThrow (O, P, V)
...
3. Let success be CreateDataProperty(O, P, V).
4. ReturnIfAbrupt(success).
...
features: [Proxy]
---*/
function T() {
return new Proxy({}, {
defineProperty: function() {
throw new Test262Error();
}
});
}
assert.throws(Test262Error, function() {
Array.of.call(T, 'Bob');
});

View File

@ -0,0 +1,46 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Return abrupt from Data Property creation
info: >
Array.of ( ...items )
...
7. Let k be 0.
8. Repeat, while k < len
a. Let kValue be items[k].
b. Let Pk be ToString(k).
c. Let defineStatus be CreateDataPropertyOrThrow(A,Pk, kValue).
d. ReturnIfAbrupt(defineStatus).
...
7.3.6 CreateDataPropertyOrThrow (O, P, V)
...
3. Let success be CreateDataProperty(O, P, V).
4. ReturnIfAbrupt(success).
5. If success is false, throw a TypeError exception.
...
---*/
function T1() {
Object.preventExtensions(this);
}
assert.throws(TypeError, function() {
Array.of.call(T1, 'Bob');
});
function T2() {
Object.defineProperty(this, 0, {
configurable: false,
writable: true,
enumerable: true
});
}
assert.throws(TypeError, function() {
Array.of.call(T2, 'Bob');
})

View File

@ -0,0 +1,26 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Return abrupt from setting the length property.
info: >
Array.of ( ...items )
...
9. Let setStatus be Set(A, "length", len, true).
10. ReturnIfAbrupt(setStatus).
...
---*/
function T() {
Object.defineProperty(this, 'length', {
set: function() {
throw new Test262Error();
}
});
}
assert.throws(Test262Error, function() {
Array.of.call(T);
});

View File

@ -0,0 +1,36 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.3
description: >
Calls the length setter if available
info: >
Array.of ( ...items )
...
9. Let setStatus be Set(A, "length", len, true).
...
---*/
var hits = 0;
var value;
var _this_;
function Pack() {
Object.defineProperty(this, "length", {
set: function(len) {
hits++;
value = len;
_this_ = this;
}
});
}
var result = Array.of.call(Pack, 'wolves', 'cards', 'cigarettes', 'lies');
assert.sameValue(hits, 1, 'instance length setter called once');
assert.sameValue(
value, 4,
'setter is called with the number of Array.of arguments'
);
assert.sameValue(_this_, result, 'setter is called with the new object');