diff --git a/test/built-ins/Date/prototype/toJSON/15.9.5.44-0-1.js b/test/built-ins/Date/prototype/toJSON/15.9.5.44-0-1.js deleted file mode 100644 index baafe4540a..0000000000 --- a/test/built-ins/Date/prototype/toJSON/15.9.5.44-0-1.js +++ /dev/null @@ -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)'); diff --git a/test/built-ins/Date/prototype/toJSON/15.9.5.44-0-2.js b/test/built-ins/Date/prototype/toJSON/15.9.5.44-0-2.js deleted file mode 100644 index ae9ed85df1..0000000000 --- a/test/built-ins/Date/prototype/toJSON/15.9.5.44-0-2.js +++ /dev/null @@ -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'); diff --git a/test/built-ins/Date/prototype/toJSON/builtin.js b/test/built-ins/Date/prototype/toJSON/builtin.js new file mode 100644 index 0000000000..7c7f38c154 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/builtin.js @@ -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); diff --git a/test/built-ins/Date/prototype/toJSON/invoke-abrupt.js b/test/built-ins/Date/prototype/toJSON/invoke-abrupt.js new file mode 100644 index 0000000000..e44e655c74 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/invoke-abrupt.js @@ -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); +}); diff --git a/test/built-ins/Date/prototype/toJSON/invoke-arguments.js b/test/built-ins/Date/prototype/toJSON/invoke-arguments.js new file mode 100644 index 0000000000..865e9d6519 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/invoke-arguments.js @@ -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); diff --git a/test/built-ins/Date/prototype/toJSON/invoke-result.js b/test/built-ins/Date/prototype/toJSON/invoke-result.js new file mode 100644 index 0000000000..0977bda24b --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/invoke-result.js @@ -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 +); diff --git a/test/built-ins/Date/prototype/toJSON/length.js b/test/built-ins/Date/prototype/toJSON/length.js new file mode 100644 index 0000000000..812b0cf511 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/length.js @@ -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, +}); diff --git a/test/built-ins/Date/prototype/toJSON/name.js b/test/built-ins/Date/prototype/toJSON/name.js index 9010ae0ad3..52278e81ff 100644 --- a/test/built-ins/Date/prototype/toJSON/name.js +++ b/test/built-ins/Date/prototype/toJSON/name.js @@ -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, +}); diff --git a/test/built-ins/Date/prototype/toJSON/non-finite.js b/test/built-ins/Date/prototype/toJSON/non-finite.js new file mode 100644 index 0000000000..7117fc42c9 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/non-finite.js @@ -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); diff --git a/test/built-ins/Date/prototype/toJSON/to-object.js b/test/built-ins/Date/prototype/toJSON/to-object.js new file mode 100644 index 0000000000..63d5f28c86 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/to-object.js @@ -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); diff --git a/test/built-ins/Date/prototype/toJSON/to-primitive-abrupt.js b/test/built-ins/Date/prototype/toJSON/to-primitive-abrupt.js new file mode 100644 index 0000000000..c610c159d7 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/to-primitive-abrupt.js @@ -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); +}); diff --git a/test/built-ins/Date/prototype/toJSON/to-primitive-symbol.js b/test/built-ins/Date/prototype/toJSON/to-primitive-symbol.js new file mode 100644 index 0000000000..b1e712aaa8 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/to-primitive-symbol.js @@ -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); diff --git a/test/built-ins/Date/prototype/toJSON/to-primitive-value-of.js b/test/built-ins/Date/prototype/toJSON/to-primitive-value-of.js new file mode 100644 index 0000000000..2d48b11505 --- /dev/null +++ b/test/built-ins/Date/prototype/toJSON/to-primitive-value-of.js @@ -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);