Skip to content
Snippets Groups Projects
Commit 69b89d85 authored by Mike Pennisi's avatar Mike Pennisi
Browse files

Add tests for destructuring assignment

parent 23b997dc
Branches
No related tags found
No related merge requests found
Showing
with 393 additions and 0 deletions
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When a `yield` token appears within the DestructuringAssignmentTarget of a
nested destructuring assignment and outside of a generator function body,
it should behave as an IdentifierExpression.
es6id: 12.14.5.3
flags: [noStrict]
---*/
var value = [86];
var yield = 'prop';
var x = {};
var result;
result = [...[x[yield]]] = value;
assert.sameValue(result, value);
assert.sameValue(x.prop, 86);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When DestructuringAssignmentTarget is an array literal, it should be parsed
parsed as a DestructuringAssignmentPattern and evaluated as a destructuring
assignment.
es6id: 12.14.5.3
---*/
var value = [1, 2, 3];
var x, result;
result = [...[x]] = value;
assert.sameValue(result, value);
assert.sameValue(x, 1);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
It is a Syntax Error if LeftHandSideExpression is either an ObjectLiteral
or an ArrayLiteral and if the lexical token sequence matched by
LeftHandSideExpression cannot be parsed with no tokens left over using
AssignmentPattern as the goal symbol.
es6id: 12.14.5.1
negative: SyntaxError
---*/
[...{ get x() {} }] = [[]];
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When DestructuringAssignmentTarget is an object literal and the iterable
emits `null` as the only value, an array with a single `null` element
should be used as the value of the nested DestructuringAssignment.
es6id: 12.14.5.3
---*/
var value = [null];
var result, x, length;
result = [...{ 0: x, length }] = value;
assert.sameValue(result, value);
assert.sameValue(x, null);
assert.sameValue(length, 1);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When DestructuringAssignmentTarget is an array literal and the iterable is
an array with a "hole", an array with a single `undefined` element should
be used as the value of the nested DestructuringAssignment.
es6id: 12.14.5.3
---*/
var value = [ , ];
var result, x, length;
result = [...{ 0: x, length }] = value;
assert.sameValue(result, value);
assert.sameValue(x, undefined);
assert.sameValue(length, 1);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When DestructuringAssignmentTarget is an array literal and the iterable
emits `undefined` as the only value, an array with a single `undefined`
element should be used as the value of the nested DestructuringAssignment.
es6id: 12.14.5.3
---*/
var value = [undefined];
var result, x, length;
result = [...{ 0: x, length }] = value;
assert.sameValue(result, value);
assert.sameValue(x, undefined);
assert.sameValue(length, 1);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When DestructuringAssignmentTarget is an obect literal and the iterable is
emits no values, an empty array should be used as the value of the nested
DestructuringAssignment.
es6id: 12.14.5.3
---*/
var value = [];
var result, x, length;
result = [...{ 0: x, length }] = value;
assert.sameValue(result, value);
assert.sameValue(x, undefined);
assert.sameValue(length, 0);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When a `yield` token appears within the Initializer of a nested
destructuring assignment and within a generator function body, it should
behave as a YieldExpression.
es6id: 12.14.5.3
features: [generators]
---*/
var value = [{}];
var assignmentResult, iterationResult, iter, x;
iter = (function*() {
assignmentResult = [...{ x = yield }] = value;
}());
iterationResult = iter.next();
assert.sameValue(assignmentResult, undefined);
assert.sameValue(iterationResult.value, undefined);
assert.sameValue(iterationResult.done, false);
assert.sameValue(x, undefined);
iterationResult = iter.next(4);
assert.sameValue(assignmentResult, value);
assert.sameValue(iterationResult.value, undefined);
assert.sameValue(iterationResult.done, true);
assert.sameValue(x, 4);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When a `yield` token appears within the Initializer of a nested
destructuring assignment and outside of a generator function body, it
should behave as an IdentifierExpression.
es6id: 12.14.5.3
flags: [onlyStrict]
negative: SyntaxError
---*/
var value = [{}];
[...{ x = yield }] = value;
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When a `yield` token appears within the Initializer of a nested
destructuring assignment and outside of a generator function body, it
should behave as an IdentifierExpression.
es6id: 12.14.5.3
flags: [noStrict]
---*/
var value = [{}];
var yield = 2;
var result, iterationResult, iter, x;
result = [...{ x = yield }] = value;
assert.sameValue(result, value);
assert.sameValue(x, 2);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When DestructuringAssignmentTarget is an object literal, it should be
parsed as a DestructuringAssignmentPattern and evaluated as a destructuring
assignment.
es6id: 12.14.5.3
---*/
var value = [1, 2, 3];
var result, x;
result = [...{ 1: x }] = value;
assert.sameValue(result, value);
assert.sameValue(x, 2);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
The assignment target should obey `const` semantics.
es6id: 12.14.5.3
features: [const]
---*/
const c = null;
assert.throws(TypeError, function() {
[ ...c ] = [1];
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
The assignment target should obey `let` semantics.
es6id: 12.14.5.3
features: [let]
---*/
assert.throws(ReferenceError, function() {
[ ...x ] = [];
let x;
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
If the DestructuringAssignmentTarget of an AssignmentElement is a
PropertyReference, it should not be evaluated.
es6id: 12.14.5.3
---*/
var value = [23, 45, 99];
var x, setValue, result;
x = {
get y() {
$ERROR('The property should not be accessed.');
},
set y(val) {
setValue = val;
}
};
result = [...x.y] = value;
assert.sameValue(result, value);
assert.sameValue(setValue.length, 3);
assert.sameValue(setValue[0], 23);
assert.sameValue(setValue[1], 45);
assert.sameValue(setValue[2], 99);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Any error raised as a result of setting the value should be forwarded to
the runtime.
es6id: 12.14.5.3
---*/
var value = [23];
var x = {
set y(val) {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
[...x.y] = value;
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
The DestructuringAssignmentTarget of an AssignmentElement may be a
PropertyReference.
es6id: 12.14.5.3
---*/
var value = [4, 3, 2];
var x = {};
var result;
result = [...x.y] = value;
assert.sameValue(result, value);
assert.sameValue(x.y.length, 3);
assert.sameValue(x.y[0], 4);
assert.sameValue(x.y[1], 3);
assert.sameValue(x.y[2], 2);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Outside of strict mode, if the the assignment target is an unresolvable
reference, a new `var` binding should be created in the environment record.
es6id: 12.14.5.3
flags: [noStrict]
---*/
{
[ ...unresolvable ] = [];
}
assert.sameValue(unresolvable.length, 0);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
In strict mode, if the the assignment target is an unresolvable reference,
a ReferenceError should be thrown.
es6id: 12.14.5.3
flags: [onlyStrict]
---*/
assert.throws(ReferenceError, function() {
[ ...unresolvable ] = [];
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When a `yield` token appears within the DestructuringAssignmentTarget of
an AssignmentRestElement and within the body of a generator function, it
should behave as a YieldExpression.
es6id: 12.14.5
features: [generators]
---*/
var value = [33, 44, 55];
var x = {};
var assignmentResult, iterationResult, iter;
iter = (function*() {
assignmentResult = [...x[yield]] = value;
}());
iterationResult = iter.next();
assert.sameValue(assignmentResult, undefined);
assert.sameValue(iterationResult.value, undefined);
assert.sameValue(iterationResult.done, false);
assert.sameValue(x.prop, undefined);
iterationResult = iter.next('prop');
assert.sameValue(assignmentResult, value);
assert.sameValue(iterationResult.value, undefined);
assert.sameValue(iterationResult.done, true);
assert.sameValue(x.prop.length, 3);
assert.sameValue(x.prop[0], 33);
assert.sameValue(x.prop[1], 44);
assert.sameValue(x.prop[2], 55);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
When a `yield` token appears within the DestructuringAssignmentTarget of an
AssignmentRestElement and outside of a generator function body, it should
behave as an IdentifierReference.
es6id: 12.14.5
flags: [onlyStrict]
negative: SyntaxError
---*/
var x = {};
[...x[yield]] = [];
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment