Skip to content

Commit

Permalink
Add tests for import assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed May 14, 2021
1 parent 219ad6f commit 18834b2
Show file tree
Hide file tree
Showing 43 changed files with 1,145 additions and 18 deletions.
4 changes: 4 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,7 @@ class-fields-private-in
# Error cause
# https://github.com/tc39/proposal-error-cause
error-cause

# Import Assertions
# https://github.com/tc39/proposal-import-assertions/
import-assertions
5 changes: 3 additions & 2 deletions src/dynamic-import/not-extensible-args.case
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ desc: ImportCall is not extensible - no arguments list
template: syntax/invalid
info: |
ImportCall :
import( AssignmentExpression[+In, ?Yield] )
import( AssignmentExpression[+In, ?Yield, ?Await] ,opt )
import( AssignmentExpression[+In, ?Yield, ?Await] , AssignmentExpression[+In, ?Yield, ?Await] ,opt )

Forbidden Extensions

- ImportCall must not be extended.
---*/

//- import
import('', '')
import('./empty_FIXTURE.js', {}, '')
16 changes: 0 additions & 16 deletions src/dynamic-import/not-extensible-no-trailing-comma.case

This file was deleted.

14 changes: 14 additions & 0 deletions src/dynamic-import/trailing-comma-first.case
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
desc: ImportCall trailing comma following first parameter
template: syntax/valid
info: |
ImportCall :
import( AssignmentExpression[+In, ?Yield, ?Await] ,opt )
import( AssignmentExpression[+In, ?Yield, ?Await] , AssignmentExpression[+In, ?Yield, ?Await] ,opt )
features: [import-assertions]
---*/

//- import
import('./empty_FIXTURE.js',)
14 changes: 14 additions & 0 deletions src/dynamic-import/trailing-comma-second.case
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2021 V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
desc: ImportCall trailing comma following second parameter
template: syntax/valid
info: |
ImportCall :
import( AssignmentExpression[+In, ?Yield, ?Await] ,opt )
import( AssignmentExpression[+In, ?Yield, ?Await] , AssignmentExpression[+In, ?Yield, ?Await] ,opt )
features: [import-assertions]
---*/

//- import
import('./empty_FIXTURE.js', {},)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Reports abrupt completions produced by assertion enumeration
esid: sec-import-call-runtime-semantics-evaluation
info: |
2.1.1.1 EvaluateImportCall ( specifierExpression [ , optionsExpression ] )
[...]
6. Let promiseCapability be ! NewPromiseCapability(%Promise%).
7. Let specifierString be ToString(specifier).
8. IfAbruptRejectPromise(specifierString, promiseCapability).
9. Let assertions be a new empty List.
10. If options is not undefined, then
a. If Type(options) is not Object,
[...]
b. Let assertionsObj be Get(options, "assert").
c. IfAbruptRejectPromise(assertionsObj, promiseCapability).
d. If assertionsObj is not undefined,
i. If Type(assertionsObj) is not Object,
[...]
ii. Let keys be EnumerableOwnPropertyNames(assertionsObj, key).
iii. IfAbruptRejectPromise(keys, promiseCapability).
[...]
features: [dynamic-import, import-assertions, Proxy]
flags: [async]
---*/

var thrown = new Test262Error();
var options = {
assert: new Proxy({}, {
ownKeys: function() {
throw thrown;
},
})
};

import('./2nd-param_FIXTURE.js', options)
.then(function() {
throw new Test262Error('Expected promise to be rejected, but promise was fulfilled.');
}, function(error) {
assert.sameValue(error, thrown);
})
.then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Follows the semantics of the EnumerableOwnPropertyNames abstract operation
during assertion enumeration
esid: sec-import-call-runtime-semantics-evaluation
info: |
2.1.1.1 EvaluateImportCall ( specifierExpression [ , optionsExpression ] )
[...]
6. Let promiseCapability be ! NewPromiseCapability(%Promise%).
7. Let specifierString be ToString(specifier).
8. IfAbruptRejectPromise(specifierString, promiseCapability).
9. Let assertions be a new empty List.
10. If options is not undefined, then
a. If Type(options) is not Object,
[...]
b. Let assertionsObj be Get(options, "assert").
c. IfAbruptRejectPromise(assertionsObj, promiseCapability).
d. If assertionsObj is not undefined,
i. If Type(assertionsObj) is not Object,
[...]
ii. Let keys be EnumerableOwnPropertyNames(assertionsObj, key).
[...]
features: [dynamic-import, import-assertions, Symbol, Proxy]
flags: [async]
---*/

var symbol = Symbol('');
var target = {
enumerable1: '',
enumerable2: '',
[symbol]: '',
unreported: '',
nonEnumerable: ''
};
var descriptors = {
enumerable1: {configurable: true, enumerable: true},
enumerable2: {configurable: true, enumerable: true},
[symbol]: {configurable: true, enumerable: true},
nonEnumerable: {configurable: true, enumerable: false}
};
var log = [];

var options = {
assert: new Proxy({}, {
ownKeys: function() {
return ['enumerable1', symbol, 'nonEnumerable', 'absent', 'enumerable2'];
},
get(_, name) {
log.push(name);
return target[name];
},
getOwnPropertyDescriptor(target, name) {
return descriptors[name];
}
})
};

import('./2nd-param_FIXTURE.js', options)
.then(function(module) {
assert.sameValue(module.default, 262);
})
.then($DONE, $DONE);

assert.sameValue(log.length, 2);
assert.sameValue(log[0], 'enumerable1');
assert.sameValue(log[1], 'enumerable2');
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Rejects promise when the `assert` property of the second argument is neither
undefined nor an object
esid: sec-import-call-runtime-semantics-evaluation
info: |
2.1.1.1 EvaluateImportCall ( specifierExpression [ , optionsExpression ] )
[...]
6. Let promiseCapability be ! NewPromiseCapability(%Promise%).
7. Let specifierString be ToString(specifier).
8. IfAbruptRejectPromise(specifierString, promiseCapability).
9. Let assertions be a new empty List.
10. If options is not undefined, then
a. If Type(options) is not Object,
[...]
b. Let assertionsObj be Get(options, "assert").
c. IfAbruptRejectPromise(assertionsObj, promiseCapability).
d. If assertionsObj is not undefined,
i. If Type(assertionsObj) is not Object,
1. Perform ! Call(promiseCapability.[[Reject]], undefined, « a
newly created TypeError object »).
2. Return promiseCapability.[[Promise]].
[...]
features: [dynamic-import, import-assertions, Symbol, BigInt]
flags: [async]
---*/

function test(promise, valueType) {
return promise.then(function() {
throw new Test262Error('Promise for ' + valueType + ' was not rejected.');
}, function(error) {
assert.sameValue(error.constructor, TypeError, valueType);
});
}

Promise.all([
test(import('./2nd-param_FIXTURE.js', {assert:null}), 'null'),
test(import('./2nd-param_FIXTURE.js', {assert:false}), 'boolean'),
test(import('./2nd-param_FIXTURE.js', {assert:23}), 'number'),
test(import('./2nd-param_FIXTURE.js', {assert:''}), 'string'),
test(import('./2nd-param_FIXTURE.js', {assert:Symbol('')}), 'symbol'),
test(import('./2nd-param_FIXTURE.js', {assert:23n}), 'bigint')
])
.then(function() {})
.then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Accepts undefined for the `assert` property of the second argument
esid: sec-import-call-runtime-semantics-evaluation
info: |
2.1.1.1 EvaluateImportCall ( specifierExpression [ , optionsExpression ] )
[...]
6. Let promiseCapability be ! NewPromiseCapability(%Promise%).
7. Let specifierString be ToString(specifier).
8. IfAbruptRejectPromise(specifierString, promiseCapability).
9. Let assertions be a new empty List.
10. If options is not undefined, then
a. If Type(options) is not Object,
[...]
b. Let assertionsObj be Get(options, "assert").
c. IfAbruptRejectPromise(assertionsObj, promiseCapability).
d. If assertionsObj is not undefined,
i. If Type(assertionsObj) is not Object,
1. Perform ! Call(promiseCapability.[[Reject]], undefined, « a
newly created TypeError object »).
2. Return promiseCapability.[[Promise]].
[...]
features: [dynamic-import, import-assertions, Symbol, BigInt]
flags: [async]
---*/

Promise.all([
import('./2nd-param_FIXTURE.js', {}),
import('./2nd-param_FIXTURE.js', {assert:undefined}),
])
.then(function(values) {
assert.sameValue(values[0].default, 262);
assert.sameValue(values[1].default, 262);
})
.then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Rejects promise when retrieving a value of the `assert` object produces an
abrupt completion
esid: sec-import-call-runtime-semantics-evaluation
info: |
2.1.1.1 EvaluateImportCall ( specifierExpression [ , optionsExpression ] )
[...]
10. If options is not undefined, then
[...]
d. If assertionsObj is not undefined,
[...]
ii. Let keys be EnumerableOwnPropertyNames(assertionsObj, key).
iii. IfAbruptRejectPromise(keys, promiseCapability).
iv. Let supportedAssertions be ! HostGetSupportedImportAssertions().
v. For each String key of keys,
1. Let value be Get(assertionsObj, key).
2. IfAbruptRejectPromise(value, promiseCapability).
[...]
features: [dynamic-import, import-assertions]
flags: [async]
---*/

var thrown = new Test262Error();

import('./2nd-param_FIXTURE.js', {assert:{get ''() { throw thrown; }}})
.then(function() {
throw new Test262Error('Expected promise to be rejected, but it was fulfilled');
}, function(error) {
assert.sameValue(error, thrown);
})
.then($DONE, $DONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Rejects promise when any property of the `assert` object is not a string
esid: sec-import-call-runtime-semantics-evaluation
info: |
2.1.1.1 EvaluateImportCall ( specifierExpression [ , optionsExpression ] )
[...]
10. If options is not undefined, then
[...]
d. If assertionsObj is not undefined,
[...]
ii. Let keys be EnumerableOwnPropertyNames(assertionsObj, key).
iii. IfAbruptRejectPromise(keys, promiseCapability).
iv. Let supportedAssertions be ! HostGetSupportedImportAssertions().
v. For each String key of keys,
1. Let value be Get(assertionsObj, key).
2. IfAbruptRejectPromise(value, promiseCapability).
3. If Type(value) is not String, then
a. Perform ! Call(promiseCapability.[[Reject]], undefined, « a
newly created TypeError object »).
b. Return promiseCapability.[[Promise]].
[...]
features: [dynamic-import, import-assertions, Symbol, BigInt]
flags: [async]
---*/

function test(promise, valueType) {
return promise.then(function() {
throw new Test262Error('Promise for ' + valueType + ' was not rejected.');
}, function(error) {
assert.sameValue(error.constructor, TypeError, valueType);
});
}

Promise.all([
test(import('./2nd-param_FIXTURE.js', {assert:{'': undefined}}), 'undefined'),
test(import('./2nd-param_FIXTURE.js', {assert:{'': null}}), 'null'),
test(import('./2nd-param_FIXTURE.js', {assert:{'': false}}), 'boolean'),
test(import('./2nd-param_FIXTURE.js', {assert:{'': 23}}), 'number'),
test(import('./2nd-param_FIXTURE.js', {assert:{'': Symbol('')}}), 'symbol'),
test(import('./2nd-param_FIXTURE.js', {assert:{'': 23n}}), 'bigint'),
test(import('./2nd-param_FIXTURE.js', {assert:{'': {}}}), 'object')
])
.then(function() {})
.then($DONE, $DONE);
21 changes: 21 additions & 0 deletions test/language/expressions/dynamic-import/2nd-param-await-expr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
ImportCall parameter list forwards the Await production parameter - AwaitExpression
esid: sec-import-call-runtime-semantics-evaluation
info: |
ImportCall[Yield, Await]:
import ( AssignmentExpression[+In, ?Yield, ?Await] ,opt )
import ( AssignmentExpression[+In, ?Yield, ?Await] , AssignmentExpression[+In, ?Yield, ?Await] ,opt )
features: [dynamic-import, import-assertions, async-functions]
flags: [async]
---*/

(async function () {
return import('./2nd-param_FIXTURE.js', await undefined);
}())
.then(function(module) {
assert.sameValue(module.default, 262);
})
.then($DONE, $DONE);
21 changes: 21 additions & 0 deletions test/language/expressions/dynamic-import/2nd-param-await-ident.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
ImportCall parameter list forwards the Await production parameter - IdentifierReference
esid: sec-import-call-runtime-semantics-evaluation
info: |
ImportCall[Yield, Await]:
import ( AssignmentExpression[+In, ?Yield, ?Await] ,opt )
import ( AssignmentExpression[+In, ?Yield, ?Await] , AssignmentExpression[+In, ?Yield, ?Await] ,opt )
features: [dynamic-import, import-assertions, async-functions]
flags: [async]
---*/

function await() {}

import('./2nd-param_FIXTURE.js', await(undefined))
.then(function(module) {
assert.sameValue(module.default, 262);
})
.then($DONE, $DONE);
Loading

0 comments on commit 18834b2

Please sign in to comment.