Slightly improve JSON.parse coverage (#2196)

This commit is contained in:
Alexey Shvayka 2019-06-12 22:29:04 +03:00 committed by Leo Balter
parent 8984ea8080
commit 395401cba0
11 changed files with 179 additions and 66 deletions

View File

@ -1,22 +0,0 @@
// Copyright (c) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: |
This test should be run without any built-ins being added/augmented.
The name JSON must be bound to an object.
Section 15 says that every built-in Function object described in this
section <EFBFBD> whether as a constructor, an ordinary function, or both <EFBFBD> has
a length property whose value is an integer. Unless otherwise specified,
this value is equal to the largest number of named arguments shown in
the section headings for the function description, including optional
parameters.
This default applies to JSON.parse, and it must exist as a function
taking 2 parameters.
es5id: 15.12.2-0-1
description: JSON.parse must exist as a function
---*/
var f = JSON.parse;
assert.sameValue(typeof(f), "function", 'typeof(f)');

View File

@ -1,23 +0,0 @@
// Copyright (c) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: |
This test should be run without any built-ins being added/augmented.
The name JSON must be bound to an object.
Section 15 says that every built-in Function object described in this
section <EFBFBD> whether as a constructor, an ordinary function, or both <EFBFBD> has
a length property whose value is an integer. Unless otherwise specified,
this value is equal to the largest number of named arguments shown in
the section headings for the function description, including optional
parameters.
This default applies to JSON.parse, and it must exist as a function
taking 2 parameters.
es5id: 15.12.2-0-2
description: JSON.parse must exist as a function taking 2 parameters
---*/
var f = JSON.parse;
assert.sameValue(typeof(f), "function", 'typeof(f)');
assert.sameValue(f.length, 2, 'f.length');

View File

@ -1,16 +0,0 @@
// Copyright (c) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: |
This test should be run without any built-ins being added/augmented.
The initial value of [[Configurable]] on JSON is true. This means we
should be able to delete (8.6.2.5) the stringify and parse properties.
es5id: 15.12.2-0-3
description: JSON.parse must be deletable (configurable)
---*/
var o = JSON;
var desc = Object.getOwnPropertyDescriptor(o, "parse");
assert.sameValue(desc.configurable, true, 'desc.configurable');

View File

@ -0,0 +1,19 @@
// Copyright (C) 2019 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
description: >
Requirements for built-in functions, defined in introduction of chapter 17,
are satisfied.
includes: [isConstructor.js]
features: [Reflect.construct]
---*/
var parse = JSON.parse;
assert(Object.isExtensible(parse));
assert.sameValue(typeof parse, 'function');
assert.sameValue(Object.prototype.toString.call(parse), '[object Function]');
assert.sameValue(Object.getPrototypeOf(parse), Function.prototype);
assert.sameValue(parse.hasOwnProperty('prototype'), false);
assert.sameValue(isConstructor(parse), false);

View File

@ -0,0 +1,25 @@
// Copyright (C) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
description: >
JSON.parse.length is 2.
info: |
JSON.parse ( text [ , reviver ] )
The "length" property of the parse function is 2.
ECMAScript Standard Built-in Objects
Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
verifyProperty(JSON.parse, 'length', {
value: 2,
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -2,6 +2,7 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
es6id: 24.3.1
description: >
JSON.parse.name is "parse".
@ -19,8 +20,9 @@ info: |
includes: [propertyHelper.js]
---*/
assert.sameValue(JSON.parse.name, "parse");
verifyNotEnumerable(JSON.parse, "name");
verifyNotWritable(JSON.parse, "name");
verifyConfigurable(JSON.parse, "name");
verifyProperty(JSON.parse, 'name', {
value: 'parse',
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -0,0 +1,20 @@
// Copyright (C) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
description: >
Property descriptor of JSON.parse.
info: |
ECMAScript Standard Built-in Objects
Every other data property described in clauses 18 through 26 and in Annex B.2
has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
verifyProperty(JSON, 'parse', {
writable: true,
enumerable: false,
configurable: true,
});

View File

@ -0,0 +1,21 @@
// Copyright (C) 2019 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
description: >
Top-level negative zero surrounded by whitespace is parsed correctly.
info: |
JSON.parse ( text [ , reviver ] )
1. Let JText be ? ToString(text).
2. Parse JText interpreted as UTF-16 encoded Unicode points (6.1.4) as a JSON
text as specified in ECMA-404. Throw a SyntaxError exception if JText is not
a valid JSON text as defined in that specification.
---*/
assert.sameValue(JSON.parse('-0'), -0);
assert.sameValue(JSON.parse(' \n-0'), -0);
assert.sameValue(JSON.parse('-0 \t'), -0);
assert.sameValue(JSON.parse('\n\t -0\n '), -0);
assert.sameValue(JSON.parse(-0), 0);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2019 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
description: >
Primitive values are coerced to strings and parsed.
info: |
JSON.parse ( text [ , reviver ] )
1. Let JText be ? ToString(text).
2. Parse JText interpreted as UTF-16 encoded Unicode points (6.1.4) as a JSON
text as specified in ECMA-404. Throw a SyntaxError exception if JText is not
a valid JSON text as defined in that specification.
features: [Symbol]
---*/
assert.throws(SyntaxError, function() {
JSON.parse();
});
assert.throws(SyntaxError, function() {
JSON.parse(undefined);
});
assert.sameValue(JSON.parse(null), null);
assert.sameValue(JSON.parse(false), false);
assert.sameValue(JSON.parse(true), true);
assert.sameValue(JSON.parse(0), 0);
assert.sameValue(JSON.parse(3.14), 3.14);
var sym = Symbol('desc');
assert.throws(TypeError, function() {
JSON.parse(sym);
});

View File

@ -0,0 +1,28 @@
// Copyright (C) 2019 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
description: >
Abrupt completion from Get and Call in ToPrimitive.
info: |
JSON.parse ( text [ , reviver ] )
1. Let JText be ? ToString(text).
---*/
assert.throws(Test262Error, function() {
JSON.parse({
toString: null,
get valueOf() {
throw new Test262Error();
},
});
});
assert.throws(Test262Error, function() {
JSON.parse({
toString: function() {
throw new Test262Error();
},
});
});

View File

@ -0,0 +1,25 @@
// Copyright (C) 2019 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-json.parse
description: >
Objects are coerced to strings using ToString.
info: |
JSON.parse ( text [ , reviver ] )
1. Let JText be ? ToString(text).
2. Parse JText interpreted as UTF-16 encoded Unicode points (6.1.4) as a JSON
text as specified in ECMA-404. Throw a SyntaxError exception if JText is not
a valid JSON text as defined in that specification.
---*/
var hint = JSON.parse({
toString: function() {
return '"string"';
},
valueOf: function() {
return '"default_or_number"';
},
});
assert.sameValue(hint, 'string');