Merge pull request #2425 from bcoe/optional-chaining-iteration

feat(optional-chaining): add tests for IterationStatement
This commit is contained in:
Leo Balter 2019-11-19 08:09:25 -08:00 committed by GitHub
commit 2213d13a4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 0 deletions

View File

@ -0,0 +1,18 @@
// Copyright 2019 Google, LLC. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
optional chain in test portion of do while statement
info: |
IterationStatement
do Statement while (OptionalExpression)
features: [optional-chaining]
---*/
let count = 0;
const obj = {a: true};
do {
count++;
break;
} while (obj?.a);
assert.sameValue(1, count);

View File

@ -0,0 +1,35 @@
// Copyright 2019 Google, LLC. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
optional chain RHS of for await statement
info: |
IterationStatement
for await (LeftHandSideExpression of AssignmentExpression) Statement
features: [optional-chaining]
flags: [async]
---*/
const obj = {
iterable: {
[Symbol.asyncIterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return Promise.resolve({ value: this.i++, done: false });
}
return Promise.resolve({ done: true });
}
};
}
}
};
async function checkAssertions() {
let count = 0;
for await (const num of obj?.iterable) {
count += num;
}
assert.sameValue(3, count);
}
checkAssertions().then($DONE, $DONE);

View File

@ -0,0 +1,22 @@
// Copyright 2019 Google, LLC. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
optional chain in test portion of do while statement
info: |
IterationStatement
for (LeftHandSideExpression in Expression) Statement
features: [optional-chaining]
---*/
const obj = {
inner: {
a: 1,
b: 2
}
};
let str = '';
for (const key in obj?.inner) {
str += key;
}
assert.sameValue('ab', str);

View File

@ -0,0 +1,28 @@
// Copyright 2019 Google, LLC. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
optional chain returning undefined in RHS of for of statement
info: |
IterationStatement
for (LeftHandSideExpression of Expression) Statement
features: [optional-chaining]
---*/
assert.throws(TypeError, function() {
for (const key of {}?.a) ;
});
assert.throws(TypeError, function() {
for (const key of {}?.a) {}
});
const obj = undefined;
assert.throws(TypeError, function() {
for (const key of obj?.a) {}
});
assert.throws(TypeError, function() {
for (const key of obj?.a);
});

View File

@ -0,0 +1,43 @@
// Copyright 2019 Google, LLC. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
optional chain in init/test/update of for statement
info: |
IterationStatement
for (Expression; Expression; Expression) Statement
features: [optional-chaining]
---*/
// OptionalExpression in test.
let count;
const obj = {a: true};
for (count = 0; obj?.a; count++) {
if (count > 0) break;
}
assert.sameValue(count, 1);
// OptionalExpression in init/test/update.
let count2 = 0;
const obj2 = undefined;
for (obj?.a; obj2?.a; obj?.a) { count2++; }
assert.sameValue(count2, 0);
for (obj?.a; undefined?.a; obj?.a) { count2++; }
assert.sameValue(count2, 0);
// Short-circuiting
let touched = 0;
const obj3 = {
get a() {
count++;
return undefined; // explicit for clarity
}
};
for (count = 0; true; obj3?.a?.[touched++]) {
if (count > 0) { break; }
}
assert.sameValue(count, 1);
assert.sameValue(touched, 0);

View File

@ -0,0 +1,18 @@
// Copyright 2019 Google, LLC. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: prod-OptionalExpression
description: >
optional chain in test portion of while statement
info: |
IterationStatement
while (Expression) Statement
features: [optional-chaining]
---*/
let count = 0;
const obj = {a: true};
while (obj?.a) {
count++;
break;
}
assert.sameValue(1, count);