Add Date.prototype.toJSON coverage (#2190)

This commit is contained in:
Alexey Shvayka 2019-06-12 21:59:54 +03:00 committed by Leo Balter
parent 57f0884396
commit 74e0cae407
13 changed files with 362 additions and 27 deletions

View File

@ -1,12 +0,0 @@
// Copyright (c) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
es5id: 15.9.5.44-0-1
description: Date.prototype.toJSON must exist as a function
---*/
var f = Date.prototype.toJSON;
assert.sameValue(typeof(f), "function", 'typeof(f)');

View File

@ -1,10 +0,0 @@
// Copyright (c) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
es5id: 15.9.5.44-0-2
description: Date.prototype.toJSON must exist as a function taking 1 parameter
---*/
assert.sameValue(Date.prototype.toJSON.length, 1, 'Date.prototype.toJSON.length');

View File

@ -0,0 +1,21 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
Tests that Date.prototype.toJSON meets the requirements
for built-in objects defined by the introduction of chapter 17 of
the ECMAScript Language Specification.
includes: [isConstructor.js]
features: [Reflect.construct]
---*/
var toJSON = Date.prototype.toJSON;
assert(Object.isExtensible(toJSON));
assert.sameValue(typeof toJSON, 'function');
assert.sameValue(Object.prototype.toString.call(toJSON), '[object Function]');
assert.sameValue(Object.getPrototypeOf(toJSON), Function.prototype);
assert.sameValue(toJSON.hasOwnProperty('prototype'), false);
assert.sameValue(isConstructor(toJSON), false);

View File

@ -0,0 +1,38 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
Abrupt completion from GetV or Call.
info: |
Date.prototype.toJSON ( key )
[...]
4. Return ? Invoke(O, "toISOString").
Invoke ( V, P [ , argumentsList ] )
[...]
3. Let func be ? GetV(V, P).
4. Return ? Call(func, V, argumentsList).
---*/
var abruptGet = {
get toISOString() {
throw new Test262Error();
},
};
assert.throws(Test262Error, function() {
Date.prototype.toJSON.call(abruptGet);
});
var abruptCall = {
toISOString() {
throw new Test262Error();
},
};
assert.throws(Test262Error, function() {
Date.prototype.toJSON.call(abruptCall);
});

View File

@ -0,0 +1,42 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
toISOString is called with correct context and without arguments.
info: |
Date.prototype.toJSON ( key )
[...]
4. Return ? Invoke(O, "toISOString").
Invoke ( V, P [ , argumentsList ] )
[...]
3. Let func be ? GetV(V, P).
4. Return ? Call(func, V, argumentsList).
---*/
var getCount = 0, getContext;
var callCount = 0, callContext, callArguments;
var obj = {
get toISOString() {
getCount += 1;
getContext = this;
return function() {
callCount += 1;
callContext = this;
callArguments = arguments;
};
},
};
Date.prototype.toJSON.call(obj);
assert.sameValue(getCount, 1);
assert.sameValue(getContext, obj);
assert.sameValue(callCount, 1);
assert.sameValue(callContext, obj);
assert.sameValue(callArguments.length, 0);

View File

@ -0,0 +1,29 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
Result of toISOString call is returned.
info: |
Date.prototype.toJSON ( key )
[...]
4. Return ? Invoke(O, "toISOString").
Invoke ( V, P [ , argumentsList ] )
[...]
3. Let func be ? GetV(V, P).
4. Return ? Call(func, V, argumentsList).
---*/
var date = new Date();
assert.sameValue(date.toJSON(), date.toISOString());
var result = {};
assert.sameValue(
Date.prototype.toJSON.call({
toISOString: function() { return result; },
}),
result
);

View File

@ -0,0 +1,23 @@
// Copyright (C) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
Date.prototype.toJSON.length is 1.
info: |
Date.prototype.toJSON ( key )
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(Date.prototype.toJSON, 'length', {
value: 1,
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -20,8 +20,9 @@ info: |
includes: [propertyHelper.js]
---*/
assert.sameValue(Date.prototype.toJSON.name, "toJSON");
verifyNotEnumerable(Date.prototype.toJSON, "name");
verifyNotWritable(Date.prototype.toJSON, "name");
verifyConfigurable(Date.prototype.toJSON, "name");
verifyProperty(Date.prototype.toJSON, 'name', {
value: 'toJSON',
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -0,0 +1,27 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
If this value coerces to non-finite number, null is returned.
info: |
Date.prototype.toJSON ( key )
[...]
2. Let tv be ? ToPrimitive(O, hint Number).
3. If Type(tv) is Number and tv is not finite, return null.
---*/
var toJSON = Date.prototype.toJSON;
assert.sameValue(
toJSON.call({
get toISOString() { throw new Test262Error(); },
valueOf: function() { return NaN; },
}),
null
);
var num = new Number(-Infinity);
num.toISOString = function() { throw new Test262Error(); };
assert.sameValue(toJSON.call(num), null);

View File

@ -0,0 +1,29 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
This value is coerced to an object.
info: |
Date.prototype.toJSON ( key )
1. Let O be ? ToObject(this value).
features: [Symbol]
---*/
var toJSON = Date.prototype.toJSON;
this.toISOString = function() { return 'global'; };
assert.throws(TypeError, function() {
toJSON.call(undefined);
});
assert.throws(TypeError, function() {
toJSON.call(null);
});
Number.prototype.toISOString = function() { return 'str'; };
assert.sameValue(toJSON.call(10), 'str');
Symbol.prototype.toISOString = function() { return 10; };
assert.sameValue(toJSON.call(Symbol()), 10);

View File

@ -0,0 +1,56 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
Abrupt completion from ToPrimitive.
info: |
Date.prototype.toJSON ( key )
[...]
2. Let tv be ? ToPrimitive(O, hint Number).
ToPrimitive ( input [ , PreferredType ] )
1. Assert: input is an ECMAScript language value.
2. If Type(input) is Object, then
[...]
g. Return ? OrdinaryToPrimitive(input, hint).
OrdinaryToPrimitive ( O, hint )
[...]
5. For each name in methodNames in List order, do
a. Let method be ? Get(O, name).
b. If IsCallable(method) is true, then
i. Let result be ? Call(method, O).
ii. If Type(result) is not Object, return result.
6. Throw a TypeError exception.
---*/
var toJSON = Date.prototype.toJSON;
var getAbrupt = {
get valueOf() {
throw new Test262Error();
},
};
assert.throws(Test262Error, function() {
toJSON.call(getAbrupt);
});
var callAbrupt = {
toString: function() {
throw new Test262Error();
},
};
assert.throws(Test262Error, function() {
toJSON.call(callAbrupt);
});
var notCoercible = Object.create(null);
assert.throws(TypeError, function() {
toJSON.call(notCoercible);
});

View File

@ -0,0 +1,45 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
This value is coerced to primitive with Number hint (exotic @@toPrimitive).
info: |
Date.prototype.toJSON ( key )
[...]
2. Let tv be ? ToPrimitive(O, hint Number).
ToPrimitive ( input [ , PreferredType ] )
1. Assert: input is an ECMAScript language value.
2. If Type(input) is Object, then
[...]
d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
e. If exoticToPrim is not undefined, then
i. Let result be ? Call(exoticToPrim, input, « hint »).
ii. If Type(result) is not Object, return result.
features: [Symbol, Symbol.toPrimitive]
---*/
var callCount = 0, _this, _arguments;
var result = new Boolean(false);
var obj = {
toISOString: function() { return result; },
toString: function() { throw new Test262Error('should not be called'); },
valueOf: function() { throw new Test262Error('should not be called'); },
};
obj[Symbol.toPrimitive] = function() {
callCount += 1;
_this = this;
_arguments = arguments;
return 3.14;
};
assert.sameValue(Date.prototype.toJSON.call(obj), result);
assert.sameValue(callCount, 1);
assert.sameValue(_this, obj);
assert.sameValue(_arguments[0], 'number');
assert.sameValue(_arguments.length, 1);

View File

@ -0,0 +1,46 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.prototype.tojson
description: >
This value is coerced to primitive with Number hint (OrdinaryToPrimitive).
info: |
Date.prototype.toJSON ( key )
[...]
2. Let tv be ? ToPrimitive(O, hint Number).
ToPrimitive ( input [ , PreferredType ] )
1. Assert: input is an ECMAScript language value.
2. If Type(input) is Object, then
[...]
g. Return ? OrdinaryToPrimitive(input, hint).
OrdinaryToPrimitive ( O, hint )
[...]
5. For each name in methodNames in List order, do
a. Let method be ? Get(O, name).
b. If IsCallable(method) is true, then
i. Let result be ? Call(method, O).
ii. If Type(result) is not Object, return result.
---*/
var callCount = 0, _this, _arguments;
var result = [];
var obj = {
toISOString: function() { return result; },
toString: function() { throw new Test262Error('should not be called'); },
valueOf: function() {
callCount += 1;
_this = this;
_arguments = arguments;
return 'NaN';
},
};
assert.sameValue(Date.prototype.toJSON.call(obj), result);
assert.sameValue(callCount, 1);
assert.sameValue(_this, obj);
assert.sameValue(_arguments.length, 0);