mirror of
https://github.com/tc39/test262.git
synced 2025-07-23 22:15:24 +02:00
Add tests for the "JSON modules" language proposal
https://github.com/tc39/proposal-json-modules This proposal advanced to Stage 3 of TC39's standardization process on 2021-01-27.
This commit is contained in:
parent
56ca8add7d
commit
e793512b55
20
test/language/import/json-extensibility-array.js
Normal file
20
test/language/import/json-extensibility-array.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Creates extensible arrays
|
||||||
|
flags: [module]
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-array_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
value.test262property = 'test262 value';
|
||||||
|
|
||||||
|
verifyProperty(value, 'test262property', {
|
||||||
|
value: 'test262 value',
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
20
test/language/import/json-extensibility-object.js
Normal file
20
test/language/import/json-extensibility-object.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Creates extensible objects
|
||||||
|
flags: [module]
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-object_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
value.test262property = 'test262 value';
|
||||||
|
|
||||||
|
verifyProperty(value, 'test262property', {
|
||||||
|
value: 'test262 value',
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
@ -0,0 +1,6 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
import value from './json-idempotency_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
globalThis.viaSecondModule = value;
|
21
test/language/import/json-idempotency.js
Normal file
21
test/language/import/json-idempotency.js
Normal file
@ -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.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: The same object representation is returned to all import sites
|
||||||
|
flags: [module, async]
|
||||||
|
features: [json-modules, globalThis, dynamic-import]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import viaStaticImport1 from './json-idempotency_FIXTURE.json' assert { type: 'json' };
|
||||||
|
import {default as viaStaticImport2} from './json-idempotency_FIXTURE.json' assert { type: 'json' };
|
||||||
|
import './json-idempotency-indirect_FIXTURE.js';
|
||||||
|
|
||||||
|
assert.sameValue(viaStaticImport1, viaStaticImport2);
|
||||||
|
assert.sameValue(globalThis.viaSecondModule, viaStaticImport1);
|
||||||
|
|
||||||
|
import('./json-idempotency_FIXTURE.json', { assert: { type: 'json' } })
|
||||||
|
.then(function(viaDynamicImport) {
|
||||||
|
assert.sameValue(viaDynamicImport.default, viaStaticImport1);
|
||||||
|
})
|
||||||
|
.then($DONE, $DONE);
|
1
test/language/import/json-idempotency_FIXTURE.json
Normal file
1
test/language/import/json-idempotency_FIXTURE.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{}
|
23
test/language/import/json-invalid.js
Normal file
23
test/language/import/json-invalid.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Does not define
|
||||||
|
info: |
|
||||||
|
# 1.4 ParseJSONModule ( source )
|
||||||
|
|
||||||
|
The abstract operation ParseJSONModule takes a single argument source which
|
||||||
|
is a String representing the contents of a module.
|
||||||
|
|
||||||
|
1. Let json be ? Call(%JSON.parse%, undefined, « source »).
|
||||||
|
2. Return CreateDefaultExportSyntheticModule(json).
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
negative:
|
||||||
|
phase: parse
|
||||||
|
type: SyntaxError
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
import value from './json-invalid_FIXTURE.json' assert { type: 'json' };
|
3
test/language/import/json-invalid_FIXTURE.json
Normal file
3
test/language/import/json-invalid_FIXTURE.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
notJson: 0
|
||||||
|
}
|
20
test/language/import/json-named-bindings.js
Normal file
20
test/language/import/json-named-bindings.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Does not define named bindings
|
||||||
|
info: |
|
||||||
|
In the early design of JSON modules, contributors considered allowing the
|
||||||
|
properties of object values in JSON modules to be imported directly by name.
|
||||||
|
This was ultimately rejected, so attempting to import in this way should
|
||||||
|
produce a SyntaxError.
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
negative:
|
||||||
|
phase: parse
|
||||||
|
type: SyntaxError
|
||||||
|
---*/
|
||||||
|
|
||||||
|
$DONOTEVALUATE();
|
||||||
|
|
||||||
|
import {name} from './json-named-bindings_FIXTURE.json' assert { type: 'json' };
|
3
test/language/import/json-named-bindings_FIXTURE.json
Normal file
3
test/language/import/json-named-bindings_FIXTURE.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"name": 0
|
||||||
|
}
|
47
test/language/import/json-value-array.js
Normal file
47
test/language/import/json-value-array.js
Normal file
@ -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.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Correctly parses the JSON representation of an array
|
||||||
|
info: |
|
||||||
|
# 1.4 ParseJSONModule ( source )
|
||||||
|
|
||||||
|
The abstract operation ParseJSONModule takes a single argument source which
|
||||||
|
is a String representing the contents of a module.
|
||||||
|
|
||||||
|
1. Let json be ? Call(%JSON.parse%, undefined, « source »).
|
||||||
|
2. Return CreateDefaultExportSyntheticModule(json).
|
||||||
|
|
||||||
|
To more fully verify parsing correctness, the source text of the imported
|
||||||
|
module record includes non-printable characters (specifically, all four forms
|
||||||
|
of JSON's so-called "whitespace" token) both before and after the "value."
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-array_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
assert(Array.isArray(value), 'the exported value is an array');
|
||||||
|
assert.sameValue(
|
||||||
|
Object.getPrototypeOf(value),
|
||||||
|
Array.prototype,
|
||||||
|
'the exported value is not a subclass of Array'
|
||||||
|
);
|
||||||
|
assert.sameValue(Object.getOwnPropertyNames(value).length, 7);
|
||||||
|
assert.sameValue(value.length, 6);
|
||||||
|
|
||||||
|
assert.sameValue(value[0], -1.2345);
|
||||||
|
assert.sameValue(value[1], true);
|
||||||
|
assert.sameValue(value[2], 'a string value');
|
||||||
|
assert.sameValue(value[3], null);
|
||||||
|
|
||||||
|
assert.sameValue(Object.getPrototypeOf(value[4]), Object.prototype);
|
||||||
|
assert.sameValue(Object.getOwnPropertyNames(value[4]).length, 0);
|
||||||
|
|
||||||
|
assert(Array.isArray(value[5]), 'the fifth element is an array');
|
||||||
|
assert.sameValue(
|
||||||
|
Object.getPrototypeOf(value[5]),
|
||||||
|
Array.prototype,
|
||||||
|
'the fifth element is not a subclass of Array'
|
||||||
|
);
|
||||||
|
assert.sameValue(Object.getOwnPropertyNames(value[5]).length, 1);
|
10
test/language/import/json-value-array_FIXTURE.json
Normal file
10
test/language/import/json-value-array_FIXTURE.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
[
|
||||||
|
-1234.500e-003,
|
||||||
|
true,
|
||||||
|
"a string value",
|
||||||
|
null,
|
||||||
|
{},
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
|
20
test/language/import/json-value-boolean.js
Normal file
20
test/language/import/json-value-boolean.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Correctly parses the JSON representation of a boolean
|
||||||
|
info: |
|
||||||
|
# 1.4 ParseJSONModule ( source )
|
||||||
|
|
||||||
|
The abstract operation ParseJSONModule takes a single argument source which
|
||||||
|
is a String representing the contents of a module.
|
||||||
|
|
||||||
|
1. Let json be ? Call(%JSON.parse%, undefined, « source »).
|
||||||
|
2. Return CreateDefaultExportSyntheticModule(json).
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-boolean_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
assert.sameValue(value, true);
|
1
test/language/import/json-value-boolean_FIXTURE.json
Normal file
1
test/language/import/json-value-boolean_FIXTURE.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
true
|
20
test/language/import/json-value-null.js
Normal file
20
test/language/import/json-value-null.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Correctly parses the JSON representation of null
|
||||||
|
info: |
|
||||||
|
# 1.4 ParseJSONModule ( source )
|
||||||
|
|
||||||
|
The abstract operation ParseJSONModule takes a single argument source which
|
||||||
|
is a String representing the contents of a module.
|
||||||
|
|
||||||
|
1. Let json be ? Call(%JSON.parse%, undefined, « source »).
|
||||||
|
2. Return CreateDefaultExportSyntheticModule(json).
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-null_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
assert.sameValue(value, null);
|
1
test/language/import/json-value-null_FIXTURE.json
Normal file
1
test/language/import/json-value-null_FIXTURE.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
null
|
20
test/language/import/json-value-number.js
Normal file
20
test/language/import/json-value-number.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Correctly parses the JSON representation of a number
|
||||||
|
info: |
|
||||||
|
# 1.4 ParseJSONModule ( source )
|
||||||
|
|
||||||
|
The abstract operation ParseJSONModule takes a single argument source which
|
||||||
|
is a String representing the contents of a module.
|
||||||
|
|
||||||
|
1. Let json be ? Call(%JSON.parse%, undefined, « source »).
|
||||||
|
2. Return CreateDefaultExportSyntheticModule(json).
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-number_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
assert.sameValue(value, -1.2345);
|
1
test/language/import/json-value-number_FIXTURE.json
Normal file
1
test/language/import/json-value-number_FIXTURE.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
-1234.500e-003
|
67
test/language/import/json-value-object.js
Normal file
67
test/language/import/json-value-object.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Correctly parses the JSON representation of an ordinary object
|
||||||
|
info: |
|
||||||
|
# 1.4 ParseJSONModule ( source )
|
||||||
|
|
||||||
|
The abstract operation ParseJSONModule takes a single argument source which
|
||||||
|
is a String representing the contents of a module.
|
||||||
|
|
||||||
|
1. Let json be ? Call(%JSON.parse%, undefined, « source »).
|
||||||
|
2. Return CreateDefaultExportSyntheticModule(json).
|
||||||
|
|
||||||
|
To more fully verify parsing correctness, the source text of the imported
|
||||||
|
module record includes non-printable characters (specifically, all four forms
|
||||||
|
of JSON's so-called "whitespace" token) both before and after the "value."
|
||||||
|
flags: [module]
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-object_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
assert.sameValue(Object.getPrototypeOf(value), Object.prototype);
|
||||||
|
assert.sameValue(Object.getOwnPropertyNames(value).length, 6);
|
||||||
|
|
||||||
|
verifyProperty(value, 'number', {
|
||||||
|
value: -1.2345,
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
verifyProperty(value, 'boolean', {
|
||||||
|
value: true,
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
verifyProperty(value, 'string', {
|
||||||
|
value: 'a string value',
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
verifyProperty(value, 'null', {
|
||||||
|
value: null,
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.sameValue(Object.getPrototypeOf(value.object), Object.prototype);
|
||||||
|
assert.sameValue(Object.getOwnPropertyNames(value.object).length, 0);
|
||||||
|
|
||||||
|
assert(
|
||||||
|
Array.isArray(value.array), 'the value of the "array" property is an array'
|
||||||
|
);
|
||||||
|
assert.sameValue(
|
||||||
|
Object.getPrototypeOf(value.array),
|
||||||
|
Array.prototype,
|
||||||
|
'the value of the "array" property is not a subclass of Array'
|
||||||
|
);
|
||||||
|
assert.sameValue(Object.getOwnPropertyNames(value.array).length, 1);
|
10
test/language/import/json-value-object_FIXTURE.json
Normal file
10
test/language/import/json-value-object_FIXTURE.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
{
|
||||||
|
"number": -1234.500e-003,
|
||||||
|
"boolean": true,
|
||||||
|
"string": "a string value",
|
||||||
|
"null": null,
|
||||||
|
"object": {},
|
||||||
|
"array": []
|
||||||
|
}
|
||||||
|
|
20
test/language/import/json-value-string.js
Normal file
20
test/language/import/json-value-string.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: Correctly parses the JSON representation of a string
|
||||||
|
info: |
|
||||||
|
# 1.4 ParseJSONModule ( source )
|
||||||
|
|
||||||
|
The abstract operation ParseJSONModule takes a single argument source which
|
||||||
|
is a String representing the contents of a module.
|
||||||
|
|
||||||
|
1. Let json be ? Call(%JSON.parse%, undefined, « source »).
|
||||||
|
2. Return CreateDefaultExportSyntheticModule(json).
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import value from './json-value-string_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
assert.sameValue(value, 'a string value');
|
1
test/language/import/json-value-string_FIXTURE.json
Normal file
1
test/language/import/json-value-string_FIXTURE.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
"a string value"
|
13
test/language/import/json-via-namespace.js
Normal file
13
test/language/import/json-via-namespace.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Copyright (C) 2021 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-parse-json-module
|
||||||
|
description: May be imported via a module namespace object
|
||||||
|
flags: [module]
|
||||||
|
features: [json-modules]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
import * as ns from './json-via-namespace_FIXTURE.json' assert { type: 'json' };
|
||||||
|
|
||||||
|
assert.sameValue(Object.getOwnPropertyNames(ns).length, 1);
|
||||||
|
assert.sameValue(ns.default, 262);
|
1
test/language/import/json-via-namespace_FIXTURE.json
Normal file
1
test/language/import/json-via-namespace_FIXTURE.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
262
|
Loading…
x
Reference in New Issue
Block a user