v8/test/mjsunit/harmony/spread-array.js
arv 9502e91adb [es6] Spread in array literals
This allows you to put iterables into your array literals
and the will get spread into the array.

  let x = [0, ...range(1, 3)];  // [0, 1, 2]

This is done by treating the array literal up to the first
spread element as usual, including using a boiler plate
array, and then appending the remaining expressions and rest
expressions.

BUG=v8:3018
LOG=N

Review URL: https://codereview.chromium.org/1125183008

Cr-Commit-Position: refs/heads/master@{#28534}
2015-05-21 08:08:55 +00:00

180 lines
3.1 KiB
JavaScript

// Copyright 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.
// Flags: --harmony-spread-arrays --allow-natives-syntax
(function TestBasics() {
var a = [1, 2];
var b = [...a];
assertArrayEquals([1, 2], b)
assertArrayEquals(['a', 'b', 'c', 'd', 'e', 'f'],
['a', ...'bc', 'd', ...'ef'])
})();
var log = [];
function* gen(n) {
log.push(n, 1);
yield 1;
log.push(n, 2);
yield 2;
log.push(n, 3);
yield 3;
log.push(n, 'done');
}
function id(v) {
log.push(v);
return v;
}
(function TestGenerator() {
assertArrayEquals([1, 2, 3], [...gen('a')]);
assertArrayEquals(['x', 1, 2, 3, 'y', 1, 2, 3, 'z'],
['x', ...gen('a'), 'y', ...gen('b'), 'z']);
})();
(function TestOrderOfExecution() {
log = [];
assertArrayEquals(['x', 1, 2, 3, 'y', 1, 2, 3, 'z'],
[id('x'), ...gen('a'), id('y'), ...gen('b'), id('z')]);
assertArrayEquals([
'x', 'a', 1, 'a', 2, 'a', 3, 'a', 'done',
'y', 'b', 1, 'b', 2, 'b', 3, 'b', 'done',
'z'
], log);
})();
(function TestNotIterable() {
var a;
assertThrows(function() {
a = [...42];
}, TypeError);
assertSame(undefined, a);
})();
(function TestInvalidIterator() {
var iter = {
[Symbol.iterator]: 42
};
var a;
assertThrows(function() {
a = [...iter];
}, TypeError);
assertSame(undefined, a);
})();
(function TestIteratorNotAnObject() {
var iter = {
[Symbol.iterator]() {
return 42;
}
};
var a;
assertThrows(function() {
a = [...iter];
}, TypeError);
assertSame(undefined, a);
})();
(function TestIteratorNoNext() {
var iter = {
[Symbol.iterator]() {
return {};
}
};
var a;
assertThrows(function() {
a = [...iter];
}, TypeError);
assertSame(undefined, a);
})();
(function TestIteratorResultDoneThrows() {
function MyError() {}
var iter = {
[Symbol.iterator]() {
return {
next() {
return {
get done() {
throw new MyError();
}
}
}
};
}
};
var a;
assertThrows(function() {
a = [...iter];
}, MyError);
assertSame(undefined, a);
})();
(function TestIteratorResultValueThrows() {
function MyError() {}
var iter = {
[Symbol.iterator]() {
return {
next() {
return {
done: false,
get value() {
throw new MyError();
}
}
}
};
}
};
var a;
assertThrows(function() {
a = [...iter];
}, MyError);
assertSame(undefined, a);
})();
(function TestOptimize() {
function f() {
return [...'abc'];
}
assertArrayEquals(['a', 'b', 'c'], f());
%OptimizeFunctionOnNextCall(f);
assertArrayEquals(['a', 'b', 'c'], f());
})();
(function TestDeoptimize() {
var iter = {
[Symbol.iterator]() {
var i = 0;
return {
next() {
$DeoptimizeFunction(f);
return {value: ++i, done: i === 3};
}
};
}
};
function f() {
return [0, ...iter];
}
assertArrayEquals([0, 1, 2], f());
});