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 370 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: >
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 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 object 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 object 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 object 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 Initializer 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 = [{}];
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 outside of a generator function body, it behaves
as a IdentifierReference.
es6id: 12.14.5.3
flags: [onlyStrict]
negative: SyntaxError
---*/
[{ 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 a nested
destructuring assignment outside of a generator function body, it behaves
as an IdentifierReference.
es6id: 12.14.5.3
flags: [noStrict]
---*/
var value = [{}];
var yield = 2;
var result, 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 = [{ x: 2 }];
var result, x;
result = [{ 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() {
'use strict';
[ 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];
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, 23);
// 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];
var x = {};
var result;
result = [x.y] = value;
assert.sameValue(result, value);
assert.sameValue(x.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: >
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, 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: >
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: >
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
---*/
var value = [1, 2, 3];
var x, y, z;
var result;
result = [x, y, z] = value;
assert.sameValue(result, value);
assert.sameValue(x, 1);
assert.sameValue(y, 2);
assert.sameValue(z, 3);
// 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 = [2, 3];
var result, argument, eval;
result = [arguments, eval] = value;
assert.sameValue(result, value);
assert.sameValue(arguments, 2);
assert.sameValue(eval, 3);
// 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 DestructuringAssignmentTarget of an
AssignmentElement within a generator function body, it behaves as a
YieldExpression.
es6id: 12.14.5.4
features: [generators]
---*/
var value = [33];
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, 33);
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