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
No related branches found
No related tags found
No related merge requests found
Showing
with 397 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: >
ArrayAssignmentPattern may include elisions at any position in a
AssignmentElementList that does not contain an AssignmentRestElement.
es6id: 12.14.5
---*/
var value = [1, 2, 3, 4, 5, 6];
var x, y;
var result;
result = [, x, , y, ,] = value;
assert.sameValue(result, value);
assert.sameValue(x, 2);
assert.sameValue(y, 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: >
If the Initializer is present and v is undefined, the Initializer should be
evaluated and the result assigned to the target reference.
es6id: 12.14.5.3
---*/
var result, value, x;
value = [];
result = [ x = 10 ] = value;
assert.sameValue(result, value);
assert.sameValue(x, 10, 'no element at index');
value = [2];
result = [ x = 11 ] = value;
assert.sameValue(result, value);
assert.sameValue(x, 2, 'element at index (truthy value)');
value = [ null ];
result = [ x = 12 ] = value;
assert.sameValue(result, value);
assert.sameValue(x, null, 'element at index (`null`)');
value = [ undefined ];
result = [ x = 13 ] = value;
assert.sameValue(result, value);
assert.sameValue(x, 13, 'element at index (`undefined`)');
value = [ , ];
result = [ x = 14 ] = value;
assert.sameValue(result, value);
assert.sameValue(x, 14, 'element at index (sparse array)');
// 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 Initializer should only be evaluated if v is undefined.
es6id: 12.14.5.3
---*/
var result, value, x, flag;
value = [];
flag = false;
result = [ x = flag = true ] = value;
assert.sameValue(result, value);
assert.sameValue(x, true);
assert.sameValue(flag, true);
value = [14];
flag = false;
result = [ x = flag = true ] = value;
assert.sameValue(result, value);
assert.sameValue(x, 14);
assert.sameValue(flag, false);
// 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 Initializer in an AssignmentElement may be an `in` expression.
es6id: 12.14.5
---*/
var value = [];
var result, x;
result = [ x = 'x' in {} ] = value;
assert.sameValue(result, value);
assert.sameValue(x, false);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Value retrieval of Initializer obeys `let` semantics.
es6id: 12.14.5.3
features: [let]
---*/
var x;
assert.throws(ReferenceError, function() {
[ x = y ] = [];
let y;
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Initializer values should be assigned in left-to-right order.
es6id: 12.14.5.3
---*/
var value = [];
var x = 0;
var a, b, result;
result = [ a = x += 1, b = x *= 2 ] = value;
assert.sameValue(result, value);
assert.sameValue(a, 1);
assert.sameValue(b, 2);
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: >
Identifiers that appear as the DestructuringAssignmentTarget in an
AssignmentElement should take on the iterated value corresponding to their
position in the ArrayAssignmentPattern.
es6id: 12.14.5.3
flags: [noStrict]
---*/
var value = [];
var result, argument, eval;
result = [arguments = 4, eval = 5] = value;
assert.sameValue(result, value);
assert.sameValue(arguments, 4);
assert.sameValue(eval, 5);
// 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 neither an
ObjectLiteral nor an ArrayLiteral and
IsValidSimpleAssignmentTarget(LeftHandSideExpression) is
false.
es6id: 12.14.5.1
flags: [onlyStrict]
negative: SyntaxError
---*/
([arguments] = []);
// 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 an
AssignmentElement within a generator function body, it behaves as a
YieldExpression.
es6id: 12.14.5.4
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(86);
assert.sameValue(assignmentResult, value);
assert.sameValue(iterationResult.value, undefined);
assert.sameValue(iterationResult.done, true);
assert.sameValue(x, 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 a `yield` token appears within the Initializer of an AssignmentElement
outside of a generator function body, it behaves as an IdentifierReference.
es6id: 12.14.5.4
flags: [onlyStrict]
negative: SyntaxError
---*/
var x;
[ x = yield ] = [];
// 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 an AssignmentElement
outside of a generator function body, it behaves as an IdentifierReference.
es6id: 12.14.5.4
flags: [noStrict]
---*/
var value = [];
var yield = 4;
var result, x;
result = [ x = yield ] = value;
assert.sameValue(result, value);
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: >
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
---*/
var x, y;
[[(x, y)]] = [[]];
// 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 value is
`null`, a TypeError should be thrown.
es6id: 12.14.5.3
---*/
var x;
assert.throws(TypeError, function() {
[[ x ]] = [null];
});
// 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 value is a
"hole", a TypeError should be thrown.
es6id: 12.14.5.3
---*/
var x;
assert.throws(TypeError, function() {
[[ 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 array literal and the value is
`undefined`, a TypeError should be thrown.
es6id: 12.14.5.3
---*/
var x;
assert.throws(TypeError, function() {
[[ x ]] = [undefined];
});
// 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 no value is
defined, a TypeError should be thrown.
es6id: 12.14.5.3
---*/
var x;
assert.throws(TypeError, function() {
[[ 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 a `yield` token appears within the DestructuringAssignmentTarget of a
nested destructuring assignment and within a generator function body, it
behaves as a YieldExpression.
es6id: 12.14.5.3
features: [generators]
---*/
var value = [[22]];
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, 22);
// 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 outside of strict mode, it behaves as an
IdentifierReference.
es6id: 12.14.5.3
flags: [onlyStrict]
negative: SyntaxError
---*/
[[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 DestructuringAssignmentTarget of a
nested destructuring assignment outside of strict mode, it behaves as an
IdentifierReference.
es6id: 12.14.5.3
flags: [noStrict]
---*/
var value = [[22]];
var yield = 'prop';
var x = {};
var result;
result = [[x[yield]]] = value;
assert.sameValue(result, value);
assert.sameValue(x.prop, 22);
// 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]];
var x, result;
result = [[x]] = value;
assert.sameValue(result, value);
assert.sameValue(x, 1);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment